package org.neuroph.nnet.learning.kmeans;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
/**
*
* 1. Pick an initial set of K centroids (this can be random or any other means)
* 2. For each data point, assign it to the member of the closest centroid according to the given distance function
* 3. Adjust the centroid position as the mean of all its assigned member data points. Go back to (2) until the membership isn't change and centroid position is stable.
*
* @author Zoran Sevarac
* @author Uros Stojkic
*/
public class KMeansClustering {
/**
* Data/points to cluster
*/
private DataSet dataSet;
private KVector[] dataVectors;
/**
* Total number of clusters
*/
private int numberOfClusters;
/**
* Clusters
*/
private Cluster[] clusters;
StringBuilder log=new StringBuilder();
public KMeansClustering(DataSet dataSet) {
this.dataSet = dataSet;
this.dataVectors = new KVector[dataSet.size()];
// iterate dataset and create dataVectors field
this.dataVectors = new KVector[dataSet.size()];
// iterate dataset and create dataVectors field
int i=0;
for(DataSetRow row : dataSet.getRows()) {
KVector vector = new KVector(row.getInput());
this.dataVectors[i]=vector;
i++;
}
}
public KMeansClustering(DataSet dataSet, int numberOfClusters) {
this.dataSet = dataSet;
this.numberOfClusters = numberOfClusters;
this.dataVectors = new KVector[dataSet.size()];
// iterate dataset and create dataVectors field
int i=0;
for(DataSetRow row : dataSet.getRows()) {
KVector vector = new KVector(row.getInput());
this.dataVectors[i]=vector;
i++;
}
}
// find initial values for centroids/clusters
// dont need to return string this is just for debuging and output in dialog
// forgy and random partitions
// http://en.wikipedia.org/wiki/K-means_clustering
public void initClusters() {
ArrayList idxList = new ArrayList<>();
for(int i=0; i0) { // that cointains data
double[] avgSum = cluster.getAvgSum(); // calculate avg sum
cluster.getCentroid().setValues(avgSum); // and set new centroid to avg sum
}
}
}
public DataSet getDataSet() {
return dataSet;
}
public void setDataSet(DataSet vectors) {
this.dataSet = vectors;
}
public void setNumberOfClusters(int numberOfClusters) {
this.numberOfClusters = numberOfClusters;
}
public Cluster[] getClusters() {
return clusters;
}
public String getLog() {
return log.toString();
}
}
Ads help maintain this website.