本特征提取:
將文本數(shù)據(jù)轉(zhuǎn)化成特征向量的過程
比較常用的文本特征表示法為詞袋法
詞袋法:
不考慮詞語出現(xiàn)的順序卒蘸,每個出現(xiàn)過的詞匯單獨作為一列特征
這些不重復(fù)的特征詞匯集合為詞表
每一個文本都可以在很長的詞表上統(tǒng)計出一個很多列的特征向量
如果每個文本都出現(xiàn)的詞匯呼渣,一般被標記為 停用詞 不計入特征向量
主要有兩個api來實現(xiàn) CountVectorizer 和 TfidfVectorizer
CountVectorizer:考慮詞匯在文本中出現(xiàn)的頻率
TfidfVetorizer:除了考量某詞匯在文本出現(xiàn)的頻率,還關(guān)注包含這個詞匯的所有文本的數(shù)量霜医,能夠削減高頻沒有意義的詞匯出現(xiàn)帶來的影響, 挖掘更有意義的特征坤次,文本條目越多,Tfid的效果會越顯著
'''
from sklearn.datasets import fetch_20newsgroups
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
1 下載新聞數(shù)據(jù)
news = fetch_20newsgroups(subset="all")
2 分割訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)
x_train, x_test, y_train, y_test = train_test_split(news.data,
news.target,
test_size=0.25,
random_state=33)
3.1 采用普通統(tǒng)計CountVectorizer提取特征向量
默認配置不去除停用詞
count_vec = CountVectorizer()
x_count_train = count_vec.fit_transform(x_train)
x_count_test = count_vec.transform(x_test)
去除停用詞
count_stop_vec = CountVectorizer(analyzer='word', stop_words='english')
x_count_stop_train = count_stop_vec.fit_transform(x_train)
x_count_stop_test = count_stop_vec.transform(x_test)
3.2 采用TfidfVectorizer提取文本特征向量
默認配置不去除停用詞
tfid_vec = TfidfVectorizer()
x_tfid_train = tfid_vec.fit_transform(x_train)
x_tfid_test = tfid_vec.transform(x_test)
去除停用詞
tfid_stop_vec = TfidfVectorizer(analyzer='word', stop_words='english')
x_tfid_stop_train = tfid_stop_vec.fit_transform(x_train)
x_tfid_stop_test = tfid_stop_vec.transform(x_test)
4 使用樸素貝葉斯分類器 分別對兩種提取出來的特征值進行學(xué)習(xí)和預(yù)測
對普通通統(tǒng)計CountVectorizer提取特征向量 學(xué)習(xí)和預(yù)測
mnb_count = MultinomialNB()
mnb_count.fit(x_count_train, y_train) # 學(xué)習(xí)
mnb_count_y_predict = mnb_count.predict(x_count_test) # 預(yù)測
去除停用詞
mnb_count_stop = MultinomialNB()
mnb_count_stop.fit(x_count_stop_train, y_train) # 學(xué)習(xí)
mnb_count_stop_y_predict = mnb_count_stop.predict(x_count_stop_test) # 預(yù)測
對TfidfVectorizer提取文本特征向量 學(xué)習(xí)和預(yù)測
mnb_tfid = MultinomialNB()
mnb_tfid.fit(x_tfid_train, y_train)
mnb_tfid_y_predict = mnb_tfid.predict(x_tfid_test)
去除停用詞
mnb_tfid_stop = MultinomialNB()
mnb_tfid_stop.fit(x_tfid_stop_train, y_train) # 學(xué)習(xí)
mnb_tfid_stop_y_predict = mnb_tfid_stop.predict(x_tfid_stop_test) # 預(yù)測
5 模型評估
對普通統(tǒng)計CountVectorizer提取的特征學(xué)習(xí)模型進行評估
print("未去除停用詞的CountVectorizer提取的特征學(xué)習(xí)模型準確率:", mnb_count.score(x_count_test, y_test))
print("更加詳細的評估指標:\n", classification_report(mnb_count_y_predict, y_test))
print("去除停用詞的CountVectorizer提取的特征學(xué)習(xí)模型準確率:", mnb_count_stop.score(x_count_stop_test, y_test))
print("更加詳細的評估指標:\n", classification_report(mnb_count_stop_y_predict, y_test))
對TfidVectorizer提取的特征學(xué)習(xí)模型進行評估
print("TfidVectorizer提取的特征學(xué)習(xí)模型準確率:", mnb_tfid.score(x_tfid_test, y_test))
print("更加詳細的評估指標:\n", classification_report(mnb_tfid_y_predict, y_test))
print("去除停用詞的TfidVectorizer提取的特征學(xué)習(xí)模型準確率:", mnb_tfid_stop.score(x_tfid_stop_test, y_test))
print("更加詳細的評估指標:\n", classification_report(mnb_tfid_stop_y_predict, y_test))
'''
未去除停用詞的CountVectorizer提取的特征學(xué)習(xí)模型準確率: 0.8397707979626485
更加詳細的評估指標:
precision recall f1-score support
0 0.86 0.86 0.86 201
1 0.86 0.59 0.70 365
2 0.10 0.89 0.17 27
3 0.88 0.60 0.72 350
4 0.78 0.93 0.85 204
5 0.84 0.82 0.83 271
6 0.70 0.91 0.79 197
7 0.89 0.89 0.89 239
8 0.92 0.98 0.95 257
9 0.91 0.98 0.95 233
10 0.99 0.93 0.96 248
11 0.98 0.86 0.91 272
12 0.88 0.85 0.86 259
13 0.94 0.92 0.93 252
14 0.96 0.89 0.92 239
15 0.96 0.78 0.86 285
16 0.96 0.88 0.92 272
17 0.98 0.90 0.94 252
18 0.89 0.79 0.84 214
19 0.44 0.93 0.60 75
avg / total 0.89 0.84 0.86 4712
去除停用詞的CountVectorizer提取的特征學(xué)習(xí)模型準確率: 0.8637521222410866
更加詳細的評估指標:
precision recall f1-score support
0 0.89 0.85 0.87 210
1 0.88 0.62 0.73 352
2 0.22 0.93 0.36 59
3 0.88 0.62 0.73 341
4 0.85 0.93 0.89 222
5 0.85 0.82 0.84 273
6 0.79 0.90 0.84 226
7 0.91 0.91 0.91 239
8 0.94 0.98 0.96 264
9 0.92 0.98 0.95 236
10 0.99 0.92 0.95 251
11 0.97 0.91 0.93 254
12 0.89 0.87 0.88 254
13 0.95 0.94 0.95 248
14 0.96 0.91 0.93 233
15 0.94 0.87 0.90 250
16 0.96 0.89 0.93 271
17 0.98 0.95 0.97 238
18 0.90 0.84 0.87 200
19 0.53 0.91 0.67 91
avg / total 0.90 0.86 0.87 4712
TfidVectorizer提取的特征學(xué)習(xí)模型準確率: 0.8463497453310697
更加詳細的評估指標:
precision recall f1-score support
0 0.67 0.84 0.75 160
1 0.74 0.85 0.79 218
2 0.85 0.82 0.83 256
3 0.88 0.76 0.82 275
4 0.84 0.94 0.89 217
5 0.84 0.96 0.89 229
6 0.69 0.93 0.79 192
7 0.92 0.84 0.88 259
8 0.92 0.98 0.95 259
9 0.91 0.96 0.94 238
10 0.99 0.88 0.93 264
11 0.98 0.73 0.83 321
12 0.83 0.91 0.87 226
13 0.92 0.97 0.95 231
14 0.96 0.89 0.93 239
15 0.97 0.51 0.67 443
16 0.96 0.83 0.89 293
17 0.97 0.92 0.95 245
18 0.62 0.98 0.76 119
19 0.16 0.93 0.28 28
avg / total 0.88 0.85 0.85 4712
去除停用詞的TfidVectorizer提取的特征學(xué)習(xí)模型準確率: 0.8826400679117148
更加詳細的評估指標:
precision recall f1-score support
0 0.81 0.86 0.83 190
1 0.81 0.85 0.83 238
2 0.87 0.84 0.86 257
3 0.88 0.78 0.83 269
4 0.90 0.92 0.91 235
5 0.88 0.95 0.91 243
6 0.80 0.90 0.85 230
7 0.92 0.89 0.90 244
8 0.94 0.98 0.96 265
9 0.93 0.97 0.95 242
10 0.99 0.88 0.93 264
11 0.98 0.85 0.91 273
12 0.86 0.93 0.89 231
13 0.93 0.96 0.95 237
14 0.97 0.90 0.93 239
15 0.96 0.70 0.81 320
16 0.98 0.84 0.90 294
17 0.99 0.92 0.95 248
18 0.74 0.97 0.84 145
19 0.29 0.96 0.45 48
avg / total 0.90 0.88 0.89 4712
'''
重要參考鏈接:
https://www.cnblogs.com/Lin-Yi/p/8974108.html
https://blog.csdn.net/weixin_43026262/article/details/95045331
https://blog.csdn.net/weixin_38278334/article/details/82320307