虽然有六个不同的词。结果中仅打印了5个字。如何基于所有单词(6列向量)获得结果?
from sklearn.feature_extraction.text import TfidfVectorizer
sent=["This is a sample", "This is another example"]
tf = TfidfVectorizer(analyzer='word', ngram_range=(1,1), min_df = 0)
tfidf_matrix = tf.fit_transform(sent)
print tfidf_matrix.toarray()
[[0. 0. 0.50154891 0.70490949 0.50154891] [
0.57615236 0.57615236 0.40993715 0. 0.40993715]]
还有如何打印列详细信息(功能(单词))和行详细信息(文档)?
最佳答案
您正在使用默认的token_pattern,它仅选择2个或更多字符的标记。
token_pattern:
“令牌”,仅在分析器=='word'时使用。默认正则表达式选择
2个或更多字母数字字符的记号(标点完全
被忽略,并始终被视为令牌分隔符)
如果定义新的token_pattern,则将获得'a'字符,例如:
from sklearn.feature_extraction.text import TfidfVectorizer
sent=["This is a sample", "This is another example"]
tf = TfidfVectorizer(token_pattern=u'(?u)\\b\\w+\\b')
tfidf_matrix = tf.fit_transform(sent)
print tfidf_matrix.toarray()
tf.vocabulary_
[[0.57615236 0. 0. 0.40993715 0.57615236 0.40993715]
[0. 0.57615236 0.57615236 0.40993715 0. 0.40993715]]
tf.vocabulary_
{u'a':0,u'sample':4,u'another':1,u'this':5,u'is':3,u'example':2}
关于python - tfidfvectorizer根据所有单词打印结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43136202/