【问题标题】:Is there any way to decode decision tree probablity output?有没有办法解码决策树概率输出?
【发布时间】:2020-11-09 17:55:08
【问题描述】:

我的代码在 google colab 上可用

Code for indetail information

我制作了一种算法,使用决策树进行多概率预测

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X1, Y1
                                        , test_size=0.4,
                                        random_state=1)



model = tree.DecisionTreeClassifier(criterion='entropy')

# Train the model using the training sets and check score
model.fit(X_train,y_train)

score= model.score(X1, Y1)
print('Score:- \n',score)

multi2 = model.predict_proba(X_test)

我收到了

 [array([[1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.],
        [1.]]),
 array([[1., 0.],
        [1., 0.],
        [1., 0.],
        [0., 1.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [0., 1.],
        [1., 0.]])]

在尝试解码时遇到此问题

vnc = OneHotEncoder(handle_unknown='ignore')
vnc.fit(Y1)
vnc.inverse_transform(multi2)

ValueError: could not broadcast input array from shape (38,1) into shape (38)

我的输出应该是:

[[decode_value1,decode_value2,decode_value3,decode_value4]]

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn decision-tree


    【解决方案1】:

    predict_proba 输出中的第 i 行包含一个长度为 2 的数组,其中包含您传递的数组中第 i 个样本的正类和负类的概率(这意味着它们应该总和为 1)给它。

    默认情况下,DecisionTreeClassifier 会拆分,直到所有节点都是纯节点,即包含单个值。

    scikit-learn 文档指出:

    预测的类概率是叶子中同一类的样本的分数。

    这意味着如果决策树中的所有叶子都包含一个值(这是默认情况,即max_depth=None),predict_proba 将始终输出预测类别的概率 1。

    max_depth 设置为合理的正整数是有意义的,具体取决于数据集的大小。这将有助于调节过度拟合,尤其是在您有一个小数据集的情况下,并且您会观察到 predict_proba 输出的不仅仅是 0 和 1。

    参考: https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html

    【讨论】:

    • 它不能解决我的问题,我得到@Nikhil Kumar 的预测数组
    • 我已经调整了预测输出的大小并与测试数据相反,所以它对我有用。
    猜你喜欢
    • 2016-03-27
    • 2020-12-03
    • 2018-09-29
    • 2015-01-13
    • 2012-08-04
    • 1970-01-01
    • 2015-01-04
    • 2020-03-30
    • 2014-06-09
    相关资源
    最近更新 更多