【发布时间】:2017-07-20 09:17:55
【问题描述】:
我正在使用 python,我想获取大量数据的 TFIDF 表示,我正在使用以下代码将文档转换为它们的 TFIDF 形式。
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(
min_df=1, # min count for relevant vocabulary
max_features=4000, # maximum number of features
strip_accents='unicode', # replace all accented unicode char
# by their corresponding ASCII char
analyzer='word', # features made of words
token_pattern=r'\w{1,}', # tokenize only words of 4+ chars
ngram_range=(1, 1), # features made of a single tokens
use_idf=True, # enable inverse-document-frequency reweighting
smooth_idf=True, # prevents zero division for unseen words
sublinear_tf=False)
tfidf_df = tfidf_vectorizer.fit_transform(df['text'])
这里我传递了一个参数max_features。矢量化器将选择最佳特征并返回一个 scipy 稀疏矩阵。问题是我不知道选择了哪些功能以及如何将这些功能名称映射回我得到的 scipy 矩阵?基本上对于m 文档数量中的n 选定特征,我想要一个m x n 矩阵,其中选定特征作为列名而不是它们的整数ID。我该如何做到这一点?
【问题讨论】:
标签: python scikit-learn nlp