【发布时间】: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_
但这给了我“列”而不是单词的特征重要性
【问题讨论】:
-
你是想找出一个词或一个句子的可恨程度吗?