gpt4 book ai didi

java - 使用自己的 Java 代码和模型在 WEKA 中获取预测百分比

转载 作者:搜寻专家 更新时间:2023-11-01 03:06:09 25 4
gpt4 key购买 nike

概览

我知道可以通过 GUI 和命令行选项获得经过训练的 WEKA 模型中每个预测的百分比,如文档文章 "Making predictions" 中方便地解释和演示的那样.

WHAT I WANT WITH MY WEKA OOOHH *LADY GAGA PIANO*

预测

我知道记录了三种获得这些预测的方法:

  1. command line
  2. GUI
  3. Java 代码/使用 WEKA API,我在对 "Get risk predictions in WEKA using own Java code" 的回答中做到了这一点
  4. 这第四个需要一个生成的 WEKA .MODEL 文件

我有一个经过训练的 .MODEL 文件,现在我想使用它和预测百分比对新实例进行分类,类似于下面的(GUI 的输出资源管理器,CSV 格式):

inst#,actual,predicted,error,distribution,
1,1:0,2:1,+,0.399409,*0.7811
2,1:0,2:1,+,0.3932409,*0.8191
3,1:0,2:1,+,0.399409,*0.600591
4,1:0,2:1,+,0.139409,*0.64
5,1:0,2:1,+,0.399409,*0.600593
6,1:0,2:1,+,0.3993209,*0.600594
7,1:0,2:1,+,0.500129,*0.600594
8,1:0,2:1,+,0.399409,*0.90011
9,1:0,2:1,+,0.211409,*0.60182
10,1:0,2:1,+,0.21909,*0.11101

predicted 列是我想从 .MODEL 文件中得到的。


我所知道的

根据我使用 WEKA API 方法的经验,可以使用以下代码(将 PlainText 插入到 Evaluation 对象中)获得这些预测,但我不想执行由 Evaluation 对象提供的 k-fold 交叉验证。

StringBuffer predictionSB = new StringBuffer();
Range attributesToShow = null;
Boolean outputDistributions = new Boolean(true);

PlainText predictionOutput = new PlainText();
predictionOutput.setBuffer(predictionSB);
predictionOutput.setOutputDistribution(true);

Evaluation evaluation = new Evaluation(data);
evaluation.crossValidateModel(j48Model, data, numberOfFolds,
randomNumber, predictionOutput, attributesToShow,
outputDistributions);

System.out.println(predictionOutput.getBuffer());

来自 WEKA 文档

请注意,.MODEL 文件对来自 .ARFF 或相关输入的数据进行分类,在 "Use Weka in your Java code" 中进行了讨论。和 "Serialization" a.k.a.“如何在您自己的 Java 代码中使用 .MODEL 文件对新实例进行分类”(为什么标题模糊不清)。

使用自己的Java代码进行分类

加载 .MODEL 文件是通过“反序列化”,以下是版本 > 3.5.5:

// deserialize model
Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/j48.model");

Instance 对象是数据,它被提供给 classifyInstance。此处提供输出(取决于结果属性的数据类型):

// classify an Instance object (testData)
cls.classifyInstance(testData.instance(0));

问题"How to reuse saved classifier created from explorer(in weka) in eclipse java"也有很好的答案!

Java文档

我已经检查了 Classifier 的 Javadocs (经过训练的模型)和 Evaluation (以防万一)但没有人直接明确地解决这个问题。

唯一最接近我想要的是 ClassifierclassifyInstances 方法:

Classifies the given test instance. The instance has to belong to a dataset when it's being classified. Note that a classifier MUST implement either this or distributionForInstance().


如何使用我自己的 Java 代码(也称为使用 WEKA API)同时使用 WEKA .MODEL 文件对新实例进行分类和预测?

WHAT I WANT WITH MY WEKA OOOHH *LADY GAGA PIANO*

最佳答案

这个答案只是更新了我在 How to reuse saved classifier created from explorer(in weka) in eclipse java 中的答案.

我将展示如何获得预测实例值和预测百分比(或分布)。示例模型是在 Weka Explorer 中创建和保存的 J48 决策树。它是根据 Weka 提供的名义天气数据构建的。它被称为“tree.model”。

import weka.classifiers.Classifier;
import weka.core.Instances;

public class Main {

public static void main(String[] args) throws Exception
{
String rootPath="/some/where/";
Instances originalTrain= //instances here

//load model
Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"tree.model");

//predict instance class values
Instances originalTrain= //load or create Instances to predict

//which instance to predict class value
int s1=0;

//perform your prediction
double value=cls.classifyInstance(originalTrain.instance(s1));

//get the prediction percentage or distribution
double[] percentage=cls.distributionForInstance(originalTrain.instance(s1));

//get the name of the class value
String prediction=originalTrain.classAttribute().value((int)value);

System.out.println("The predicted value of instance "+
Integer.toString(s1)+
": "+prediction);

//Format the distribution
String distribution="";
for(int i=0; i <percentage.length; i=i+1)
{
if(i==value)
{
distribution=distribution+"*"+Double.toString(percentage[i])+",";
}
else
{
distribution=distribution+Double.toString(percentage[i])+",";
}
}
distribution=distribution.substring(0, distribution.length()-1);

System.out.println("Distribution:"+ distribution);
}

}

输出结果为:

The predicted value of instance 0: no  
Distribution: *1, 0

关于java - 使用自己的 Java 代码和模型在 WEKA 中获取预测百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21674522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com