注意代码中坐标横轴分层测试集的实现:
train_sizes
---------------------'
# ================================================
# 朴素贝叶斯预测糖尿病
# 与逻辑回归比较
# 2019-03-02
# ================================================
%matplotlib inline
df = pd.read_csv('./pima-indians-diabetes.data', header=None)
# print(df)
y = df[8]
X = df[[0, 1, 2, 3, 4, 5, 6, 7]]
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=11)
print(len(X_train))
lr = LogisticRegression()
nb = GaussianNB()
lr_scores = []
nb_scores = []
print('========不断提高训练的样本数据量=======')
train_sizes = range(10, len(X_train), 10)
for train_size in train_sizes:
X_slice, _, y_slice, _ = train_test_split(
X_train, y_train, train_size=train_size, stratify=y_train, random_state=11)
nb.fit(X_slice, y_slice)
nb_scores.append(nb.score(X_test, y_test))
lr.fit(X_slice, y_slice)
lr_scores.append(lr.score(X_test, y_test))
plt.plot(train_sizes, nb_scores, label='Naive Bayes')
plt.plot(train_sizes, lr_scores, linestyle='--', label='Logistic Regression')
plt.title("Naive Bayes and Logistic Regression Accuracies")
plt.xlabel("Number of training instances")
plt.ylabel("Test set accuracy")
plt.legend()
![v2-8e5fc3ae75d082e139075f15e7bb3db0_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=02e7fcba-4b2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-8e5fc3ae75d082e139075f15e7bb3db0_b.jpg)