我使用weka在java中实现了kmeans算法
所以我有这个代码java,我复制自:Cannot handle any class attribute! kmeans java
public class demo {
public demo() throws Exception {
// TODO Auto-generated constructor stub
BufferedReader breader = null;
breader = new BufferedReader(new FileReader(
"D:/workspace_java/JenaTutorial/movie.arff"));
Instances Train = new Instances(breader);
//Train.setClassIndex(Train.numAttributes() - 1); // comment out this line
SimpleKMeans kMeans = new SimpleKMeans();
kMeans.setSeed(10);
kMeans.setPreserveInstancesOrder(true);
kMeans.setNumClusters(3);
kMeans.buildClusterer(Train);
int[] assignments = kMeans.getAssignments();
int i = 0;
for (int clusterNum : assignments) {
System.out.printf("Instance %d -> Cluster %d", i, clusterNum);
i++;
}
breader.close();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new demo();
}
}
它是我的文件“movie.arff”的摘录:
@attribute title string
@attribute annee string
@attribute genre string
@attribute genre1 string
@attribute genre2 string
@attribute genre3 string
@attribute actor string
@attribute actor1 string
@attribute actor2 string
@attribute actor3 string
@attribute actor4 string
@attribute actor5 string
@attribute actor6 string
@attribute actor7 string
@attribute actor8 string
@attribute actor9 string
@attribute writer string
@attribute writer1 string
@attribute writer2 string
@attribute writer3 string
@attribute writer4 string
@attribute writer5 string
@attribute writer6 string
@attribute writer7 string
@attribute writer8 string
@attribute writer9 string
@data
'Toy Story','1995','Adventure','Animation','Children','Comedy','Fantasy','Tom Hanks','Wallace Shawn','John Ratzenberger','John Morris ','Annie Potts','Don Rickles','Laurie Metcalf','Tim Allen','Jim Varney','Erik von Detten','Joel Cohen (writer)','Joss Whedon','Andrew Stanton','Alec Sokolow',
'Jumanji','1995','Adventure','Children','Fantasy','?','Robin Williams','Kirsten Dunst','David Alan Grier','Bonnie Hunt','Bebe Neuwirth','Jonathan Hyde','?','?','?','?','Greg Taylor (author)','Jonathan Hensleigh','?','?','?','?','?','?','?','?','Greg Taylor (author)','Jonathan Hensleigh','?','?',
'Grumpier Old Men','1995','Comedy','Romance','?','?','Sophia Loren','Walter Matthau','Jack Lemmon','Ann-Margret','Burgess Meredith','Daryl Hannah','Ann Morgan Guilbert','Kevin Pollak','?','?','Mark Steven Johnson','?','?','?','?','?','?','?','?','?','Mark Steven Johnson','?','?','?',
'Waiting to Exhale','1995','Comedy','Drama','Romance','?','Whitney Houston','Lela Rochon','Loretta Devine','Angela Bassett','?','?','?','?','?','?','Ronald Bass','Terry McMillan','?','?','?','?','?','?','?','?','Ronald Bass','Terry McMillan','?','?',
'Father of the Bride Part II','1995','Comedy','?','?','?','Steve Martin','Martin Short','Diane Keaton','?','?','?','?','?','?','?','Charles Shyer','Albert Hackett','Frances Goodrich','Nancy Meyers','?','?','?','?','?','?','Charles Shyer','Albert Hackett','Frances Goodrich','Nancy Meyers',
但在执行过程中我有这个异常(exception):
Exception in thread "main" java.io.IOException: keyword @relation expected, read Token[@attribute], line 1
at weka.core.converters.ArffLoader$ArffReader.errorMessage(ArffLoader.java:354)
at weka.core.converters.ArffLoader$ArffReader.readHeader(ArffLoader.java:806)
at weka.core.converters.ArffLoader$ArffReader.<init>(ArffLoader.java:204)
at weka.core.Instances.<init>(Instances.java:135)
at wakaproject.demo.<init>(demo.java:14)
at wakaproject.demo.main(demo.java:31)
你能帮帮我吗?
最佳答案
正如它所说,文件必须以 @relation 而不是 @attribute 开头
关于java - 关键字 @relation 预期 weka java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731772/