2023 The current program only run one iteration of the KMeans algorithm Please revise it | Assignment Collections

Computer Science 2023 KM Algorithm Hadoop

2023 The current program only run one iteration of the KMeans algorithm Please revise it | Assignment Collections

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<Object, Text, IntWritable, Text>{

   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<IntWritable, Text, IntWritable, Text> {
   // write output: cid t centroid_vector
   private Text result = new Text();

   public void reduce(IntWritable key, Iterable<Text> 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<d)
           maxv = d;
   }

   if (maxv <threshold)
     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);
 }
}

 

We give our students 100% satisfaction with their assignments, which is one of the most important reasons students prefer us to other helpers. Our professional group and planners have more than ten years of rich experience. The only reason is that we have successfully helped more than 100000 students with their assignments on our inception days. Our expert group has more than 2200 professionals in different topics, and that is not all; we get more than 300 jobs every day more than 90% of the assignment get the conversion for payment.

Place Order Now

#write essay #research paper #blog writing #article writing #academic writer #reflective paper #essay pro #types of essays #write my essay #reflective essay #paper writer #essay writing service #essay writer free #essay helper #write my paper #assignment writer #write my essay for me #write an essay for me #uk essay #thesis writer #dissertation writing services #writing a research paper #academic essay #dissertation help #easy essay #do my essay #paper writing service #buy essay #essay writing help #essay service #dissertation writing #online essay writer #write my paper for me #types of essay writing #essay writing website #write my essay for free #reflective report #type my essay #thesis writing services #write paper for me #research paper writing service #essay paper #professional essay writers #write my essay online #essay help online #write my research paper #dissertation writing help #websites that write papers for you for free #write my essay for me cheap #pay someone to write my paper #pay someone to write my research paper #Essaywriting #Academicwriting #Assignmenthelp #Nursingassignment #Nursinghomework #Psychologyassignment #Physicsassignment #Philosophyassignment #Religionassignment #History #Writing #writingtips #Students #universityassignment #onlinewriting #savvyessaywriters #onlineprowriters #assignmentcollection #excelsiorwriters #writinghub #study #exclusivewritings #myassignmentgeek #expertwriters #art #transcription #grammer #college #highschool #StudentsHelpingStudents #studentshirt #StudentShoe #StudentShoes #studentshoponline #studentshopping #studentshouse #StudentShoutout #studentshowcase2017 #StudentsHub #studentsieuczy #StudentsIn #studentsinberlin #studentsinbusiness #StudentsInDubai #studentsininternational