【问题标题】:keyword @relation expected weka java关键字@relation 预期 weka java
【发布时间】:2023-03-17 07:59:01
【问题描述】:

我在java中使用weka实现了一个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 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)

你能帮帮我吗!

【问题讨论】:

  • 尝试在 weka explorer 中加载你的文件,你会得到同样的错误。修复你的 arff 文件,使其可以在 weka explorer 中加载,然后尝试编写 java 代码。
  • 重新阅读 arff 格式规范。您的文件不是正确的 arff 文件。
  • 也明白k-means只能使用数值数据。顾名思义,它需要计算 mean。 “玩具总动员”和“冒险”是什么意思?

标签: java exception attributes weka k-means


【解决方案1】:

我在使用 Java 程序时也遇到过类似的问题。

程序总是需要有效的 *.arff 文件格式,如下所示:

@relation **<file name>**
@attribute <attribute1-name> ...
@attribute <attribute2-name> ...

@data
1,1
1,2
1,1.333333
...
...

如果您在使用 CSV 文件时遇到类似的异常。请在此处查看https://www.researchgate.net/publication/280612446_Using_WEKA_in_your_java_code_Clustering

【讨论】:

    【解决方案2】:

    正如它所说,文件必须以@relation 而不是@attribute 开头

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 2013-04-19
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多