# 机器学习之红楼梦作者判断(贝叶斯分类)

·  阅读 866

## 基本过程

### 准备工作

import os
import numpy as np
import pandas as pd
import re
import sys
import matplotlib.pyplot as plt

import jieba
import jieba.analyse

vorc = [jieba.analyse.extract_tags(i, topK=1000) for i in text["text"]]
vorc = [" ".join(i) for i in vorc]

from  sklearn.feature_extraction.text import CountVectorizer
vertorizer = CountVectorizer(max_features=5000)
train_data_features = vertorizer.fit_transform(vorc)

train_data_features = train_data_features.toarray()

train_data_features.shape

### 标签生成

labels = np.array([[0] * 80 + [1] * 40]).reshape(-1 ,1) # 目标值
labels.shape

### 分层随机抽样

# 分层抽样
from sklearn.model_selection import train_test_split
# train_data_features = train_data_features[0:80]
X_train, X_test, Y_train, Y_test = train_test_split(train_data_features, labels,
test_size = 0.2, stratify=labels)

### 模型训练和预测

from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression

gnb = GaussianNB()
gnb.fit(X_train, Y_train)

y_pred = gnb.predict(X_test)

from sklearn.metrics import accuracy_score
accuracy_score(Y_test, y_pred)
# 0.875

# 分层交叉验证

from sklearn.model_selection import StratifiedShuffleSplit
sss = StratifiedShuffleSplit(n_splits=10,test_size=0.2)
for train_index, test_index in sss.split(train_data_features, labels):
X_train, X_test = train_data_features[train_index], train_data_features[test_index]
Y_train, Y_test = labels[train_index], labels[test_index]
gnb = GaussianNB()
gnb.fit(X_train, Y_train)
Y_pred = gnb.predict(X_test)
scores.append(accuracy_score(Y_test, Y_pred))
print(scores)
print(np.array(scores).mean())

[0.9166666666666666, 0.8333333333333334, 1.0, 0.875, 0.75, 0.8333333333333334, 0.8333333333333334, 0.9583333333333334, 0.875, 0.8333333333333334]
0.8708333333333333

### 交叉验证

labels_val = np.array([[0] * 40 + [1] * 40]).reshape(-1 ,1) # 目标值
sss_val = StratifiedShuffleSplit(n_splits=5,test_size=0.2)#分成5组，测试比例为0.25，训练比例是0.75
scores = []
train_data_features_val = train_data_features[0:80]
for train_index, test_index in sss_val.split(train_data_features_val, labels_val):
X_train, X_test = train_data_features_val[train_index], train_data_features_val[test_index]
Y_train, Y_test = labels_val[train_index], labels_val[test_index]
gnb = GaussianNB()
gnb.fit(X_train, Y_train)
Y_pred = gnb.predict(X_test)
scores.append(accuracy_score(Y_test, Y_pred))
print(scores)
print(np.array(scores).mean())

[0.8125, 0.875, 0.75, 0.875, 0.75]
0.8125