如何在sklearn中使用SelectFromModel来查找类的正面信息功能

时间:2016-06-22 07:23:04

标签: machine-learning scikit-learn feature-selection

我想我明白,直到最近人们才在python的机器学习库sklearn中使用线性模型中的属性coef_到extract the most informative features。现在,用户可以转而使用SelectFromModel。 SelectFromModel允许根据阈值减少功能。因此,类似下面的代码将功能简化为那些具有重要性的特征> 0.5。我现在的问题是:有没有办法确定某个功能是否对某个类别有积极的或消极的区分?

我的数据在一个名为data的pandas数据框中,第一列是文本文件的文件名列表,第二列是标签。

count_vect = CountVectorizer(input="filename", analyzer="word")
X_train_counts = count_vect.fit_transform(data["filenames"])
print(X_train_counts.shape)
tf_transformer = TfidfTransformer(use_idf=True)
traindata = tf_transformer.fit_transform(X_train_counts)
print(traindata.shape) #report size of the training data
clf = LogisticRegression()
model = SelectFromModel(clf, threshold=0.5)
X_transform = model.fit_transform(traindata, data["labels"])
print("reduced features: ", X_transform.shape)
#get the names of all features
words = np.array(count_vect.get_feature_names())
#get the names of the important features using the boolean index from model 
print(words[model.get_support()])

1 个答案:

答案 0 :(得分:2)

据我所知,你需要回到.coef_方法,看看哪些系数是负数还是正数。负系数明显降低了该类发生的几率(如此负面关系),而正系数则增加了该类发生的几率(所以正面关系)。

然而,这种方法不会给你意义,只有方向。您将需要SelectFromModel方法来提取它。