【问题标题】:How may I un-encode the features from a decision tree to see the important features?如何对决策树中的特征进行解编码以查看重要特征?
【发布时间】:2019-04-29 16:18:28
【问题描述】:

我有一个正在使用的数据集。我正在将它们从分类特征转换为我的决策树的数字特征。转换发生在整个数据帧上,其中包含以下几行:

le = LE()
df = df.apply(le.fit_transform)

我稍后将这些数据拆分为训练和测试数据,如下所示:

target = ['label']
df_y = df['label']
df_x = df.drop(target, axis=1)

# Split into training and testing data
train_x, test_x, train_y, test_y = tts(df_x, df_y, test_size=0.3, random_state=42)  

然后我将它传递给一个训练决策树的方法:

def Decision_Tree_Classifier(train_x, train_y, test_x, test_y, le):
    print " - Candidate: Decision Tree Classifier"
    dec_tree_classifier = DecisionTreeClassifier(random_state=0) # Load Module
    dec_tree_classifier.fit(train_x, train_y) # Fit
    accuracy = dec_tree_classifier.score(test_x, test_y) # Acc
    predicted = dec_tree_classifier.predict(test_x)
    mse = mean_squared_error(test_y, predicted)

    tree_feat = list(le.inverse_transform(dec_tree_classifier.tree_.feature))
    print "Tree Features:"
    print tree_feat
    print "Tree Thresholds:"
    print dec_tree_classifier.tree_.threshold

    scores = cross_val_score(dec_tree_classifier, test_x, test_y.values.ravel(), cv=10)
    return (accuracy, mse, scores.mean(), scores.std())

在上述方法中,我传递了原本用于对数据帧进行编码的 LabelEncoder 对象。我有这条线

tree_feat = list(le.inverse_transform(dec_tree_classifier.tree_.feature))

尝试将特征转换回其原始分类表示,但我不断收到此堆栈跟踪错误:

  File "<ipython-input-6-c2005f8661bc>", line 1, in <module>
    runfile('main.py', wdir='/Users/mydir)

  File "/Users/me/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/Users/me/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 100, in execfile
    builtins.execfile(filename, *where)

  File "/Users/me/mydir/main.py", line 125, in <module>
    main()  # Run main routine

  File "candidates.py", line 175, in get_baseline
    dec_tre_acc = Decision_Tree_Classifier(train_x, train_y, test_x, test_y, le)

  File "candidates.py", line 40, in Decision_Tree_Classifier
    tree_feat = list(le.inverse_transform(dec_tree_classifier.tree_.feature))

  File "/Users/me/anaconda2/lib/python2.7/site-packages/sklearn/preprocessing/label.py", line 281, in inverse_transform
    "y contains previously unseen labels: %s" % str(diff))

ValueError: y contains previously unseen labels: [-2]

我需要进行哪些更改才能查看实际功能本身?

【问题讨论】:

    标签: python scikit-learn decision-tree encoder


    【解决方案1】:

    当你这样做时:

    df = df.apply(le.fit_transform)
    

    您正在为所有列使用单个 LabelEncoder 实例。当调用fit()fit_transform() 时,le 将忘记以前的数据,只学习当前数据。因此,您拥有的le 仅存储有关它看到的最后一列的信息,而不是所有列。

    有多种方法可以解决这个问题:

    1. 您可以维护多个 LabelEncoder 对象(每列一个)。在此处查看这个出色的答案:

    2. 如果你想保留一个对象来处理所有列,如果你安装了最新版本的 scikit-learn,你可以使用OrdinalEncoder

      from sklearn.preprocessing import OrdinalEncoder
      enc = OrdinalEncoder()
      
      df = enc.fit_transform(df)
      

    但仍然无法解决错误,因为tree_.feature 不对应于特征的值,而是用于在该节点进行拆分的索引(df 中的列)。因此,如果数据中有 3 个特征(列)(与该列中的值无关),tree_.feature 可以有值:

    • 0、1、2、-2

    • -2 是一个特殊的占位符值,表示该节点是叶节点,因此不使用任何特征来分割任何东西。

    tree_.threshold 将包含与您的数据值对应的值。但这将是浮点数,因此您必须根据类别到数字的转换进行转换。

    查看这个例子详细了解树形结构:

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 2020-06-24
      • 2019-01-11
      • 2016-03-18
      • 2018-08-16
      • 2018-05-15
      • 2021-07-25
      • 2012-07-19
      • 2020-02-02
      相关资源
      最近更新 更多