【问题标题】:How to get "Word" Importance in NLP (TFIDF + Logistic Regression)如何在 NLP 中获得“单词”重要性(TFIDF + Logistic 回归)
【发布时间】:2020-01-29 14:20:27
【问题描述】:

我有一个函数可以像这样获得 tfidf 功能:

def get_tfidf_features(data, tfidf_vectorizer=None, ngram_range=(1,2)):
    """ Creates tfidf features and returns them as sparse matrix. If no tfidf_vectorizer is given, 
    the function will train one."""

    if tfidf_vectorizer is not None:
        tfidf = tfidf_vectorizer.transform(data.Comment_text)
    else:
        # only add words to the vocabulary that appear at least 200 times
        tfidf_vectorizer = TfidfVectorizer(min_df=700, ngram_range=ngram_range, stop_words='english')
        tfidf = tfidf_vectorizer.fit_transform(data.Comment_text)        

    tfidf = pd.SparseDataFrame(tfidf.toarray()).to_sparse()
    tfidf.applymap(lambda x: round(x, 4))
    tfidf_features = ['tfidf_' + word for word in tfidf_vectorizer.get_feature_names()]
    tfidf.columns = tfidf_features
    data = data.reset_index().join(tfidf).set_index('index')

    return data, tfidf_vectorizer, tfidf_features    

X_train, tfidf_vectorizer, tfidf_features = get_tfidf_features(X_train)

我应用了这样一个简单的逻辑回归:

logit = LogisticRegression(random_state=0, solver='lbfgs', multi_class='ovr')
logit.fit(X_train.loc[:, features].fillna(0), X_train['Hateful_or_not'])
preds = logit.predict(X_test.loc[:, features].fillna(0))

我得到这样的功能重要性:

 logit.coef_

但这给了我“列”而不是单词的特征重要性

【问题讨论】:

  • 你是想找出一个词或一个句子的可恨程度吗?

标签: python nlp nltk tf-idf


【解决方案1】:

logit.coef_ 确实为您提供了每个单词特征(或二元组)的系数。它将返回一个包含len(features) 元素的数组,其中features 中第i 个位置的单词的系数将位于logit.coef_ 数组的第i 个位置。

例子:

features = ['love','hate','positive','negative']
logit.coef_ = [0.9, -1.2, 0.5, -0.75]

“爱”的系数是0.9,“恨”是-1.2等等……

【讨论】:

  • 哦,谢谢。能否也了解一下,哪些词帮助模型预测“有毒”词,哪些词帮助模型预测“无毒词”
猜你喜欢
  • 1970-01-01
  • 2018-12-29
  • 2016-09-18
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 2015-01-06
  • 2021-02-24
相关资源
最近更新 更多