java - 韦卡分类器

标签 java classification weka naivebayes

我编写了一个简单的贝叶斯分类器。 这是代码:

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

最佳答案

在我看来,问题在于训练集中只有两个实例,并且 naiv baies 分类器无法从中学习有值(value)的模型。这就是为什么你得到了坦白结果。 尝试生成至少 100 个或更多训练实例,或者在这里您可以找到一些示例数据集来学习如何应用 ML 方法:http://storm.cis.fordham.edu/~gweiss/data-mining/datasets.html

关于java - 韦卡分类器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977576/

相关文章:

java - 将 pdf 转换为缩略图并将该数据也保存到 solr 中吗?

r - R 中的分类/预测

dataset - 大型分类文档语料库

machine-learning - 朴素贝叶斯,不那么朴素吗?

Java 维卡 : How to specify split percentage?

sqlite - 如何将SQLite数据库连接到Weka

java - 用于 Java 持久性的 JPA 或 Hibernate?

java - 如何修复 NoSuchMethodError?

artificial-intelligence - 人工智能和机器学习中的随机性

java - 从 Retrivel API 中的消息中提取 OTP(自动短信验证)