python - 如何阻止 Python Sci-kit 库的 Count Vectorizer 进行任何类型的单词过滤?

标签 python scikit-learn

Count Vectorizer 实现了对一些英文单词(如 'a')的默认基本过滤,如其文档 here 中所写。 。它只是忽略了我不想要的它们。有什么办法可以阻止它这样做吗?

>>> count_vectorizer = CountVectorizer()
>>> list = ['a for the']
>>> counts = count_vectorizer.fit_transform(list)
>>> print counts

输出为

(0, 0)  1
(0, 1)  1

这意味着它识别了'for''the'(已测试),但无法识别'a'。有什么方法可以识别每一个可能的单词吗?

最佳答案

来自文档: token_pattern:字符串 正则表达式表示“ token ”的构成,仅在分析器 == 'word' 时使用。默认正则表达式选择 2 个或更多字母数字字符的标记(标点符号完全被忽略并始终被视为标记分隔符)。

默认正则表达式是:

token_pattern='(?u)\b\w\w+\b'

只需编写您自己想要使用的正则表达式即可。

关于python - 如何阻止 Python Sci-kit 库的 Count Vectorizer 进行任何类型的单词过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34991405/

相关文章:

python - 如何在给定特征集作为字典的情况下实现交叉验证和随机森林分类器?

Python ctypes : passing to function with 'const char ** ' argument

python - matplotlib:如何有条件地从二维数组绘制直方图

python - 如何正确替换 QScintilla 小部件上的某些匹配项?

python - 主成分分析不起作用

python - 如何获取Scikit-learn的svm中的训练误差?

java - 如何将文件中所有使用的 ASCII 字符放入字典/数组/列表中并为每个字符分配一个值?

python - 修复 pyright 中的 'Import [module] could not be resolved'

python - 交叉验证和文本分类

python - Python 中的单词数统计