SlideShare a Scribd company logo
1 of 10
The current program only run one iteration of the KMeans
algorithm. Please revise it (in the main function) to implement
iterative processing, paste your code here, and briefly describe
how it works. Were you able to successfully compile and run
your program (yes/no)?
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import
org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class KMeans {
public static class KMMapper
extends Mapper{
private double [][] _centroids;
private IntWritable cid = new IntWritable();
public void setup(Mapper.Context context){
Configuration conf = context.getConfiguration();
String filename = conf.get("Centroids-file");
_centroids = loadCentroids(filename, conf);
}
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
double [] vec = parseVector(value.toString());
cid.set(closest(vec));
context.write(cid, value);
}
private int closest(double [] v){
double mindist = dist(v, _centroids[0]);
int label =0;
for (int i=1; i<_centroids.length; i++){
double t = dist(v, _centroids[i]);
if (mindist>t){
mindist = t;
label = i;
}
}
return label;
}
}
public static class KMReducer
extends Reducer {
// write output: cid t centroid_vector
private Text result = new Text();
public void reduce(IntWritable key, Iterable vectors,
Context context
) throws IOException, InterruptedException {
double [] sum = null;
int n=0;
for (Text vec : vectors) {
double [] v = parseVector(vec.toString());
if (sum == null) sum = v;
else
for (int i = 0; i < v.length; i++)
sum[i] += v[i];
n ++;
}
String out = Double.toString(sum[0]/n);
for (int i = 1; i < sum.length; i ++ ){
out += "," + Double.toString(sum[i]/n); // csv output
}
result.set(out);
context.write(key, result);
}
}
// compute square Euclidean distance between two vectors v1
and v2
public static double dist(double [] v1, double [] v2){
double sum=0;
for (int i=0; i< v1.length; i++){
double d = v1[i]-v2[i];
sum += d*d;
}
return Math.sqrt(sum);
}
// check convergence condition
// max{dist(c1[i], c2[i]), i=1..numClusters < threshold
private boolean converge(double [][] c1, double [][] c2, double
threshold){
// c1 and c2 are two sets of centroids
double maxv = 0;
for (int i=0; i< c1.length; i++){
double d= dist(c1[i], c2[i]);
if (maxv
maxv = d;
}
if (maxv
return true;
else
return false;
}
public static double [][] loadCentroids(String filename,
Configuration conf){
double [][] centroids=null;
Path p = new Path(filename); // Path is used for opening the
file.
try{
FileSystem fs = FileSystem.get(conf);//determines local or
HDFS
FSDataInputStream file = fs.open(p);
byte[] bs = new byte[file.available()];
file.read(bs);
file.close();
String [] lines = (new String(bs)).split("n"); //lines are
separated by n
for (String line:lines)
System.out.println(line);
centroids = new double[lines.length][];
for (int i = 0; i < lines.length; i++){
// cid t centroid
String [] parts = lines[i].split("t");
int cid = Integer.parseInt(parts[0]);
centroids[cid] = parseVector(parts[1]);
}
}catch(Exception e){
//log.error(e);
System.out.println(e);
}
return centroids;
}
public static double [] parseVector(String s){
String [] itr = s.split(","); // comma separated
double [] v = new double[itr.length];
for (int i = 0; i < itr.length; i++)
v[i] = Double.parseDouble(itr[i]);
return v;
}
public static void main(String[] args) throws Exception {
// usage: hadoop jar km.jar
hdfs://localhost:9000/user/your_home_directory/centroids
data.hdfs output
Configuration conf = new Configuration();
conf.set("Centroids-file", args[0]);
System.out.println(conf.get("Centroids-file"));
Job job = Job.getInstance(conf, "KMeans");
job.setJarByClass(KMeans.class);
job.setMapperClass(KMMapper.class);
//job.setCombinerClass(KMCombiner.class);
job.setReducerClass(KMReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
The current program only run one iteration of the KMeans algorithm. .docx

More Related Content

Similar to The current program only run one iteration of the KMeans algorithm. .docx

От Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовОт Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовYandex
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010Chris Ramsdale
 
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageabilityDaniel Fisher
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleChristopher Curtin
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GDG Devfest 2019 - Build go kit microservices at kubernetes with easeGDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GDG Devfest 2019 - Build go kit microservices at kubernetes with easeKAI CHU CHUNG
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsAzul Systems, Inc.
 
服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScriptQiangning Hong
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with codekamal kotecha
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseSages
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Chris Ramsdale
 
От Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовОт Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовYandex
 
NVIDIA HPC ソフトウエア斜め読み
NVIDIA HPC ソフトウエア斜め読みNVIDIA HPC ソフトウエア斜め読み
NVIDIA HPC ソフトウエア斜め読みNVIDIA Japan
 
Big-data-analysis-training-in-mumbai
Big-data-analysis-training-in-mumbaiBig-data-analysis-training-in-mumbai
Big-data-analysis-training-in-mumbaiUnmesh Baile
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptSeok-joon Yun
 

Similar to The current program only run one iteration of the KMeans algorithm. .docx (20)

От Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовОт Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей Родионов
 
CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
 
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading example
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GDG Devfest 2019 - Build go kit microservices at kubernetes with easeGDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
От Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовОт Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей Родионов
 
NVIDIA HPC ソフトウエア斜め読み
NVIDIA HPC ソフトウエア斜め読みNVIDIA HPC ソフトウエア斜め読み
NVIDIA HPC ソフトウエア斜め読み
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Big-data-analysis-training-in-mumbai
Big-data-analysis-training-in-mumbaiBig-data-analysis-training-in-mumbai
Big-data-analysis-training-in-mumbai
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 

More from todd241

The debate on the ratification of the U.S. Constitution was conducte.docx
The debate on the ratification of the U.S. Constitution was conducte.docxThe debate on the ratification of the U.S. Constitution was conducte.docx
The debate on the ratification of the U.S. Constitution was conducte.docxtodd241
 
The debate over the Constitution is not only crucial to understandin.docx
The debate over the Constitution is not only crucial to understandin.docxThe debate over the Constitution is not only crucial to understandin.docx
The debate over the Constitution is not only crucial to understandin.docxtodd241
 
The Cultural Literature Review includes a review of the litera.docx
The Cultural Literature Review includes a review of the litera.docxThe Cultural Literature Review includes a review of the litera.docx
The Cultural Literature Review includes a review of the litera.docxtodd241
 
The CTO appreciated the analysis performed between the cloud s.docx
The CTO appreciated the analysis performed between the cloud s.docxThe CTO appreciated the analysis performed between the cloud s.docx
The CTO appreciated the analysis performed between the cloud s.docxtodd241
 
The deadline for the final paper is Monday May 11th at 1159pm. .docx
The deadline for the final paper is Monday May 11th at 1159pm. .docxThe deadline for the final paper is Monday May 11th at 1159pm. .docx
The deadline for the final paper is Monday May 11th at 1159pm. .docxtodd241
 
The CRM implementation of the project has been underway for 3 months.docx
The CRM implementation of the project has been underway for 3 months.docxThe CRM implementation of the project has been underway for 3 months.docx
The CRM implementation of the project has been underway for 3 months.docxtodd241
 
The Darth” Side of Technology Use AnInductively Derived Ty.docx
The Darth” Side of Technology Use AnInductively Derived Ty.docxThe Darth” Side of Technology Use AnInductively Derived Ty.docx
The Darth” Side of Technology Use AnInductively Derived Ty.docxtodd241
 
THE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docx
THE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docxTHE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docx
THE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docxtodd241
 
The data in the case are quotes taken from interviews of parents.docx
The data in the case are quotes taken from interviews of parents.docxThe data in the case are quotes taken from interviews of parents.docx
The data in the case are quotes taken from interviews of parents.docxtodd241
 
The Dark Side of the Force”Language & ProcessRisk Aversio.docx
The Dark Side of the Force”Language & ProcessRisk Aversio.docxThe Dark Side of the Force”Language & ProcessRisk Aversio.docx
The Dark Side of the Force”Language & ProcessRisk Aversio.docxtodd241
 
The Criminalization of American BusinessWhat do Bank of Americ.docx
The Criminalization of American BusinessWhat do Bank of Americ.docxThe Criminalization of American BusinessWhat do Bank of Americ.docx
The Criminalization of American BusinessWhat do Bank of Americ.docxtodd241
 
The Creation of the Ocean FloorSCI209Running head .docx
The Creation of the Ocean FloorSCI209Running head .docxThe Creation of the Ocean FloorSCI209Running head .docx
The Creation of the Ocean FloorSCI209Running head .docxtodd241
 
The creation of a weak acid titration plot is described in chapter 1.docx
The creation of a weak acid titration plot is described in chapter 1.docxThe creation of a weak acid titration plot is described in chapter 1.docx
The creation of a weak acid titration plot is described in chapter 1.docxtodd241
 
The Dark Ages World History Crash Course #141. The period b.docx
The Dark Ages World History Crash Course #141. The period b.docxThe Dark Ages World History Crash Course #141. The period b.docx
The Dark Ages World History Crash Course #141. The period b.docxtodd241
 
The Culturally Responsive Social Service Agency The Application o.docx
The Culturally Responsive Social Service Agency The Application o.docxThe Culturally Responsive Social Service Agency The Application o.docx
The Culturally Responsive Social Service Agency The Application o.docxtodd241
 
The cult of Machismo (masculine superiority) appeared early in Latin.docx
The cult of Machismo (masculine superiority) appeared early in Latin.docxThe cult of Machismo (masculine superiority) appeared early in Latin.docx
The cult of Machismo (masculine superiority) appeared early in Latin.docxtodd241
 
The CultureBooksdu r ing t he f ir s t se a son of her.docx
The CultureBooksdu r ing t he f ir s t se a son of her.docxThe CultureBooksdu r ing t he f ir s t se a son of her.docx
The CultureBooksdu r ing t he f ir s t se a son of her.docxtodd241
 
The CultureBooksduring the first season of her criti-.docx
The CultureBooksduring the first season of her criti-.docxThe CultureBooksduring the first season of her criti-.docx
The CultureBooksduring the first season of her criti-.docxtodd241
 
The critique of the Musical Cats must include the following informat.docx
The critique of the Musical Cats must include the following informat.docxThe critique of the Musical Cats must include the following informat.docx
The critique of the Musical Cats must include the following informat.docxtodd241
 
The CSI effect at university forensic science students’ telev.docx
The CSI effect at university forensic science students’ telev.docxThe CSI effect at university forensic science students’ telev.docx
The CSI effect at university forensic science students’ telev.docxtodd241
 

More from todd241 (20)

The debate on the ratification of the U.S. Constitution was conducte.docx
The debate on the ratification of the U.S. Constitution was conducte.docxThe debate on the ratification of the U.S. Constitution was conducte.docx
The debate on the ratification of the U.S. Constitution was conducte.docx
 
The debate over the Constitution is not only crucial to understandin.docx
The debate over the Constitution is not only crucial to understandin.docxThe debate over the Constitution is not only crucial to understandin.docx
The debate over the Constitution is not only crucial to understandin.docx
 
The Cultural Literature Review includes a review of the litera.docx
The Cultural Literature Review includes a review of the litera.docxThe Cultural Literature Review includes a review of the litera.docx
The Cultural Literature Review includes a review of the litera.docx
 
The CTO appreciated the analysis performed between the cloud s.docx
The CTO appreciated the analysis performed between the cloud s.docxThe CTO appreciated the analysis performed between the cloud s.docx
The CTO appreciated the analysis performed between the cloud s.docx
 
The deadline for the final paper is Monday May 11th at 1159pm. .docx
The deadline for the final paper is Monday May 11th at 1159pm. .docxThe deadline for the final paper is Monday May 11th at 1159pm. .docx
The deadline for the final paper is Monday May 11th at 1159pm. .docx
 
The CRM implementation of the project has been underway for 3 months.docx
The CRM implementation of the project has been underway for 3 months.docxThe CRM implementation of the project has been underway for 3 months.docx
The CRM implementation of the project has been underway for 3 months.docx
 
The Darth” Side of Technology Use AnInductively Derived Ty.docx
The Darth” Side of Technology Use AnInductively Derived Ty.docxThe Darth” Side of Technology Use AnInductively Derived Ty.docx
The Darth” Side of Technology Use AnInductively Derived Ty.docx
 
THE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docx
THE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docxTHE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docx
THE CRISIS A DECADE LATERLehman’s Last Hires LookBack.docx
 
The data in the case are quotes taken from interviews of parents.docx
The data in the case are quotes taken from interviews of parents.docxThe data in the case are quotes taken from interviews of parents.docx
The data in the case are quotes taken from interviews of parents.docx
 
The Dark Side of the Force”Language & ProcessRisk Aversio.docx
The Dark Side of the Force”Language & ProcessRisk Aversio.docxThe Dark Side of the Force”Language & ProcessRisk Aversio.docx
The Dark Side of the Force”Language & ProcessRisk Aversio.docx
 
The Criminalization of American BusinessWhat do Bank of Americ.docx
The Criminalization of American BusinessWhat do Bank of Americ.docxThe Criminalization of American BusinessWhat do Bank of Americ.docx
The Criminalization of American BusinessWhat do Bank of Americ.docx
 
The Creation of the Ocean FloorSCI209Running head .docx
The Creation of the Ocean FloorSCI209Running head .docxThe Creation of the Ocean FloorSCI209Running head .docx
The Creation of the Ocean FloorSCI209Running head .docx
 
The creation of a weak acid titration plot is described in chapter 1.docx
The creation of a weak acid titration plot is described in chapter 1.docxThe creation of a weak acid titration plot is described in chapter 1.docx
The creation of a weak acid titration plot is described in chapter 1.docx
 
The Dark Ages World History Crash Course #141. The period b.docx
The Dark Ages World History Crash Course #141. The period b.docxThe Dark Ages World History Crash Course #141. The period b.docx
The Dark Ages World History Crash Course #141. The period b.docx
 
The Culturally Responsive Social Service Agency The Application o.docx
The Culturally Responsive Social Service Agency The Application o.docxThe Culturally Responsive Social Service Agency The Application o.docx
The Culturally Responsive Social Service Agency The Application o.docx
 
The cult of Machismo (masculine superiority) appeared early in Latin.docx
The cult of Machismo (masculine superiority) appeared early in Latin.docxThe cult of Machismo (masculine superiority) appeared early in Latin.docx
The cult of Machismo (masculine superiority) appeared early in Latin.docx
 
The CultureBooksdu r ing t he f ir s t se a son of her.docx
The CultureBooksdu r ing t he f ir s t se a son of her.docxThe CultureBooksdu r ing t he f ir s t se a son of her.docx
The CultureBooksdu r ing t he f ir s t se a son of her.docx
 
The CultureBooksduring the first season of her criti-.docx
The CultureBooksduring the first season of her criti-.docxThe CultureBooksduring the first season of her criti-.docx
The CultureBooksduring the first season of her criti-.docx
 
The critique of the Musical Cats must include the following informat.docx
The critique of the Musical Cats must include the following informat.docxThe critique of the Musical Cats must include the following informat.docx
The critique of the Musical Cats must include the following informat.docx
 
The CSI effect at university forensic science students’ telev.docx
The CSI effect at university forensic science students’ telev.docxThe CSI effect at university forensic science students’ telev.docx
The CSI effect at university forensic science students’ telev.docx
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

The current program only run one iteration of the KMeans algorithm. .docx

  • 1. The current program only run one iteration of the KMeans algorithm. Please revise it (in the main function) to implement iterative processing, paste your code here, and briefly describe how it works. Were you able to successfully compile and run your program (yes/no)? import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  • 2. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class KMeans { public static class KMMapper extends Mapper{ private double [][] _centroids; private IntWritable cid = new IntWritable(); public void setup(Mapper.Context context){ Configuration conf = context.getConfiguration(); String filename = conf.get("Centroids-file"); _centroids = loadCentroids(filename, conf); } public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { double [] vec = parseVector(value.toString()); cid.set(closest(vec));
  • 3. context.write(cid, value); } private int closest(double [] v){ double mindist = dist(v, _centroids[0]); int label =0; for (int i=1; i<_centroids.length; i++){ double t = dist(v, _centroids[i]); if (mindist>t){ mindist = t; label = i; } } return label; } }
  • 4. public static class KMReducer extends Reducer { // write output: cid t centroid_vector private Text result = new Text(); public void reduce(IntWritable key, Iterable vectors, Context context ) throws IOException, InterruptedException { double [] sum = null; int n=0; for (Text vec : vectors) { double [] v = parseVector(vec.toString()); if (sum == null) sum = v; else for (int i = 0; i < v.length; i++) sum[i] += v[i]; n ++; } String out = Double.toString(sum[0]/n);
  • 5. for (int i = 1; i < sum.length; i ++ ){ out += "," + Double.toString(sum[i]/n); // csv output } result.set(out); context.write(key, result); } } // compute square Euclidean distance between two vectors v1 and v2 public static double dist(double [] v1, double [] v2){ double sum=0; for (int i=0; i< v1.length; i++){ double d = v1[i]-v2[i]; sum += d*d; } return Math.sqrt(sum); }
  • 6. // check convergence condition // max{dist(c1[i], c2[i]), i=1..numClusters < threshold private boolean converge(double [][] c1, double [][] c2, double threshold){ // c1 and c2 are two sets of centroids double maxv = 0; for (int i=0; i< c1.length; i++){ double d= dist(c1[i], c2[i]); if (maxv maxv = d; } if (maxv return true; else return false; } public static double [][] loadCentroids(String filename,
  • 7. Configuration conf){ double [][] centroids=null; Path p = new Path(filename); // Path is used for opening the file. try{ FileSystem fs = FileSystem.get(conf);//determines local or HDFS FSDataInputStream file = fs.open(p); byte[] bs = new byte[file.available()]; file.read(bs); file.close(); String [] lines = (new String(bs)).split("n"); //lines are separated by n for (String line:lines) System.out.println(line); centroids = new double[lines.length][]; for (int i = 0; i < lines.length; i++){ // cid t centroid String [] parts = lines[i].split("t");
  • 8. int cid = Integer.parseInt(parts[0]); centroids[cid] = parseVector(parts[1]); } }catch(Exception e){ //log.error(e); System.out.println(e); } return centroids; } public static double [] parseVector(String s){ String [] itr = s.split(","); // comma separated double [] v = new double[itr.length]; for (int i = 0; i < itr.length; i++) v[i] = Double.parseDouble(itr[i]); return v; } public static void main(String[] args) throws Exception {
  • 9. // usage: hadoop jar km.jar hdfs://localhost:9000/user/your_home_directory/centroids data.hdfs output Configuration conf = new Configuration(); conf.set("Centroids-file", args[0]); System.out.println(conf.get("Centroids-file")); Job job = Job.getInstance(conf, "KMeans"); job.setJarByClass(KMeans.class); job.setMapperClass(KMMapper.class); //job.setCombinerClass(KMCombiner.class); job.setReducerClass(KMReducer.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[1])); FileOutputFormat.setOutputPath(job, new Path(args[2])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }