Python SKI中的TfidfVectorizer了解如何专门包含单词

Python SKI中的TfidfVectorizer了解如何专门包含单词,python,machine-learning,nlp,scikit-learn,Python,Machine Learning,Nlp,Scikit Learn,我对TFIDFvectorier有一些问题 我不清楚这些词是如何选择的。我们可以提供最低限度的支持,但在这之后,什么将决定选择哪些功能(例如,更高的支持更大的机会)?如果我们说max_features=10000,我们总是得到相同的结果吗?如果我们说max\u features=12000,我们会得到相同的10000功能,但会额外增加2000 还有,有没有一种方法可以扩展,比如说,max\u features=20000功能?我把它放在一些文本上,但我知道一些肯定应该包括的单词,还有一些表情符号

我对
TFIDFvectorier
有一些问题

我不清楚这些词是如何选择的。我们可以提供最低限度的支持,但在这之后,什么将决定选择哪些功能(例如,更高的支持更大的机会)?如果我们说max_features=10000,我们总是得到相同的结果吗?如果我们说
max\u features=12000
,我们会得到相同的
10000
功能,但会额外增加
2000

还有,有没有一种方法可以扩展,比如说,
max\u features=20000
功能?我把它放在一些文本上,但我知道一些肯定应该包括的单词,还有一些表情符号“-”等等。如何将这些添加到
TfidfVectorizer
对象中,以便可以使用该对象,使用它来
fit
predict

to_include = [":-)", ":-P"]
method = TfidfVectorizer(max_features=20000, ngram_range=(1, 3),
                      # I know stopwords, but how about include words?
                      stop_words=test.stoplist[:100], 
                      # include words ??
                      analyzer='word',
                      min_df=5)
method.fit(traindata)
寻求的结果:
X=method.transform(列车数据)
X
], 
其中N是样本量

您要问几个独立的问题。让我分别回答:

“我不清楚这些词是如何选择的。”

从:

在整个语料库中,所有的特征(在你的例子中是单格、双格和三叉)都是按频率排序的,然后选择顶部的
10000
。不寻常的词被扔掉了

“如果我们说max\u features=10000,我们总是得到相同的功能吗?如果我们说max\u features=12000,我们会得到相同的10000个功能,但会额外增加2000个功能吗?”

对。这个过程是确定的:对于给定的语料库和给定的
max\u features
,您将始终获得相同的特征

我把它放在一些文本中,但我知道一些应该包含的单词,[…]如何将它们添加到TfidfVectorizer对象中?

您可以使用
词汇表
参数指定应该使用哪些功能。例如,如果只希望提取表情符号,可以执行以下操作:

emoticons = {":)":0, ":P":1, ":(":2}
vect = TfidfVectorizer(vocabulary=emoticons)
matrix = vect.fit_transform(traindata)
这将返回一个
]
。请注意,只有3列,每个要素对应一列

如果您希望词汇表包括表情符号以及
N
最常见的特征
,您可以先计算最常见的特征,然后将它们与表情符号合并并重新矢量化,如下所示:

# calculate the most frequent features first
vect = TfidfVectorizer(vocabulary=emoticons, max_features=10)
matrix = vect.fit_transform(traindata)
top_features = vect.vocabulary_
n = len(top_features)

# insert the emoticons into the vocabulary of common features
emoticons = {":)":0, ":P":1, ":(":2)}
for feature, index in emoticons.items():
    top_features[feature] = n + index

# re-vectorize using both sets of features
# at this point len(top_features) == 13
vect = TfidfVectorizer(vocabulary=top_features)
matrix = vect.fit_transform(traindata)

谢谢你完整的回答!虽然这并不考虑“代码>:-”“< /代码>”不是一个词的事实吗?据我所知,这些将被过滤掉(0个存储元素)。与我所怀疑的相反,
分析器没有查看任何与“令牌(ize)”相关的内容,如果有人感兴趣,它需要一个不同的功能。
emoticons = {":)":0, ":P":1, ":(":2}
vect = TfidfVectorizer(vocabulary=emoticons)
matrix = vect.fit_transform(traindata)
# calculate the most frequent features first
vect = TfidfVectorizer(vocabulary=emoticons, max_features=10)
matrix = vect.fit_transform(traindata)
top_features = vect.vocabulary_
n = len(top_features)

# insert the emoticons into the vocabulary of common features
emoticons = {":)":0, ":P":1, ":(":2)}
for feature, index in emoticons.items():
    top_features[feature] = n + index

# re-vectorize using both sets of features
# at this point len(top_features) == 13
vect = TfidfVectorizer(vocabulary=top_features)
matrix = vect.fit_transform(traindata)