【发布时间】:2021-12-24 11:42:22
【问题描述】:
from sklearn.feature_extraction.text import CountVectorizer
foo = ["the Cat is :", "is smart now"]
cv = CountVectorizer(vocabulary = foo)
new_list =["the Cat is : the most","is smart now"]
data = cv.fit_transform(new_list).toarray()
print(data)
代码返回以下内容:
[[0 0]
[0 0]]
但我希望它返回:
[[1 0]
[0 1]]
我尝试调整传递给CountVectorizer() 的参数,但似乎没有任何解决方法。有什么建议吗??
【问题讨论】:
-
这对您有帮助吗? stackoverflow.com/questions/55573279/… - 注意他们是如何定义他们的词汇的,它需要是一个字典,按照 docs
-
根据我对文档的理解:vocab = ["the"] 或 vocab = {'the':0} 都有效,我测试了代码。但是,如果我尝试在字符串中添加空格以具有: vocab = ["the cat"] 或 vocab = {'the cat':0} 我得到全零
-
你说得对,我读的是可调用不可迭代,我很抱歉。至于其余的,这是因为根据第一个链接进行了标记化。标记字符串给出
[['The', 'cat', 'is', 'the', 'most'], ['is', 'smart' 'now']]。您的标记器必须返回所需的标记以匹配词汇。从链接中,您可以看到如何定义自己的标记器来执行此操作。否则,您可能需要重新考虑为什么要尝试使用CountVectorizer来完成任务,以及它是否是最佳或最理想的方法。
标签: python scikit-learn countvectorizer