栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在python中使用sklearn计算n-gram的TF-IDF

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在python中使用sklearn计算n-gram的TF-IDF

尝试增加

ngram_range
in
TfidfVectorizer

tfidf = TfidfVectorizer(vocabulary = myvocabulary, stop_words = 'english', ngram_range=(1,2))

编辑: 的输出

TfidfVectorizer
是稀疏格式的TF-IDF矩阵(或者实际上是您寻求的格式的转置)。您可以打印出其内容,例如:

feature_names = tfidf.get_feature_names()corpus_index = [n for n in corpus]rows, cols = tfs.nonzero()for row, col in zip(rows, cols):    print((feature_names[col], corpus_index[row]), tfs[row, col])

应该产生

('biscuit pudding', 1) 0.646128915046('chocolates', 1) 0.763228291628('chocolates', 2) 0.508542320378('tim tam', 2) 0.861036995944('chocolates', 3) 0.508542320378('fresh milk', 3) 0.861036995944

如果矩阵不大,则以密集形式检查矩阵可能会更容易。

Pandas
使这个非常方便:

import pandas as pddf = pd.Dataframe(tfs.T.todense(), index=feature_names, columns=corpus_index)print(df)

这导致

  1         2         3tim tam          0.000000  0.861037  0.000000jam   0.000000  0.000000  0.000000fresh milk       0.000000  0.000000  0.861037chocolates       0.763228  0.508542  0.508542biscuit pudding  0.646129  0.000000  0.000000


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号