Question: Note: An object of the class java.util.Random has a method nextInt ( int n ) that returns a randomly generated number in the range [
Note: An object of the class java.util.Random has a method nextIntint n that returns a randomly generated number in the range n
Note: When a cluster has only one example, that example is centroid!
Provide me with the Clustering.java class given the following:
FeatureVector.java class:
public class FeatureVector
private String name;
private double features;
private boolean verbose;
public FeatureVectorString name, int size
this.name name;
this.features new doublesize;
this.verbose false;
public FeatureVectorString name, double elems
this.name name;
this.features new doubleelemslength;
System.arraycopyelems this.features, elems.length;
this.verbose false;
public String getName
return name;
public int getSize
return features.length;
public double featureAtint index
return featuresindex;
public void featureSetint index, double value
featuresindex value;
public FeatureVector copy
return new FeatureVectorthisname, this.features;
public double getDistanceFeatureVector other
if thisgetSize other.getSize
throw new IllegalArgumentExceptionVectors must have the same size";
double sumOfSquares ;
for int i ; i this.getSize; i
double diff this.featureAti other.featureAti;
sumOfSquares diff diff;
return Math.sqrtsumOfSquares;
public void plusFeatureVector other
if thisgetSize other.getSize
throw new IllegalArgumentExceptionVectors must have the same size";
for int i ; i this.getSize; i
this.featuresi other.featureAti;
public void divFeatureVector other
if thisgetSize other.getSize
throw new IllegalArgumentExceptionVectors must have the same size";
for int i ; i this.getSize; i
if otherfeatureAti
throw new ArithmeticExceptionDivision by zero";
this.featuresi other.featureAti;
public void divdouble value
if value
throw new ArithmeticExceptionDivision by zero";
for int i ; i this.getSize; i
this.featuresi value;
public void setVerboseboolean value
this.verbose value;
@Override
public String toString
StringBuilder sb new StringBuilder;
if verbose
sbappendnameappend: ;
sbappend;
for int i ; i features.length; i
sbappendfeaturesi;
if i features.length
sbappend;
sbappend;
return sbtoString;
public boolean equalsFeatureVector other
if thisgetSize other.getSize
return false;
for int i ; i this.getSize; i
if thisfeatureAti other.featureAti
return false;
return true;
Cluster.java class:
public class Cluster
private FeatureVector elements;
private int capacity;
private int size;
public Clusterint capacity
this.capacity capacity;
elements new FeatureVectorcapacity;
size ;
public int getSize
return size;
public boolean addFeatureVector elem
if size capacity
return false;
elementssize elem;
return true;
public FeatureVector getElementAtint index
if index index size
return null;
return elementsindex;
public FeatureVector getCentroid
if size return null;
int featureLength elementsgetSize;
double centroidFeatures new doublefeatureLength;
for int i ; i size; i
for int j ; j featureLength; j
centroidFeaturesj elementsifeatureAtj;
for int j ; j featureLength; j
centroidFeaturesj size;
return new FeatureVectorcentroid centroidFeatures;
public double getVariance
FeatureVector centroid getCentroid;
if centroid null return ;
double variance ;
for int i ; i size; i
variance Math.powelementsigetDistancecentroid;
return Math.sqrt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
