gpt4 book ai didi

python - CountVectorizer(分析器 ='char_wb')未按预期工作

转载 作者:太空狗 更新时间:2023-10-30 00:48:48 26 4
gpt4 key购买 nike

我正在尝试使用 scikit-learn 的 CountVectorizer 计算字符 2-gram,忽略空格。在docs它提到了参数 analyzer 声明

Whether the feature should be made of word or character n-grams. Option ‘char_wb’ creates character n-grams only from text inside word boundaries.

但是,“char_wb”似乎没有像我预期的那样工作。例如:

corpus = [
"The blue dog Blue",
"Green the green cat",
"The green mouse",
]

# CountVectorizer character 2-grams with word boundaries
vectorizer = CountVectorizer(analyzer='char_wb', ngram_range=(2, 2), min_df=1)
X = vectorizer.fit_transform(corpus)
vectorizer.get_feature_names()
[' b',
' c',
' d',
' g',
' m',
' t',
'at',
'bl',
'ca', ....

请注意像“b”这样的示例,其中包含一个空格。给了什么?

最佳答案

我认为这是文档中长期存在的错误,欢迎您帮助修复。更正确的说法是:

Option ‘char_wb’ creates character n-grams, but does not generate n-grams that cross word boundaries.

更改似乎是在 this commit 中进行的以确保;查看贡献者的 comment .将二元语法输出与 analyzer='char' 的输出进行比较时,它看起来特别尴尬,但是当你增加到三元语法时,你会看到空格可以开始或结束一个 n-gram,但不能在中间.这有助于表示特征的词首或词尾性质,而无需捕获嘈杂的交叉词字符 n-gram。它还确保,与提交之前不同,所有提取的 n-gram 都具有相同的长度!

关于python - CountVectorizer(分析器 ='char_wb')未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36188875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com