python - 如何在 countvectorizer 中使用 max_features 参数对某些特征进行优先级排序
问题描述
我有一个工作程序,但我意识到测试数据中的一些重要 n-gram 不是我在训练数据中允许的 6500 个 max_features 的一部分。是否可以添加诸如“骗子”或“可怜”之类的特征作为我将使用我的训练数据进行训练的特征?
这是我目前用于制作矢量化器的内容:
vectorizer = CountVectorizer(ngram_range=(1, 2)
,max_features=6500)
X = vectorizer.fit_transform(train['text'])
feature_names = vectorizer.get_feature_names()
解决方案
这是 hacky,你可能不能指望它在未来工作,但CountVectorizer
主要依赖于学习属性vocabulary_
,这是一个以标记作为键和“特征索引”作为值的字典。您可以添加到该字典中,一切似乎都按预期工作;借用文档中的示例:
from sklearn.feature_extraction.text import CountVectorizer
corpus = [
'This is the first document.',
'This document is the second document.',
'And this is the third one.',
'Is this the first document?',
]
vectorizer2 = CountVectorizer(analyzer='word', ngram_range=(2, 2))
X2 = vectorizer2.fit_transform(corpus)
print(X2.toarray())
## Output:
# [[0 0 1 1 0 0 1 0 0 0 0 1 0]
# [0 1 0 1 0 1 0 1 0 0 1 0 0]
# [1 0 0 1 0 0 0 0 1 1 0 1 0]
# [0 0 1 0 1 0 1 0 0 0 0 0 1]]
# Now we tweak:
vocab_len = len(vectorizer2.vocabulary_)
vectorizer2.vocabulary_['new token'] = vocab_len # append to end
print(vectorizer2.transform(["And this document has a new token"]).toarray())
## Output
# [[1 0 0 0 0 0 0 0 0 0 1 0 0 1]]
推荐阅读
- php - 使用 PHP 对选定的复选框值求和
- javascript - 传单/渐进式 Web 应用程序 - 在 404 块上快速周转(可能使用 Workbox)?
- c++ - c ++内联ASM,'asm'操作数具有不可能的约束
- php - 物化模态不工作
- azure-devops - 将自定义子项添加到功能卡
- mysql - SQL + goLang - 运行带有 IN 条件的 select 语句 - 并传递一个 id 数组?
- nuget - 将 NuGet 包从一个 VSTS 帐户迁移到另一个
- javascript - 使用 .keydown() 返回 div id?
- sql - 如何使用日期字段和 SQL 在文本中说出多少年前
- python - Python 模式的 linter 不适用于 4 个 vim 安装