使用先前保存的模型获取测试数据的分类准确率

我正在使用Orange数据挖掘工具编写一个Python脚本,使用先前保存的模型(pickle文件)来获取测试数据的分类准确率。

dataFile = "training.csv" data = Orange.data.Table(dataFile);learner = Orange.classification.RandomForestLearner()cf = learner(data)#save the pickle filewith open("1.pkcls", "wb") as f:    pickle.dump(cf, f)#load the pickle filewith open("1.pkcls", "rb") as f:    loadCF = pickle.load(f)testFile = "testing.csv" test = Orange.data.Table(testFile);learners = [1]learners[0] = cfresult = Orange.evaluation.testing.TestOnTestData(data,test,learners)# get classification accuracyCAs = Orange.evaluation.CA(result)

我能够成功保存和加载模型,但遇到了一个错误

    CAs = Orange.evaluation.CA(result)File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/Orange/evaluation/scoring.py", line 39, in __new__    return self(results, **kwargs)  File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/Orange/evaluation/scoring.py", line 48, in __call__    return self.compute_score(results, **kwargs)  File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/Orange/evaluation/scoring.py", line 84, in compute_score    return self.from_predicted(results, skl_metrics.accuracy_score)  File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/Orange/evaluation/scoring.py", line 75, in from_predicted    dtype=np.float64, count=len(results.predicted))  File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/Orange/evaluation/scoring.py", line 74, in <genexpr>    for predicted in results.predicted),  File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 172, in accuracy_score    y_type, y_true, y_pred = _check_targets(y_true, y_pred)  File "/Users/anaconda2/envs/py36/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 82, in _check_targets    "".format(type_true, type_pred))ValueError: Can't handle mix of multiclass and continuous

我找到了一种解决这个问题的方法,通过删除

cf = learner(data)

然而,如果我删除了这行代码,我将无法训练模型并保存它,因为RandomForestLearner在保存和加载模型的代码之前不会根据输入文件训练模型。

with open("1.pkcls", "wb") as f:pickle.dump(cf, f)#load the pickle filewith open("1.pkcls", "rb") as f:loadCF = pickle.load(f)

有人知道是否可以先训练一个模型并将其保存为pickle文件,然后再使用它来测试另一个文件以获取分类准确率吗?


回答:

在将分类器传递给TestOnTestData之前,您不能预先训练分类器(它的名字应该是TrainOnTrainAndTestOnTestData,即它会自行调用拟合/训练步骤)。

不幸的是,没有现成的明确方法可以从预训练分类器在测试数据集上的应用中创建Result实例。

一种快速且粗糙的方法是将传递给TestOnTest数据的’learners’转换为返回预训练模型的函数

results = Orange.evaluation.testing.TestOnTestData(data, test, [lambda testdata: loadCF])

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注