【问题标题】:Why does a column of 1s impact the results of a decision tree classifier?为什么一列 1 会影响决策树分类器的结果?
【发布时间】:2020-11-12 11:54:26
【问题描述】:

我在一个随机生成的分类问题上测试 sklearn 的 Pipeline

import numpy as np
import pandas as pd

from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score

x, y = make_classification(n_samples=100, n_features=5, random_state=10)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.2, random_state=0)

model = DecisionTreeClassifier(random_state=0)
pipe = Pipeline(steps=[('scale', StandardScaler()),
                       ('poly', PolynomialFeatures(degree=2, include_bias=False)),
                       ('model', model)])
pipe.fit(x_train, y_train)

pipe_pred = pipe.predict(x_test)
accuracy_score(y_test, pipe_pred)

这导致准确度得分为.85。但是,当我将PolynomialFeatures 参数include_bias 更改为True 时,它只是将一列1 插入到数组中,准确度分数变为.90。为了可视化,下面我绘制了当偏差为TrueFalse 时的结果的单个树:

include_bias=True: True

include_bias=False: False

这些图片由plot_tree(pipe['model'])生成。

数据集是相同的,除了include_bias=True 在第 0 列中插入了额外的 1 列。因此,include_bias=True 数据的列索引对应于include_bias=False 数据中的i + 1 列索引。 (例如with_bias[:, 5] == without_bias[:, 4]

根据我的理解,1 列不应该对决策树产生影响。我错过了什么?

【问题讨论】:

    标签: python machine-learning scikit-learn decision-tree


    【解决方案1】:

    来自documentation for DecisionTreeClassifier

    random_state : int,RandomState 实例,默认=None
    控制估计器的随机性。即使splitter 设置为"best",这些特征总是在每次拆分时随机排列。当max_features < n_features 时,算法将在每次拆分时随机选择 max_features,然后再找到其中的最佳拆分。但是即使max_features=n_features,在不同的运行中找到的最佳拆分可能也会有所不同。就是这样,如果标准的改进对于几个拆分是相同的,并且必须随机选择一个拆分。为了在拟合期间获得确定性行为,random_state 必须固定为整数。有关详细信息,请参阅词汇表。

    您已经设置了random_state,但是具有不同数量的列仍然会使这些随机随机播放不同。请注意,gini 的值对于每个节点上的两棵树都是相同的,即使不同的特征正在分裂。

    【讨论】:

    • 只是为了让我理解 sklearn DecisionTreeClassifier 实现的大图景中的random_state,假设max_features 设置为None,它使用所有功能来创建拆分。鉴于基尼系数相同但用于拆分的特征不同,我们在使用完全相同的特征但不同 random_states 的决策树之间得到不同结果的原因是平局,因为如果出现平局,DecisionTreeClassifier 选择随机一个?我理解正确吗?
    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2016-05-31
    • 2023-03-30
    • 2018-10-20
    • 2019-05-27
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多