【问题标题】:Features used in sci-kit learn implementation of DTsci-kit 中使用的特性学习 DT 的实现
【发布时间】:2013-10-13 03:30:05
【问题描述】:

我在 sci-kit learn 中实现了一个带有 CV 的 DT 分类器。 但是,我还想输出有助于分类的特征数量。这是我到目前为止的代码:

from collections import defaultdict

import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.tree import DecisionTreeClassifier

from scipy.sparse import csr_matrix

lemma2feat = defaultdict(lambda: defaultdict(float))  # { lemma: {feat : weight}}
lemma2cat = dict()
features = set()


with open("input.csv","rb") as infile:
    for line in infile:
        lemma, feature, weight, tClass = line.split()
        lemma2feat[lemma][feature] = float(weight)
        lemma2cat[lemma] = int(tClass)
        features.add(feature)

sorted_rows = sorted(lemma2feat.keys())
col2index = dict()
for colIdx, col in enumerate(sorted(list(features))):
    col2index[col] = colIdx

dMat = np.zeros((len(sorted_rows), len(col2index.keys())), dtype = float)


# popola la matrice
for vIdx, vector in enumerate(sorted_rows):
    for feature in lemma2feat[vector].keys():
        dMat[vIdx][col2index[feature]] = lemma2feat[vector][feature]

res = []
for lem in sorted_rows:
    res.append(lemma2cat[lem])


clf = DecisionTreeClassifier(random_state=0)


print "Acc:"
print cross_val_score(clf, dMat, np.asarray(res), cv=10, scoring = "accuracy")

我可以包含什么来输出特征数量,例如,我查看了 RFE,因为我在另一个问题中询问过,但它不能轻易包含在 DT 中。因此,我想知道是否有办法修改我的上述代码以输出有助于最高精度的特征数量。此处的总体目标是将其绘制成肘形图,并与其他分类器的输出进行比较。 谢谢。

【问题讨论】:

    标签: python scikit-learn decision-tree feature-selection


    【解决方案1】:

    一旦您的树适合,您可以使用feature_importances_ 属性检查相关功能。它会给你一个 n_features 浮点值数组,如果第 i 个特征对构建树很重要/有助于构建树,feature_importances_[i] 将很高(相对于其他值),如果它不是。

    【讨论】:

      猜你喜欢
      • 2013-05-23
      • 2014-07-16
      • 1970-01-01
      • 2020-10-15
      • 2020-07-17
      • 2014-08-14
      • 2019-11-28
      • 2017-02-05
      • 2015-02-14
      相关资源
      最近更新 更多