import warnings
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
matplotlib.use('TkAgg')
warnings.filterwarnings("ignore",category=UserWarning)
#准备数据
iris = load_iris()
data = iris.data
target=iris.target
#拆分样本集
def stt(train_size,seed):
#数据数种子
np.random.seed(seed)
#获得打乱的索引
random_index = np.random.permutation(target.size)
# 数据预处理
X = data[random_index]
y = target[random_index]
# 拆分策略
n_split = int(y.size*train_size) #拆分策略 train_size=0.1, 0.2, 0.3........
# 训练集
x_train = X[:n_split]
y_train = y[:n_split]
# 测试集
x_test = X[n_split:]
y_test = y[n_split:]
return x_train, y_train, x_test, y_test
#模型评价
def acc_mean(knn,train_size,times):
train_acc_list = []
test_acc_list = []
#循环拆分 times 次 得到不同的样本拆分集 用于得到评分
for i in range(times):
x_train, y_train, x_test, y_test = stt(train_size,i)
#训练
knn.fit(x_train, y_train)
#测试
y_ = knn.predict(x_test)
#训练集评分
train_acc = knn.score(x_train,y_train)
train_acc_list.append(train_acc)
#测试集评分
test_acc = knn.score(x_test, y_test)
test_acc_list.append(test_acc)
#训练集评分 平均数
train_mean = np.array(train_acc_list).mean()
#测试集评分 平均数
test_mean = np.array(test_acc_list).mean()
return train_mean,test_mean
k_list = np.arange(1,int(np.sqrt(target.shape[0])+10),2) #[1 3 5 7 9 11]
k_train_mean=[]
k_test_mean=[]
for k in k_list:
knn = KNeighborsClassifier(n_neighbors=k)
train_mean,test_mean = acc_mean(knn,0.8,100)
k_train_mean.append(train_mean)
k_test_mean.append(test_mean)
# print(k_train_mean)
# print(k_test_mean)
#绘制学习曲线
plt.plot(k_list,k_train_mean,label = "acc_train_mean")
plt.plot(k_list,k_test_mean,label = "acc_test_mean")
plt.xlabel("k_list",fontsize=15)
plt.ylabel("acc",fontsize=15)
plt.xticks(k_list) #刻度标签
plt.legend() #显示图例
plt.show()
#散点图plt.scatter(X[""],X[""],c = target)
#绘制评分差值图形
# plt.plot(k_list,np.array(k_train_mean)-np.array(k_test_mean))
# plt.xticks(k_list)
# plt.show()
数据分析与挖掘-KNN算法-3模型评估之样本集拆分-拆分策略-优化
最新推荐文章于 2024-09-03 21:45:00 发布