//质心发生变化后用平均值来重新计算质心 public double[] findCentroid(Cluster c) { Set<DataPoint> clusterPoints = c.getElements(); int n = clusterPoints.size(); if (n == 0) { return new double[0]; } int d = c.getDimensionCount(); double[] meanAttributes = new double[d]; for (DataPoint p : clusterPoints) { double[] pointAttributes = p.getNumericAttrValues(); for (int i = 0; i < d; i++) { meanAttributes[i] += pointAttributes[i]; } } for (int i = 0; i < d; i++) { meanAttributes[i] = meanAttributes[i] / n; } return meanAttributes; }
//打印结果 public void print() { Cluster[] clusters = this.getAllClusters(); System.out.println("Clusters:"); for (Cluster c : clusters) { System.out.println(c.getElementsAsString()); } }
public Cluster findClusterByElement(DataPoint e) { Cluster cluster = null; for (Cluster c : allClusters) { if (c.contains(e)) { cluster = c; break; } } return cluster; }