【问题标题】:Java/WEKA: K Clustering error: Cannot handle any class attributeJava/WEKA:K 聚类错误:无法处理任何类属性
【发布时间】:2014-12-18 13:00:44
【问题描述】:
    SimpleKMeans kmeans = new SimpleKMeans();
    int numberOfClusters = 2;
    int[] assignments = null;

    kmeans.setSeed(10);

    // This is the important parameter to set
    kmeans.setPreserveInstancesOrder(true);
    try {
        kmeans.setNumClusters(numberOfClusters);
        kmeans.buildClusterer(instancesOne); // <-- exception being thrown
        // This array returns the cluster number (starting with 0) for each instance
        // The array has as many elements as the number of instances
        assignments = kmeans.getAssignments();
    } catch (Exception e) {
        e.printStackTrace();
    }

我正在尝试通过 k-means 算法初始化 EM 算法的参数。所以我试图获得 2 个质心,我可以在其中进一步训练 GMM 的参数。但是,我收到以下错误:

weka.core.WekaException: weka.clusterers.SimpleKMeans: Cannot handle any class attribute!
    at weka.core.Capabilities.test(Unknown Source)
    at weka.core.Capabilities.test(Unknown Source)
    at weka.core.Capabilities.testWithFail(Unknown Source)
    at weka.clusterers.SimpleKMeans.buildClusterer(Unknown Source)
    at hmm.HMM.run(HMM.java:62)
    at hmm.HMM.main(HMM.java:22)
Exception in thread "main" java.lang.NullPointerException
    at hmm.HMM.run(HMM.java:71)
    at hmm.HMM.main(HMM.java:22)

另外,我如何设置两个随机质心。我认为 setSeed() 方法可以做到这一点,但我如何让它与我的数据集相匹配?我的 csv 文件如下所示:

然后加载它:

Instances instancesOne = loader.loadCsv("train", "class1");

以下是加载时属性的一些信息:

dataset:

@relation class1

@attribute x numeric
@attribute y numeric

@data
-9.0278,3.1518
-9.5656,3.6383
-9.805,3.8284
etc...

回答,需要此代码来使 Instances 无类(删除类属性):

// remove class attribute, make class-less
Instances dataClusterer = null;
weka.filters.unsupervised.attribute.Remove filter = new weka.filters.unsupervised.attribute.Remove();
filter.setAttributeIndices("" + (instancesOne.classIndex() + 1));
try {
    filter.setInputFormat(instancesOne);
    dataClusterer = Filter.useFilter(instancesOne, filter);
} catch (Exception e1) {
    e1.printStackTrace();
    return;
}

【问题讨论】:

  • 代码中抛出的异常在哪里?
  • @MatthewSpencer 更新问题,顶一下!

标签: java weka


【解决方案1】:

我不认为 K-Means 聚类需要类属性。如果您为您的实例设置了一个,请尝试将其删除并重新运行代码。 This guide 可能有助于构建聚类模型的方法。

希望这会有所帮助!

【讨论】:

  • 很奇怪,我在您发布的链接中收到与 EM 方法相同的错误。这个类属性怎么去掉,看来是万恶之源!
  • 你是对的,我不得不手动删除类属性。
猜你喜欢
  • 2015-04-26
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 2019-09-12
  • 2020-10-11
  • 1970-01-01
相关资源
最近更新 更多