package cbr;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.FarthestFirst;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
public class Cluster {
protected int m_ClusterCentroids;
protected Instances
protected Instances m_instances;
protected int m_Seed;
public static void main(String[] args) throws Exception {
Instances data = new Instances(new
BufferedReader(new
FileReader("FinalDatasetforCBSAPDS.arff")));
data.setClassIndex(data.numAttributes() -1);
Remove filter = new Remove();
filter.setAttributeIndices("" +(data.classIndex() + 1));
filter.setInputFormat(data);
Instances dataClusterer = Filter.useFilter(data, filter);
FarthestFirst clusterer = new FarthestFirst();
clusterer.setNumClusters(5);
clusterer.setSeed(5127); // set the seed size
String[] options = new String[2];
int nClusters = 5;
int seed = 5127;
options[0] = String.valueOf(nClusters);
options[1] = String.valueOf(seed);
clusterer.setOptions(options);
ClusterEvaluation eval= new ClusterEvaluation();
eval.setClusterer(clusterer);
eval.evaluateClusterer(data);
System.out.println("# of clusters: " +
eval.getNumClusters());
clusterer.buildClusterer(dataClusterer);
eval.setClusterer(clusterer);
eval.evaluateClusterer(data);
System.out.println(eval.clusterResultsToString());
Instances centroids = clusterer.getClusterCentroids();
int numclusters = clusterer.getNumClusters();
String filename = "Final_Dataset";
filename += "for_CBSAPDS";
filename += ".txt";
File file = new File(filename);
try (Writer writer = new BufferedWriter(new
FileWriter(file))) {
for (int i= 0; i<=5127; i++) {
for (int j= 0; j<numclusters; j++) {
String centroidsStr= centroids.instance(i).toString();
String[] to = centroidsStr.split(",");
writer.write(Integer.toString(i));
writer.write(",");
writer.write(data.instance(j) + "," + "case" + i + "," + (to[0]));
writer.write("\n");
}
}
System.out.println("\nInformation:");
}
}
}
这是我编写的将farthestfirst聚类算法与CBR集成在一起的farthestfirst聚类算法的Java代码。但这会引发异常:
线程“ main”中的异常java.lang.Exception:非法选项:5 527
在weka.core.Utils.checkForRemainingOptions(Utils.java:505)在weka.clusterers.FarthesFirst.setOptions(FarthestFirst.java:568)
在cbr.Cluster.main(Cluster.java)
所以在这个问题上我需要帮助
答案 0 :(得分:0)
您将错误的参数传递给setOptions(..)
方法。 The API指出,您可以提供以下字符串形式的参数:
"-N <number of clusters>"
和"-S <seed>"
因此,如果您想使用setOption
方法,则需要构造看起来像这样的参数。但是,这似乎很麻烦,因为此方法与setSeed
和setNumClusters
(您已经在使用)相同,因此您可能会摆脱setOptions(..)
方法和相应的方法参数。