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 nextInt(int n) that returns a randomly generated number in the range [0,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 FeatureVector(String name, int size){
this.name = name;
this.features = new double[size];
this.verbose = false;
}
public FeatureVector(String name, double[] elems){
this.name = name;
this.features = new double[elems.length];
System.arraycopy(elems,0, this.features, 0, elems.length);
this.verbose = false;
}
public String getName(){
return name;
}
public int getSize(){
return features.length;
}
public double featureAt(int index){
return features[index];
}
public void featureSet(int index, double value){
features[index]= value;
}
public FeatureVector copy(){
return new FeatureVector(this.name, this.features);
}
public double getDistance(FeatureVector other){
if (this.getSize()!= other.getSize()){
throw new IllegalArgumentException("Vectors must have the same size");
}
double sumOfSquares =0.0;
for (int i =0; i this.getSize(); i++){
double diff = this.featureAt(i)- other.featureAt(i);
sumOfSquares += diff * diff;
}
return Math.sqrt(sumOfSquares);
}
public void plus(FeatureVector other){
if (this.getSize()!= other.getSize()){
throw new IllegalArgumentException("Vectors must have the same size");
}
for (int i =0; i this.getSize(); i++){
this.features[i]+= other.featureAt(i);
}
}
public void div(FeatureVector other){
if (this.getSize()!= other.getSize()){
throw new IllegalArgumentException("Vectors must have the same size");
}
for (int i =0; i this.getSize(); i++){
if (other.featureAt(i)==0){
throw new ArithmeticException("Division by zero");
}
this.features[i]/= other.featureAt(i);
}
}
public void div(double value){
if (value ==0){
throw new ArithmeticException("Division by zero");
}
for (int i =0; i this.getSize(); i++){
this.features[i]/= value;
}
}
public void setVerbose(boolean value){
this.verbose = value;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
if (verbose){
sb.append(name).append(": ");
}
sb.append("{");
for (int i =0; i features.length; i++){
sb.append(features[i]);
if (i features.length -1){
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
public boolean equals(FeatureVector other){
if (this.getSize()!= other.getSize()){
return false;
}
for (int i =0; i this.getSize(); i++){
if (this.featureAt(i)!= other.featureAt(i)){
return false;
}
}
return true;
}
}
Cluster.java class:
public class Cluster {
private FeatureVector[] elements;
private int capacity;
private int size;
public Cluster(int capacity){
this.capacity = capacity;
elements = new FeatureVector[capacity];
size =0;
}
public int getSize(){
return size;
}
public boolean add(FeatureVector elem){
if (size >= capacity){
return false;
}
elements[size++]= elem;
return true;
}
public FeatureVector getElementAt(int index){
if (index 0|| index >= size){
return null;
}
return elements[index];
}
public FeatureVector getCentroid(){
if (size ==0) return null;
int featureLength = elements[0].getSize();
double[] centroidFeatures = new double[featureLength];
for (int i =0; i size; i++){
for (int j =0; j featureLength; j++){
centroidFeatures[j]+= elements[i].featureAt(j);
}
}
for (int j =0; j featureLength; j++){
centroidFeatures[j]/= size;
}
return new FeatureVector("centroid", centroidFeatures);
}
public double getVariance(){
FeatureVector centroid = getCentroid();
if (centroid == null) return 0;
double variance =0;
for (int i =0; i size; i++){
variance += Math.pow(elements[i].getDistance(centroid),2);
}
return Math.sqrt
Note: An object of the class java.util.Random has

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!