如何为DecisionTreeClassifier计算0-1确定性得分?

时间:2019-05-29 11:18:41

标签: python machine-learning scikit-learn classification data-science

数据集 0-9列:浮动功能(产品的参数) 第10栏:int标签(产品)

目标

  1. 计算标签的0-1分类确定性得分(这是我当前的代码应该执行的操作)

  2. 在每行(22'000)处为每个“ product_name”(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 1 1 2 2 2 8.0 1460211580 1461091152 1 1 0.0 0.0 2 2 3 3 3 1.0 1469869039 1470560880 1 1 0.0 0.0 3 3 4 4 4 1.0 1461482675 1461482675 0 0 0.0 0.0 4 4 5 5 5 5.0 1462173043 1462386863 1 1 0.0 0.0 5

类列(300个项目列)

头行:苹果Gameboy电池.... 第一排得分:0.763 0.346 0.345 .... 第二行得分:0.256 0.732 0.935 ....

例如,某人对猫VS进行图像分类时使用的得分相似。狗和分类给出了置信度得分。

1 个答案:

答案 0 :(得分:2)

您无法预测标签的概率。

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]

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

编辑

在阅读您的评论后,可以预测proba正是您想要的。

举个例子。在下面的代码中,我们有一个包含3个类的分类器:11、12或13。

如果输入为1,则分类器应预测11

如果输入为2,则分类器应预测12

...

如果输入为7,则分类器应预测12

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])

如果我们想要一个概率,那么预测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都会创建一个数组,因此对于多于一行的数据,您的输出将成为一个矩阵。