/// <summary> /// Assignes vector to this cluster. </summary> /// <param name="vector"> vector to assign </param> public virtual void assignVector(KVector vector) { // if vector's cluster is allready set to this, save some cpu cycles if (vector.Cluster != this) { vector.Cluster = this; vectors.Add(vector); } }
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; foreach (DataSetRow row in dataSet.Rows) { KVector vector = new KVector(row.Input); this.dataVectors[i] = vector; i++; } }
/// <summary> /// Calculates and returns euclidean distance of this vector from the given cluster </summary> /// <param name="cluster"> </param> /// <returns> euclidean distance of this vector from given cluster </returns> public virtual double distanceFrom(KVector otherVector) { // get values and do this in loop double[] otherValues = otherVector.Values; double distance = 0; for (int i = 0; i < values.Length; i++) { distance += Math.Pow(otherValues[i] - values[i], 2); } distance = Math.Sqrt(distance); return(distance); }
/// <summary> /// Find and return the nearest cluster for the specified vector </summary> /// <param name="vector"> </param> /// <returns> </returns> private Cluster getNearestCluster(KVector vector) { Cluster nearestCluster = null; double minimumDistanceFromCluster = double.MaxValue; double distanceFromCluster = 0; foreach (Cluster cluster in clusters) { distanceFromCluster = vector.distanceFrom(cluster.Centroid); if (distanceFromCluster < minimumDistanceFromCluster) { minimumDistanceFromCluster = distanceFromCluster; nearestCluster = cluster; } } return(nearestCluster); }
// 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 virtual void initClusters() { List <int> idxList = new List <int>(); for (int i = 0; i < dataSet.size(); i++) { idxList.Add(i); } Shuffle(idxList); // log.append("Clusters initialized at:\n\n"); clusters = new Cluster[numberOfClusters]; for (int i = 0; i < numberOfClusters; i++) { clusters[i] = new Cluster(); int randomIdx = idxList[i]; KVector randomVector = dataVectors[randomIdx]; clusters[i].Centroid = randomVector; //log.append(randomVector.toString()+System.lineSeparator() ); } //log.append(System.lineSeparator()); }
public virtual void removePoint(KVector point) { vectors.Remove(point); }