machine-learning - 如何在 sklearn 中使用 SelectFromModel 查找类的积极信息特征

标签 machine-learning scikit-learn feature-selection

我想我明白,直到最近人们还使用属性 coef_ 到 extract the most informative features来自 python 机器学习库 sklearn 中的线性模型。现在,用户会被指向 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()])

最佳答案

据我所知,您需要坚持使用 .coef_ 方法并查看哪些系数是负值或正值。负系数明显降低了该类别发生的几率(因此负关系),而正系数则增加了该类别发生的几率(因此正关系)。

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

关于machine-learning - 如何在 sklearn 中使用 SelectFromModel 查找类的积极信息特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37961163/

相关文章:

python - 如何将json数据定义为X和Y sklearn决策树数组

python - 所有中间步骤都应该是变压器并实现拟合和变换

machine-learning - scikit learn 的plot_learning_curve 得分是多少?

python - Tensorflow 没有预测足够准确的结果

python - eli5 show_prediction 仅显示特征权重而不显示示例预测

python - 使用互信息进行特征图之间的特征选择(python)

python - 如何在 python 中从头开始选择用于逻辑回归的特征?

python - scikit-learn 中的目标转换和特征选择

python - 如何绘制多元线性回归的最佳拟合平面?

python - OSX 中的 Sklearn 安装