用python统计单词出现的个数_使用countVectorizer来计算python中我自己的词汇表中的单词出现次数...

你可以通过在CountVectorizer中指定ngram_range参数来构建所有可能的二进制和三元组的词汇表。from sklearn.feature_extraction.text import CountVectorizer

Doc1 = 'And that was the fallacy. Once I was free to talk with staff members'

Doc2 = 'In the new, stripped-down, every-job-counts business climate, these human'

Doc3 = 'Another reality makes emotional intelligence ever more crucial'

Doc4 = 'The globalization of the workforce puts a particular premium on emotional'

Doc5 = 'As business changes, so do the traits needed to excel. Data tracking'

doc_set = [Doc1, Doc2, Doc3, Doc4, Doc5]

vectorizer = CountVectorizer(ngram_range=(2, 3))

tf = vectorizer.fit_transform(doc_set)

vectorizer.vocabulary_

vectorizer.get_feature_names()

tf.toarray()

至于你试图做的事情,如果你在你的词汇表上训练CountVectorizer然后转换文档,它将会起作用。my_vocabulary= ['was the fallacy', 'more crucial', 'particular premium', 'to excel', 'data tracking', 'another reality']

vectorizer = CountVectorizer(ngram_range=(2, 3))

vectorizer.fit_transform(my_vocabulary)

tf = vectorizer.transform(doc_set)

vectorizer.vocabulary_

Out[26]:

{'another reality': 0,

'data tracking': 1,

'more crucial': 2,

'particular premium': 3,

'the fallacy': 4,

'to excel': 5,

'was the': 6,

'was the fallacy': 7}

tf.toarray()

Out[25]:

array([[0, 0, 0, 0, 1, 0, 1, 1],

[0, 0, 0, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0],

[0, 0, 0, 1, 0, 0, 0, 0],

[0, 1, 0, 0, 0, 1, 0, 0]], dtype=int64)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值