【发布时间】: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 更新问题,顶一下!