【问题标题】:k-means weka java codek-means weka java代码
【发布时间】:2014-10-29 09:10:19
【问题描述】:

我阅读了很多在 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 我的双重列表作为参数。我该怎么做?

提前感谢您的回答。

问候。

【问题讨论】:

  • 你没看懂k是什么意思?
  • 如果你能告诉我如何在 weka 库中的这个算法中使用我自己的数据而不是 arff 文件

标签: java weka k-means


【解决方案1】:

如果您不想通过读取DataSource 来创建一组Instances,您也可以手动创建它,使用任何实现Instance 接口的类,例如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);

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2011-08-13
    • 2011-10-04
    • 2012-11-08
    • 2016-07-15
    • 2013-04-20
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多