首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本特征的特征重要性

文本特征的特征重要性
EN

Data Science用户
提问于 2021-05-13 00:10:19
回答 1查看 1.1K关注 0票数 2

我想在几种模型中确定特性的重要性:

  • 支持向量机
  • logistic回归
  • 朴素贝叶斯
  • 随机林

我读到需要一个不可知论的模型,所以我考虑使用performance_importance (在python中)。我的容貌就像

  • 文字(例如,钢笔在桌子上,天空是蓝色的,.)
  • 年份(例如2019年、2020年、.)
  • #_of_characters (例如,34、67、.):这个值来自文本
  • 政党(例如,国家、地方、绿色、.)
  • Over18 (例如,1,0,.):这是一个布尔变量

我的目标变量是Voted

在预处理阶段,我使用BoW和TF作为文本,OneHotEncoder作为缔约方,SimpleImputer作为数字。使用以下方法:

代码语言:javascript
复制
from sklearn.inspection import permutation_importance
import matplotlib.pyplot as plt

result = permutation_importance(clf, X_test, y_test, n_repeats=5, random_state=42, n_jobs=2)
sorted_idx = result.importances_mean.argsort()

plt.boxplot(result.importances[sorted_idx].T,
            vert=False, labels=X.columns[sorted_idx]);

我得到了类似的输出,如下所示(我忘了包含Over18,但它只是给出了输出的一个概念):

虽然我在解释结果时有困难,特别是圆圈和负值,但我想了解,在文本分类的情况下,是否应该使用Text来表示重要性,而不是,例如,单个单词(单字、比格、.)。例如,在我的例子中,我有['The','pen','is','on','table','sky','blue']。理解每个词对模型的贡献有多大而不是文本是否更有意义,或者这只是在文本中考虑的(在文本中有许多词对模型有贡献),这是模型中最重要的特性?

更新:对于不同的特性,我使用以下预处理程序:

代码语言:javascript
复制
categorical_preprocessing = OneHotEncoder(handle_unknown='ignore')

numeric_preprocessing = Pipeline([
    ('imputer', SimpleImputer(strategy='mean')
])

# CountVectorizer
text_preprocessing_cv =  Pipeline(steps=[
    ('CV',CountVectorizer())
]) 

# TF-IDF
text_preprocessing_tfidf = Pipeline(steps=[
    ('TF-IDF',TfidfVectorizer())       
]) 

然后

代码语言:javascript
复制
preprocessing_cv = ColumnTransformer(
    transformers=[
        ('text',text_preprocessing_cv, 'Text'),
        ('category', categorical_preprocessing, categorical_features),
        ('numeric', numeric_preprocessing, numerical_features)
], remainder='passthrough')

clf_nb = Pipeline(steps=[('preprocessor', preprocessing_cv),
                      ('classifier', MultinomialNB())])
EN

回答 1

Data Science用户

回答已采纳

发布于 2021-05-16 15:12:47

permutation_importance正在考虑顶级功能。它是依次排列每一个,并学习的重要性。

因此,内部编码即OHE/tfid是不可见的。

要了解顶层特性的组件的重要性,您应该分别对其进行编码,然后将编码的数据传递给permutation_importance

  • 使用preprocessing_cv.fit_transform(X_train)获取预处理数据。
  • 对上述数据和您选择的任何模型调用您的permutation_importance代码

编辑

添加片段。我把ColumnTranformer排除在外,因为它引起了一些问题。

代码语言:javascript
复制
data = {'Number':[1,2,3], 'Text':['pen is table', 'sky is blue','Sun is kool'], 'Cat':['A','B', 'C']}
df = pd.DataFrame(data)

categorical_preprocessing = OneHotEncoder(handle_unknown='ignore')
numeric_preprocessing = SimpleImputer(strategy='mean')
text_preprocessing_cv =  CountVectorizer()

text_tfid = text_preprocessing_cv.fit_transform(df['Text']).toarray()
num = numeric_preprocessing.fit_transform(df['Number'].values.reshape(-1, 1))
cat = categorical_preprocessing.fit_transform(df['Cat'].values.reshape(-1, 1)).toarray()

data = np.concatenate((cat,num,text_tfid), axis=1)
cols =  np.concatenate((categorical_preprocessing.get_feature_names(), text_preprocessing_cv.get_feature_names(), ['Num'])) # New cols name

df = pd.DataFrame(data, columns=cols) # Encoded DataFrame with col name
代码语言:javascript
复制
from sklearn.inspection import permutation_importance
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier().fit(df, [1,0,1])

result = permutation_importance(clf, df, [1,0,1], n_repeats=2, random_state=42)
sorted_idx = result.importances_mean.argsort()

plt.boxplot(result.importances[sorted_idx].T,
            vert=False, labels=df.columns[sorted_idx]);
票数 2
EN
页面原文内容由Data Science提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://datascience.stackexchange.com/questions/94377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档