自然语言处理_基础技术4_CountVectorizer
onehot编码是一种稀疏编码方式,如果词语越多,维度也越大.会出现维数灾难.针对one-hot编码,sklearn中实现如下.CountVectorizer类(计数向量)先用英文举例.它会针对每个单词计数,丢失位置信息from sklearn.feature_extraction.text import CountVectorizercorpus = ['This is the first do
·
onehot编码是一种稀疏编码方式,如果词语越多,维度也越大.会出现维数灾难.
针对one-hot编码,sklearn中实现如下.CountVectorizer类(计数向量)
先用英文举例.
它会针对每个单词计数,丢失位置信息
from sklearn.feature_extraction.text import CountVectorizer
corpus = [
'This is the first document.',
'This document is the second document.',
'And this is the third one.',
'Is this the first document?',
]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names())
print(X.toarray())
下面这个是ngram为2的编码方式
vectorizer2 = CountVectorizer(analyzer='word', ngram_range=(2, 2))
X2 = vectorizer2.fit_transform(corpus)
print(vectorizer2.get_feature_names())
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)