我可以告诉你的一件事是我在 IDE 之外开发了一个应用程序。您只需要在您的应用程序目录中获取 WEKA 包文件夹并将其作为包导入您的 java 类应用程序中,然后直接调用 WEKA 分类器或您希望的任何其他工作。例如,我正在使用 PART Classifier 以下代码工作并且不言自明。
import java.io.*;
import java.util.Random;
import weka.core.Instances;
import weka.classifiers.Classifier;
import weka.classifiers.rules.PART;
import weka.classifiers.Evaluation;
public class Prediction
{
public double Create(String Rf1,String Rf2,String Rf3) throws Exception
{
input p = new input();
double res=p.classify(Rf1,Rf2,Rf3);
return res;
}
}
class input
{
public double classify(String file1,String file2,String file3) throws Exception
{
// ------------> 1. Reading from an ARFF file
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
Instances data = new Instances(br);
br.close();
// setting class attribute
data.setClassIndex(data.numAttributes() - 1);
// -------------> 2. Building a Classifier
String[] options = new String[1];
options[0] = "M 2 -C 0.25 -Q 1"; // confidenceFactor = 0.25, minNumObject = 2
PART tree = new PART(); // new instance of tree
tree.setOptions(options); // set the options
tree.buildClassifier(data); // build classifier
// -------------> 3. Cross-validation
Evaluation eval = new Evaluation(data);
eval.crossValidateModel(tree, data, 10, new Random(1));
// check --------------> 4. Train
Instances train = new Instances(data);
Instances test = new Instances(train);
// train classifier
Classifier cls = new PART();
cls.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval1 = new Evaluation(train);
eval1.evaluateModel(cls, test);
//System.out.println(eval1.toSummaryString("\nResults\n======\n", false));
// ----------------> 5. Statistics
String[] options1 = new String[2];
options1[0] = "-t";
options1[1] = file1;
// ----------------> 6. Classifying instances
// load unlabeled data
FileReader fr2 = new FileReader(file2);
BufferedReader br2 = new BufferedReader(fr2);
Instances unlabeled = new Instances(br2);
// set class attribute
unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
// create copy
Instances labeled = new Instances(unlabeled);
double clsLabel[];
clsLabel = new double[100];
int count = 0;
// label instances
for (int i = 0; i < unlabeled.numInstances(); i++)
{
clsLabel[i] = tree.classifyInstance(unlabeled.instance(i));
labeled.instance(i).setClassValue(clsLabel[i]);
count++;
}
// save labeled data
FileWriter fw = new FileWriter(file3);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(labeled.toString());
bw.newLine();
bw.flush();
bw.close();
fw.close();
for(int i=0;i<count;i++)
System.out.println(clsLabel[i]);
return clsLabel[0];
} // main
} // class