我目前正在开发一个疾病预测的机器学习模型。我在模型中使用了随机森林分类器,现在我想获取预测值的概率,但代码出现了错误。在这个程序中,我希望能具体获取“每个”预测的概率。例如,我输入症状来预测疾病,结果预测的疾病是“过敏”。然后,我希望我的程序显示预测疾病“过敏”的概率为百分比,但程序却出现了诸如“分类指标无法处理多类和未知目标的混合”的错误。我猜想我需要使用混淆矩阵来显示概率,但它也出现了同样的关于多类问题的错误。更具体地说,我只是想以“百分比”的形式显示每个预测值的概率。例如,过敏疾病的概率是90%,等等。我该如何做到这一点,以及如何解决我的问题?
以下是相关的代码:
p=pickle_model.predict([[22,8,50,9,20,47,50,38,0,0,0]])
actual=np.array((22,8,50,9,20,47,50,38,0,0,0))
pred=pickle_model.predict_proba([[p,0,0,0,0,0,0,0,0,0,0]])
在下面的代码块中:
from sklearn.metrics import confusion_matriximport sklearn.metrics as mtfrom sklearn.metrics import accuracy_scorefrom sklearn.metrics import precision_scoreprint(accuracy_score(actual, p, normalize=True, sample_weight=None))
我得到了以下错误:
ValueError Traceback (most recent call last)<ipython-input-69-e8980bf68410> in <module> 3 from sklearn.metrics import accuracy_score 4 from sklearn.metrics import precision_score----> 5 print(accuracy_score(actual, p, normalize=True, sample_weight=None)) 6 #precision, recall, fscore, support = 7 #score(y_test, p)~\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 if extra_args <= 0:---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0~\anaconda3\lib\site-packages\sklearn\metrics\_classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight) 200 201 # Compute accuracy for each possible representation--> 202 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 203 check_consistent_length(y_true, y_pred, sample_weight) 204 if y_type.startswith('multilabel'):~\anaconda3\lib\site-packages\sklearn\metrics\_classification.py in _check_targets(y_true, y_pred) 81 y_pred : array or indicator matrix 82 """---> 83 check_consistent_length(y_true, y_pred) 84 type_true = type_of_target(y_true) 85 type_pred = type_of_target(y_pred)~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays) 260 uniques = np.unique(lengths) 261 if len(uniques) > 1:--> 262 raise ValueError("Found input variables with inconsistent numbers of" 263 " samples: %r" % [int(l) for l in lengths]) 264 ValueError: Found input variables with inconsistent numbers of samples: [11, 1]
此外,在这个代码块中我还得到了另一个错误:
ValueError Traceback (most recent call last)<ipython-input-65-774dbd6b46f7> in <module> 8 9 # specificity---> 10 tn, fp, fn, tp = mt.confusion_matrix(actual, predict).ravel() 11 specificity = tn / (tn+fp) 12 print(specificity)~\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 if extra_args <= 0:---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0~\anaconda3\lib\site-packages\sklearn\metrics\_classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight, normalize) 294 295 """--> 296 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 297 if y_type not in ("binary", "multiclass"): 298 raise ValueError("%s is not supported" % y_type)~\anaconda3\lib\site-packages\sklearn\metrics\_classification.py in _check_targets(y_true, y_pred) 90 91 if len(y_type) > 1:---> 92 raise ValueError("Classification metrics can't handle a mix of {0} " 93 "and {1} targets".format(type_true, type_pred)) 94 ValueError: Classification metrics can't handle a mix of multiclass and unknown targets
回答:
你要找的函数是 predict_proba()
。
这个函数的输入必须与你的模型训练时完全一致。它必须与函数 predict()
的输入相同。
如果你这部分代码没有报错(因为你没有报告任何错误):
p=pickle_model.predict([[22,8,50,9,20,47,50,38,0,0,0]])
那么这也应该能工作:
p=pickle_model.predict_proba([[22,8,50,9,20,47,50,38,0,0,0]])
注意: 注意“[]”,你可能需要删除一些。