0
即時通訊編程很新穎。我編碼了一個簡單的貝葉斯分類器。 這裏是代碼:Weka分類器不起作用
public static void main(String[] args) throws Exception {
Attribute Attribute1 = new Attribute("firstNumeric");
Attribute Attribute2 = new Attribute("secondNumeric");
// Declare a nominal attribute along with its values
ArrayList<String> fvNominalVal = new ArrayList(3);
fvNominalVal.add("blue");
fvNominalVal.add("gray");
fvNominalVal.add("black");
Attribute Attribute3 = new Attribute("aNominal", fvNominalVal);
// Declare the class attribute along with its values
ArrayList<String> fvClassVal = new ArrayList(2);
fvClassVal.add("positive");
fvClassVal.add("negative");
Attribute ClassAttribute = new Attribute("theClass", fvClassVal);
// Declare the feature vector
ArrayList<Attribute> fvWekaAttributes = new ArrayList(4);
fvWekaAttributes.add(Attribute1);
fvWekaAttributes.add(Attribute2);
fvWekaAttributes.add(Attribute3);
fvWekaAttributes.add(ClassAttribute);
// Create an empty training set
Instances isTrainingSet = new Instances("Rel", fvWekaAttributes, 10);
// Set class index
isTrainingSet.setClassIndex(3);
// Create the instance
Instance ex1 = new DenseInstance(4);
ex1.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
ex1.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
ex1.setValue((Attribute) fvWekaAttributes.get(2), "gray");
ex1.setValue((Attribute) fvWekaAttributes.get(3), "positive");
Instance ex2 = new DenseInstance(4);
ex1.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
ex1.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
ex1.setValue((Attribute) fvWekaAttributes.get(2), "blue");
ex1.setValue((Attribute) fvWekaAttributes.get(3), "negative");
// add the instance
isTrainingSet.add(ex1);
isTrainingSet.add(ex2);
// Create a naïve bayes classifier
Classifier cModel = (Classifier) new NaiveBayes();
cModel.buildClassifier(isTrainingSet);
Instance testData = new DenseInstance(4);
testData.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
testData.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
testData.setValue((Attribute) fvWekaAttributes.get(2), "gray");
Instances testDataSet = new Instances("Rel", fvWekaAttributes, 1);
testDataSet.setClassIndex(3);
testDataSet.add(testData);
double[] a = cModel.distributionForInstance(testDataSet.firstInstance());
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
但結果似乎並不爲真。 這裏是結果:
6.702810252023562E-151
1.0
即使我改變TESTDATA這樣:
testData.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
testData.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
testData.setValue((Attribute) fvWekaAttributes.get(2), "blue");
的結果幾乎是這個。如下:
3.351405126011781E-151
1.0
感謝在先進。