我已经使用maxvoting(决策树,随机森林,逻辑回归)分类器建立了机器学习模型。对于我有输入为
{“薪水”:50000,“当前贷款”:15000,“信用分数”:616,“请求的贷款”:25000}
当我将此数据传递给模型时。它给出的预测为
{“状态”:已批准}
但是我需要检索响应,如
{“状态”:批准,“准确性”:0.87}
任何帮助将不胜感激
似乎您可能正在使用sklearn的VotingClassifier
。装配好分类器后,即可通过属性VotingClassifier
查看与每个类相关的概率。请注意,这实际上是每个类别的关联概率,而不是准确性。因此,如果您希望测试样本的类别为predict_proba
,则必须在相应列上为输出predict_proba
编制索引。这是使用sklearn的虹膜数据集的示例:
n
例如,我们可以获得与第一类相关的概率:
y_pred_prob
最后,要获得您所描述的输出,可以使用from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
clf1 = LogisticRegression(multi_class='multinomial', random_state=1)
clf2 = RandomForestClassifier(n_estimators=50, random_state=1)
clf3 = GaussianNB()
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
eclf2 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft')
eclf2 = eclf2.fit(X_train, y_train)
返回的结果来索引2D概率数组,如下所示:
eclf2.predict_proba(X_test)[:,0].round(2)
array([0.99, 0. , 0. , 0. , 0. , 0. , 0.01, 0.01, 0. , 0. , 0. ,
0.99, 0. , 0.99, 0.99, 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0.01, 0.98, 0. , 1. , 0.99, 0. , 0. , 0. , 0.99, 0.98,
0. , 0.99, 0. , 0.01, 0.99])
或者,如果您希望将输出作为字典:
predict