栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021-09-29 Scikit-learn中CountVectorizer类的使用

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

2021-09-29 Scikit-learn中CountVectorizer类的使用

CountVectorizer会将文本中的词语转换为词频矩阵,它通过fit_transform函数计算各个词语出现的次数。它通过fit_transform()函数计算各个词语出现的次数,通过get_feature_names()可获取词袋中所有文本的关键字,通过toarray()可看到词频矩阵的结果。

from sklearn.feature_extraction.text import CountVectorizer

corpus=[
    'this is the first document',
    'this is the second second document',
    'and And and the third one'

]

vectorizer=CountVectorizer()

vectorizer.fit(corpus)

x=vectorizer.transform(corpus)

word=vectorizer.get_feature_names()

word_1=vectorizer.vocabulary_

print(word)    

#['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']

print(word_1)  

#{'this': 8, 'is': 3, 'the': 6, 'first': 2, 'document': 1, 'second': 5, 'and': 0, 'third': 7, 'one': 4}  key:词,value:词在词袋列表中的索引值。

print(x.toarray())

#[[0 1 1 1 0 0 1 0 1]
 [0 1 0 1 0 2 1 0 1]
 [3 0 0 0 1 0 1 1 0]] 文本中有多少个词就有多少列,有几个文本就有几行。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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