jieba分詞
先上錯(cuò)誤代碼:
def cut_word(text):
# 中文分詞
text_new = jieba.cut(text)
return " ".join(list(text_new))
data2 = ['沉香燃明滅', '灰屑散不盡', '前塵空斷腸', '相思了無(wú)益']
cut_word(data2)
運(yùn)行之后,就會(huì)報(bào)錯(cuò)'list' object has no attribute 'decode'
修改代碼如下:
def cut_word(text):
# 中文分詞
text_new = jieba.lcut(str(text))
return " ".join(text_new)
這樣再運(yùn)行就可以了。
完整的jieba分詞+sklearn文本特征提取代碼如下:
def cut_word(text):
# 中文分詞
text_new = jieba.lcut(str(text))
return " ".join(text_new)
def count_chinese_demo(data):
# 中文文本特征抽取
data_new = []
for sent in data:
sent_new = cut_word(sent)
data_new.append(sent_new)
print(data_new)
# 1.實(shí)例化一個(gè)轉(zhuǎn)換器類
transfer = CountVectorizer()
# 2.調(diào)用fit_transform()
data_new = transfer.fit_transform(data_new)
print('new_data:\n', data_new.toarray())
print('特征名字:\n', transfer.get_feature_names())
return None
data2 = ['沉香燃明滅', '灰屑散不盡', '前塵空斷腸', '相思了無(wú)益']
count_chinese_demo(data2)
補(bǔ)充說(shuō)明: sklearn文本特征抽取CountVectorizer用法 sklearn.feature_extraction.text.CountVectorizer(stop_words=[]) stop_words 停用詞列表 返回詞頻矩陣 CountVectorizer.fit_transform(x) 統(tǒng)計(jì)特征值出現(xiàn)的個(gè)數(shù) x 文本或者包含文本字符串的可迭代對(duì)象 返回值:返回sparse矩陣 CountVectorizer.inverse_transform(x) 反變換 CountVectorizer.get_feature_names() 返回單詞列表
運(yùn)行結(jié)果如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-519433.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-519433.html
到了這里,關(guān)于jieba分詞+sklearn文本特征提取時(shí)報(bào)錯(cuò)‘list‘ object has no attribute ‘decode‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!