java - java中使用weka的实例分类中的ArrayIndexOutOfBoundsException
问题描述
我正在编写一个程序来将给定的测试实例分类为“正面”或“负面”;使用 j48 算法。我使用 Weka 工具创建了模型,并在我的 java 程序中使用它。
我的 Java 程序
public static void main(String[] args) {
double result = -1;
try {
ArrayList<Attribute> attributeList = new ArrayList<>(2);
Attribute tweet = new Attribute("tweet", true);
ArrayList<String> classVal = new ArrayList<>();
classVal.add("positive");
classVal.add("negative");
attributeList.add(tweet);
attributeList.add(new Attribute("class", classVal));
Instances data = new Instances("TestInstances", attributeList, 1);
Instance inst_co = new DenseInstance(data.numAttributes());
data.add(inst_co);
data.setClassIndex(data.numAttributes() - 1);
inst_co.setValue(tweet, "I love my grandmom");
StringToWordVector filter = new StringToWordVector();
Classifier cls_co = (Classifier) weka.core.SerializationHelper
.read("G:/love.model");
result = cls_co.classifyInstance(data.firstInstance());
System.out.println(result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
我的训练数据文件
@RELATION love
@ATTRIBUTE tweet string
@ATTRIBUTE class {positive,negative}
@DATA
"I love my sister", positive
"I love my brother", positive
"I love my father", positive
"I love my mother", positive
"I love my uncle", positive
"I love my grandmom", positive
"I hate my cats", negative
"I hate my car", negative
"I hate my van", negative
"I hate my rice", negative
"I hate my bun", negative
我从 ArrayIndexOutOfBoundsExceptionresult = cls_co.classifyInstance(data.firstInstance());
我尝试了很多在互联网上找到的例子。但我仍然遇到同样的错误。
提前致谢。
解决方案
推荐阅读
- android - 如何始终从 DateUtils.getRelativeTimeSpanString 获取分钟数?
- mysql - MySQL:条件排序依据
- html - 我如何让我的文本也进入下一个,然后在我的按钮下?
- python - 如何将列表项添加到字典中?
- mysql - 我如何在mysql中进行大计算
- html - 如何在图像悬停而不是文本悬停上显示文本 - CSS/SASS
- python - 通过 __getitem__ 方法重载更改类型是否很好?
- python - Redis mget 无法与 redis-py-cluster 客户端按预期工作
- wordpress - 显示自定义类型分类概览页面
- database - 社交账户和普通账户数据库模型