这是我第二次发布关于weka的信息(第一篇发布为here)。我成功使用TextDirectoryLoader为Weka提供了培训和示例测试数据。效果很好。现在,我想将其移至生产环境,以便从mysql表中检索要分类的数据。这就是我的做法:
TextDirectoryLoader loader = new TextDirectoryLoader();
loader.setDirectory(new File("c:/Users/Yehia A.Salam/Desktop/dd/training-data"));
Instances dataRaw = loader.getDataSet();
StringToWordVector filter = new StringToWordVector();
filter.setInputFormat(dataRaw);
Instances dataTraining = Filter.useFilter(dataRaw, filter);
// Create test data instances[this works, but the sample data now needs to come frm the db instead, see below]
//loader.setDirectory(new File("c:/Users/Yehia A.Salam/Desktop/dd/test-data"));
//dataRaw = loader.getDataSet();
//Instances dataTest = Filter.useFilter(dataRaw, filter);
InstanceQuery query = new InstanceQuery();
query.setUsername("myusername");
query.setPassword("mypassword");
String sql = "SELECT d.desc FROM deals d WHERE d.j48 = 1";
query.setQuery(sql);
Instances dataTest = Filter.useFilter(query.retrieveInstances(), filter);
// Classify
J48 model = new J48();
model.buildClassifier(dataTraining);
for (int i = 0; i < dataTest.numInstances(); i++) {
dataTest.instance(i).setClassMissing();
double cls = model.classifyInstance(dataTest.instance(i));
dataTest.instance(i).setClassValue(cls);
System.out.println(cls + " -> " + dataTest.instance(i).classAttribute().value((int) cls));
}
不幸的是,这行不通,weka在这条线上意外停止:
Instances dataTest = Filter.useFilter(query.retrieveInstances(), filter);
所以我想我的问题是如何改变这部分
// Create test data instances[this works, but the sample data now needs to come frm the db instead, see below]
//loader.setDirectory(new File("c:/Users/Yehia A.Salam/Desktop/dd/test-data"));
//dataRaw = loader.getDataSet();
//Instances dataTest = Filter.useFilter(dataRaw, filter);
到基于sql的数据
InstanceQuery query = new InstanceQuery();
query.setUsername("myusername");
query.setPassword("mypassword");
String sql = "SELECT d.desc FROM deals d WHERE d.j48 = 1";
query.setQuery(sql);
Instances dataTest = Filter.useFilter(query.retrieveInstances(), filter);
请注意,数据库连接没有问题,实际上我得到了正确数量的实例。
感谢帮助,非常亲密。
最佳答案
您的代码使用基于TextDirectoryLoader的Arff Files from Text Collections类。根据他们的帮助文件
"Loads all text files in a directory and
uses the subdirectory names as class labels.
The content of the text files will be stored in a String attribute,
the filename can be stored as well."
请参阅以下code
double[] newInst = new double[2];
newInst[0] = (double)data.attribute(0).addStringValue(files[i]);
....
newInst[1] = (double)data.attribute(1).addStringValue(txtStr.toString());
data.add(new Instance(1.0, newInst));
如您所见,此代码需要2个属性值来添加数据集。但是您的sql仅提供一个属性。
String sql = "SELECT d.desc FROM deals d WHERE d.j48 = 1";
这可能是代码newInst 1部分中的问题“(java.lang.ArrayIndexOutOfBoundsException)”的原因。 Weka找不到第二个属性。