使用 NaiveBayes 分类器对 Weka 中的一个实例进行分类

Posted

技术标签:

【中文标题】使用 NaiveBayes 分类器对 Weka 中的一个实例进行分类【英文标题】:Classifying one instance in Weka using NaiveBayes Classifier 【发布时间】:2012-02-14 00:54:02 【问题描述】:

我想知道是否有办法使用朴素贝叶斯训练模型,然后将其应用于单个记录。我是 weka 的新手,所以我不知道这是否可能。另外,有没有办法将分类器输出存储在文件中?

【问题讨论】:

【参考方案1】:

答案是肯定的,因为朴素贝叶斯是一种基于简单概率贝叶斯定理的模型,可用于分类挑战。

对于使用朴素贝叶斯和其他分类器的分类,您需要首先使用样本数据集训练模型,一旦训练,模型就可以应用于任何记录。

当然,使用这种方法时总会存在错误概率,但这主要取决于样本的质量和数据集的属性。

我没有直接使用 Weka,而是作为 Rapid Miner 的扩展,但原则必须适用。训练模型后,您应该能够查看/打印模型参数。

【讨论】:

您好,我也需要这种类型的分类。现在我正在构建分类。但是我不知道如何解析单个记录并获得类值的概率。【参考方案2】:

我目前正在使用 java 寻找相同的答案。

我创建了一个 arff 文件,其中包含训练日期,并以程序 http://weka.wikispaces.com/file/view/WekaDemo.java 为例来训练和评估分类器。

我仍然需要弄清楚如何在 java 中保存和加载模型以及(更重要的是)如何针对单个记录进行测试。

WekaDemo.java

 ...
 public void execute() throws Exception 
    // run filter
    m_Filter.setInputFormat(m_Training);
    Instances filtered = Filter.useFilter(m_Training, m_Filter);

    // train classifier on complete file for tree
    m_Classifier.buildClassifier(filtered);

    // 10fold CV with seed=1
    m_Evaluation = new Evaluation(filtered);
    m_Evaluation.crossValidateModel(
        m_Classifier, filtered, 10, m_Training.getRandomNumberGenerator(1));
    //TODO Save model
    //TODO Load model
    //TODO Test against a single information
  
  ...

编辑 1:

这里解释了保存和加载模型:How to test existing model with new instance in weka, using java code?

【讨论】:

【参考方案3】:

在http://weka.wikispaces.com/Use+WEKA+in+your+Java+code#Classification-Classifying%20instances 中有一个快速分类单个实例的方法。

//load model (saved from user interface)
Classifier tree = (Classifier) weka.core.SerializationHelper.read("/some/where/j48.model");

// load unlabeled data
Instances unlabeled = new Instances( new BufferedReader(new FileReader("/some/where/unlabeled.arff")));

// set class attribute
unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
// create copy
Instances labeled = new Instances(unlabeled);

// label instances
for (int i = 0; i < unlabeled.numInstances(); i++) 
  double clsLabel = tree.classifyInstance(unlabeled.instance(i));
  labeled.instance(i).setClassValue(clsLabel);
  System.out.println(clsLabel + " -> " + unlabeled.classAttribute().value((int) clsLabel));
  double[] dist =tree.distributionForInstance(unlabeled.instance(i))
  for(int j=0; j<dist.length;j++)
    System.print(unlabeled.classAttribute().value(j)+": " +dist[j]);
  

编辑此方法不训练、评估和保存模型。这是我通常使用 weka gui 做的事情。 (http://weka.wikispaces.com/Serialization) 此方法在示例中使用带有名义类的树类型模型,但应该很容易将其转换为朴素贝叶斯示例。

【讨论】:

以上是关于使用 NaiveBayes 分类器对 Weka 中的一个实例进行分类的主要内容,如果未能解决你的问题,请参考以下文章

为啥 weka NaiveBayes 类不实现分类实例方法?

为啥 WEKA NaiveBayes 分类器会给出标准。开发。全零属性的值?

从命令行运行 weka - 找不到类 NaiveBayes

使用预训练模型对一个实例进行分类时,NaiveBayes 分类器出错

Weka 逻辑分类器不可用

如何提高 Weka 中 SMO 分类器的性能?