- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我知道可以通过 GUI 和命令行选项获得经过训练的 WEKA 模型中每个预测的百分比,如文档文章 "Making predictions" 中方便地解释和演示的那样.
我知道记录了三种获得这些预测的方法:
.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());
请注意,.MODEL
文件对来自 .ARFF
或相关输入的数据进行分类,在 "Use Weka in your Java code" 中进行了讨论。和 "Serialization" a.k.a.“如何在您自己的 Java 代码中使用 .MODEL
文件对新实例进行分类”(为什么标题模糊不清)。
加载 .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"也有很好的答案!
我已经检查了 Classifier
的 Javadocs (经过训练的模型)和 Evaluation
(以防万一)但没有人直接明确地解决这个问题。
唯一最接近我想要的是 Classifier
的 classifyInstances
方法:
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
文件对新实例进行分类和预测?
最佳答案
这个答案只是更新了我在 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/
我最近开始使用 weka,我正在尝试使用朴素贝叶斯将推文分为正面或负面。因此,我有一个训练集,其中包含我为其指定标签的推文,以及一个包含所有带有“正面”标签的推文的测试集。当我运行朴素贝叶斯时,我得到
我正在使用来自 UCI 的成人数据 Here ,当我将它转换为excel文件时==>然后在weka中导入 weka 无法识别缺失值(它告诉 Missing:0 (0%)), 但成人数据包含带有“?”的
我对数据挖掘并不陌生,所以我完全被 WEKA 结果难住了。希望得到一些帮助。提前致谢! 我有一个具有二进制分类(S,H)的数字向量数据集。我训练了一个 NaiveBayes 模型(尽管方法真的无关紧要
我正在使用 Weka 上的多层感知器生成功率模型,Weka 是一个统计工具箱。 Weka 显示了以下生成的功率模型,但是,我不知道如何解释它。 如何使用 Weka 生成的模型计算预测值?我想知道如何用
我必须在我的 java 代码中使用 WEKA 进行预测。基本上我必须研究给定的代码并重用它。 testdata.setClassIndex(data.numAttributes() - 1); 我无法
您好,我正在尝试使用 java CSVLoader 在 weka 中加载管道分隔文件。看起来 CSVLoader 只加载逗号和制表符。有什么办法可以更改这些加载器上的分隔符吗? 有没有人在 Weka
我已经使用 Weka 3.7.9 将随机森林模型保存到一个文件中,现在我正在尝试针对其他(非常大的)集合(在 Amazon EC2 中的一些大型机器上)对其进行评估。我正在使用以下命令行: > jav
假设 X是原始的、标记的(即带有训练标签的)数据集,并且 Process(X)返回一组 Y实例 已用属性编码并转换为像 Y.arff 这样的对 Weka 友好的文件。 还假设Process()有一些“
我正在使用 Weka 中的数据集进行包含缺失值的分类。据我了解,当使用像 NaiveBayes 这样的分类器时,Weka 会自动用训练数据的众数或均值(使用过滤器 unsupervised/attri
我已经为我想在 Weka 中使用的数据集创建了一个 arff 文件。该文件被格式化为稀疏 arff 文件。无论如何,我已经成功加载了数据。然后我切换到关联选项卡并设置我的参数。但是,“开始”按钮不会启
我有一个 csv 文件,其中每一行都是代表数据点的数字向量。我想从命令行使用 weka 来计算 csv 文件中每个数据点的最近邻。我知道如何从命令行进行 k 最近邻分类,但这不是我想要的。我想要真正的
我有一个关于在 WEKA 中过滤属性的简单问题。 假设我有 30 个类的 500 个属性和每个类的 100 个样本,这等于 3000 行和 500 列。这会导致时间和内存问题,您可以猜到。 如何过滤在
Weka 中的分类器(例如决策树)将如何解释“?” (表示 ARFF 文件中的缺失值)在学习阶段?Weka 会用一些预定义的值(例如“0”或“false”)替换它,还是会以某种方式影响训练过程? 最佳
我正在尝试在 Weka 中使用 SVM 分类器。我下载了weka-3-7-13版本。当我单击分类器选项卡时,SVM 不在列表中。 如何在这个工具中使用 SVM?请帮助我克服这个问题。 最佳答案 在 W
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在从命令行运行 WEKA 以创建用于训练的贝叶斯网络模型,然后使用该模型在单独的数据集上进行测试。贝叶斯网络使用带有简单估计器的 TAN 搜索选项。我的培训命令行调用如下所示: java -cp
请帮助解释 Weka 库中由 weka.classifiers.functions.Logistic 生成的逻辑回归结果。 我使用来自 Weka 示例的数字数据: @relation weather
对于 Weka 中可用的 10 折交叉验证和传统的 10 折交叉验证之间的区别,我有点困惑。我理解 K 折交叉验证的概念,但是从我读到的 10 -Weka 中的折叠交叉验证有点不同。 在 Weka F
我正在使用 Weka 3.7.1 我正在尝试使用 weka 分析棒球运动预测。我想使用成本矩阵,因为在我赌博的体育博彩中,不同结果的成本是不一样的。我的数据集很简单:它是一组具有标称类 {WIN,LO
我正在使用 Weka GUI 在在线帖子上运行 NaiveBayes 分类器。我正在尝试跟踪错误预测的实例(在线帖子),以便我可以进一步了解如何改进功能。 目前,我有一个解决方法:我生成包含唯一 ID
我是一名优秀的程序员,十分优秀!