public class KMeans
extends java.lang.Object
Constructor and Description |
---|
KMeans(int k)
Given a parameter k specifying the number of clusters, read data points from standard input,
perform k-means clustering, and print the centroids and clustered data to standard output.
|
Modifier and Type | Method and Description |
---|---|
boolean |
assignNewClusters()
Assign each data point to the nearest centroid and return whether or not any cluster assignments changed.
|
void |
computeNewCentroids()
Compute new centroids at the mean point of each cluster of points.
|
double |
getDistance(double[] p1,
double[] p2)
Return the Euclidean distance between the two given point vectors.
|
double |
getWCSS()
Return the within-cluster sum of squares measure for the current centroids and clustering assignments.
|
int[] |
kMeansCluster()
Perform k-means clustering with Forgy initialization and return the 0-based cluster assignments for corresponding data points..
|
static void |
main(java.lang.String[] args)
Launch k-means clustering given a command line parameter of k, the number of clusters.
|
void |
printClusters()
Print results of k-means clustering in the following output format:
|
public KMeans(int k)
% {number1} dimensions % {number2} points
k
- positive integer number of clusterspublic double getDistance(double[] p1, double[] p2)
p1
- point vector 1p2
- point vector 2public double getWCSS()
public boolean assignNewClusters()
public void computeNewCentroids()
public int[] kMeansCluster()
public void printClusters()
Output format: % {number1} dimensions % {number2} points % {number3} clusters/centroids % {number4} within-cluster sum of squares Each of the next {number3} + {number2} lines is space-separated and contains point coordinates followed by the point's associated 0-based cluster number. In the first {number3} lines, the coordinates are of the centroids, ordered by ascending centroid cluster number. In the next {number2} lines, the coordinates are from the original data.
public static void main(java.lang.String[] args)
args
- a single positive integer number of clusters