我阅读了很多在 Java 中使用这个库的例子,并且可以从 ARFF 数据文件中进行集群并且它有效。
但是我在运行我的程序时生成的双列表中有我自己的数据,我不知道如何使用这个 k-means 算法来聚类我的数据。这是一维列表。
这是我的代码:
Instances dataa = DataSource.read("C:\\Users\\Ew\\Documents\\iris.arff");
// create the model
kMeans = new SimpleKMeans();
kMeans.setNumClusters(3);
kMeans.buildClusterer(dataa);
// print out the cluster centroids
Instances centroids = kMeans.getClusterCentroids();
for (int i = 0; i < centroids.numInstances(); i++) {
System.out.println( "Centroid " + i+1 + ": " + centroids.instance(i));
}
// get cluster membership for each instance
for (int i = 0; i < dataa.numInstances(); i++) {
System.out.println( dataa.instance(i) + " is in cluster " + kMeans.clusterInstance(dataa.instance(i)) + 1);
}
我从 iris.arff 文件中读取数据并且它正在运行。现在我想给 k-means 我的双列表作为参数。我该怎么做?
预先感谢您的回答。
问候。
最佳答案
如果您不想通过读取DataSource
来创建一组Instances
,您也可以使用任何实现Instance
接口(interface)的类手动创建它,例如一个 DenseInstance
。请参阅 javadoc 中的示例代码:
// Create empty instance with three attribute values
Instance inst = new DenseInstance(3);
// Set instance's values for the attributes "length", "weight", and "position"
inst.setValue(length, 5.3);
inst.setValue(weight, 300);
inst.setValue(position, "first");
// Set instance's dataset to be the dataset "race"
inst.setDataset(race);
希望对您有所帮助。
关于java - k-means weka java代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25668512/