如何计算 DecisionTreeClassifier 的 0-1 置信度得分?

数据集0-9 列:浮点特征(产品参数)10 列:整数标签(产品)

目标

  1. 计算标签的 0-1 分类置信度得分(这是我当前代码应该做的)

  2. 为每行(22,000 行)的每个“产品名称”(300 列)计算相同的置信度得分

错误 我使用 sklearn.tree.DecisionTreeClassifier。我尝试使用 “predict_proba” 但出现了错误。

Python 代码

data_train = pd.read_csv('data.csv')features = data_train.columns[:-1]labels = data_train.columns[-1]x_features = data_train[features]x_label = data_train[labels]X_train, X_test, y_train, y_test = train_test_split(x_features, x_label, random_state=0)scaler = MinMaxScaler()X_train = scaler.fit_transform(X_train)X_test = scaler.transform(X_test)clf = DecisionTreeClassifier(max_depth=3).fit(X_train, y_train)class_probabilitiesDec = clf.predict_proba(y_train) #ERORR: ValueError: Number of features of the model must match the input. Model n_features is 10 and input n_features is 16722 print('Decision Tree Classification Accuracy Training Score (max_depth=3): {:.2f}'.format(clf.score(X_train, y_train)*100) + ('%'))print('Decision Tree Classification Accuracy Test Score (max_depth=3): {:.2f}'.format(clf.score(X_test, y_test)*100) + ('%'))print(class_probabilitiesDec[:10])# if I use X_tranin than it jsut prints out a buch of 41 element vectors: [[ 0.00490808  0.00765327  0.01123035  0.00332751  0.00665502  0.00357707   0.05182597  0.03169453  0.04267532  0.02761833  0.01988187  0.01281091   0.02936528  0.03934781  0.02329257  0.02961484  0.0353548   0.02503951   0.03577073  0.04700108  0.07661592  0.04433907  0.03019715  0.02196157   0.0108976   0.0074869   0.0291989   0.03951418  0.01372598  0.0176358   0.02345895  0.0169703   0.02487314  0.01813493  0.0482489   0.01988187   0.03252641  0.01572249  0.01455786  0.00457533  0.00083188] [....

特征(列)

(最后一列是标签)0 1 1 1 1.0 1462293561 1462293561 0 0 0.0 0.0 11 2 2 2 8.0 1460211580 1461091152 1 1 0.0 0.0 22 3 3 3 1.0 1469869039 1470560880 1 1 0.0 0.0 33 4 4 4 1.0 1461482675 1461482675 0 0 0.0 0.0 44 5 5 5 5.0 1462173043 1462386863 1 1 0.0 0.0 5

类别列(300 个物品列)

头行:苹果 游戏机 电池 ….第一行的得分:0.763 0.346 0.345 ….第二行的得分:0.256 0.732 0.935 ….

例如:当有人进行猫与狗的图像分类时,分类会给出置信度得分,类似于这样的得分使用情况。


回答:

你不能预测你的标签的概率。

predict_proba 预测你的 X 数据中每个标签的概率,因此:

class_probabilitiesDec = clf.predict_proba(X_test) 

你发布的“当我使用 X_train 时”内容:

[[ 0.00490808  0.00765327  0.01123035  0.00332751  0.00665502  0.00357707   0.05182597  0.03169453  0.04267532  0.02761833  0.01988187  0.01281091   0.02936528  0.03934781  0.02329257  0.02961484  0.0353548   0.02503951   0.03577073  0.04700108  0.07661592  0.04433907  0.03019715  0.02196157   0.0108976   0.0074869   0.0291989   0.03951418  0.01372598  0.0176358   0.02345895  0.0169703   0.02487314  0.01813493  0.0482489   0.01988187   0.03252641  0.01572249  0.01455786  0.00457533  0.00083188]

是一组每个可能标签为真的概率列表。

编辑

在阅读了你的评论后,predict_proba 正是你想要的。

让我们举个例子。在以下代码中,我们有一个具有 3 个类的分类器:要么是 11、12 或 13。

如果输入是 1,分类器应该预测 11

如果输入是 2,分类器应该预测 12

如果输入是 7,分类器应该预测 13

clf = DecisionTreeClassifier()clf.fit([[1],[2],[3],[4],[5],[6],[7]], [[11],[12],[13],[13],[12],[11],[13]])

现在,如果你有测试数据只有一行,例如 5,那么分类器应该预测 12。让我们试试看。

clf.predict([[5]])

瞧,结果是 array([12])

如果我们想要概率,那么 predict_proba 是解决方法:

clf.predict_proba([[5]])

我们得到 [array([0., 1., 0.])]

在这种情况下,数组 [0., 1., 0.] 意味着:

类别 11 的概率为 0%

类别 12 的概率为 100%

类别 13 的概率为 0%

如果我没理解错的话,这正是你想要的。你甚至可以将这些概率映射到你的类别名称上:

probabilities = clf.predict_proba([[5]])[0]{clf.classes_[i] : probabilities[i] for i in range(len(probabilities))}

这会给你一个包含类别名称及其概率的字典:

{11: 0.0, 12: 1.0, 13: 0.0}

现在,在你的案例中,你的类别远多于仅 [11,12,13],所以数组会更长。对于数据集中的每一行,predict_proba 都会创建一个数组,因此对于超过一行的数据,你的输出会变成一个矩阵。

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

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