头歌基于KNN的鸢尾花数据集分类

#调用库
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix

#数据集加载
dataset = datasets.load_iris()
X = dataset.data[:, :2]
y = dataset.target
attributes_dict = {0:"sepal_length",1:"sepal_width"}
for attribute in attributes_dict:
    ########## Begin ##########
    #计算该特征在数据集中的最大值
    print("{} 最大值:{}".format(attributes_dict[attribute], np.max(X[:,attribute])))
    #计算该特征在数据集中的最小值
    print("{} 最小值:{}".format(attributes_dict[attribute], np.min(X[:,attribute])))
    #计算该特征在数据集中的平均值
    # round 函数将float数据格式化小数点后一位
    print("{} 平均值:{}".format(attributes_dict[attribute], round(np.average(X[:, attribute]),1)))
    ########## End ##########
    print("-------------------------------------")



#数据集划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
#训练KNN
########### Begin ##########
#使用K最近邻分类器对数据进行分类,并计算混淆矩阵(取最近的5个邻居作为参考样本,使用闵可夫斯基距离来衡量样本之间的距离,p参数设置为2)
classifier =  KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
#模型训练
classifier.fit(X_train, y_train)
#模型预测
y_pred =  classifier.predict(X_test)
#生成混淆矩阵cm,用于评估分类器的性能。
cm =  confusion_matrix(y_test, y_pred)
print('cm',cm)
########### End ##########


#训练可视化
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'black', 'orange')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    color_list = [[[1,0,0],[0,1,0],[0,0,1]][i]]
    count = np.sum((y_set == j)==True)
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = color_list*count, label = j)
plt.title('K-NN (Test set-k=5)')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.legend()
plt.show()
plt.savefig('/data/workspace/myshixun/step1/reality/pic')


#可视化测试结果
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'black', 'orange')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    color_list = [[[1,0,0],[0,1,0],[0,0,1]][i]]
    count = np.sum((y_set == j)==True)
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = color_list*count, label = j)
plt.title('K-NN (Test set-k=5)')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.legend()
plt.show()
plt.savefig('/data/workspace/myshixun/step1/reality/pic1')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值