【问题标题】:get Classification accuracy on test data using previous saved model使用以前保存的模型获得测试数据的分类精度
【发布时间】:2017-08-10 08:18:39
【问题描述】:

我正在使用 Orange 数据挖掘工具编写 Python 脚本,以使用以前保存的模型(pickle 文件)对测试数据进行分类准确度。

dataFile = "training.csv" 
data = Orange.data.Table(dataFile);
learner = Orange.classification.RandomForestLearner()
cf = learner(data)
#save the pickle file
with open("1.pkcls", "wb") as f:
    pickle.dump(cf, f)

#load the pickle file
with open("1.pkcls", "rb") as f:
    loadCF = pickle.load(f)
testFile = "testing.csv" 
test = Orange.data.Table(testFile);

learners = [1]
learners[0] = cf
result = Orange.evaluation.testing.TestOnTestData(data,test,learners)
# get classification accuracy
CAs = 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 file
with open("1.pkcls", "rb") as f:
loadCF = pickle.load(f)

有谁知道是否可以先训练模型并将其保存为 pickle 文件。那我以后可以用它来测试另一个文件以获得分类准确性吗?

【问题讨论】:

  • 您确定测试和训练数据标签相同吗?两个 X 值看起来也一样吗?看起来模型因为两组之间的数据不兼容而抱怨
  • @omu_negru 感谢您的回复。我实际上使用了与训练和测试相同的文件。这两个文件的内容完全相同,只是文件名不同。
  • 你能展示datatest的真实类吗?
  • @VivekKumar 感谢您的回复。真正的类是指目标/标签吗?
  • 是的。从datatest 发布标签

标签: python machine-learning scikit-learn orange


【解决方案1】:

您不能在将分类器传递给TestOnTestData 之前对其进行预训练(其名称应为TrainOnTrainAndTestOnTestData,即它自己调用拟合/训练步骤)。

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

一种快速而肮脏的方法是将“学习者”传递给 TestOnTest 数据以返回预训练模型

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

【讨论】:

    猜你喜欢
    • 2020-09-11
    • 2019-10-22
    • 2021-06-08
    • 2020-07-24
    • 2017-02-26
    • 2019-02-10
    • 2017-01-19
    • 2021-05-28
    • 2018-05-13
    相关资源
    最近更新 更多