/** * KMeans constructor: the cluster centroids are obtained for the given dataset. Firstly, the * cluster's centroids are randomly chosen. Then the centroids are updated as the mean vlaue of * nearest examples in the dataset. The updating is carried out until no changes in the centroids * is achieved. * * @param X The dataset to be clusterized * @param nclusters The desired number of clusters * @param vrand The Randomize object to be used */ public KMeans(double[][] X, int nclusters, Randomize vrand) { rand = vrand; train = X; clusters = nclusters; cclusters = new double[nclusters][X[0].length]; for (int i = 0; i < nclusters; i++) { int pos = (int) (rand.Rand() * X.length); for (int j = 0; j < cclusters[i].length; j++) cclusters[i][j] = X[pos][j]; } int[] C = new int[X.length]; int[] C_old = new int[X.length]; for (int i = 0; i < X.length; i++) { C_old[i] = nearestCentroid(X[i]); } centroidsUpdating(C_old); int cambios = 0, iteracion = 0; do { iteracion++; System.out.println("Iter=" + iteracion + " changes=" + cambios); cambios = 0; for (int i = 0; i < X.length; i++) { C[i] = nearestCentroid(X[i]); if (C[i] != C_old[i]) cambios++; C_old[i] = C[i]; } centroidsUpdating(C); } while (cambios > 0); }