python实现KNN算法的全体流程代码#1-1KNN算法的原理底层代码import numpy as npimport matplotlib.pyplot as plt #导入相应的数据可视化模块raw_data_X=[[3.393533211,2.331273381], [3.110073483,1.781539638], [1.343808831,3.368360954], [3.582294042,4.679179110], [2.280362439,2.866990263], [7.423436942,4.696522875], [5.745051997,3.533989803], [9.172168622,2.511101045], [7.792783481,3.424088941], [7.939820817,0.791637231]]raw_data_Y=[0,0,0,0,0,1,1,1,1,1]print(raw_data_X)print(raw_data_Y)x_train=np.array(raw_data_X)y_train=np.array(raw_data_Y) #数据的预处理,需要将其先转换为矩阵,并且作为训练数据集print(x_train.ndim)print(y_train.ndim)print(x_train)print(y_train)plt.figure(1)plt.scatter(x_train[y_train==0,0],x_train[y_train==0,1],color="g")plt.scatter(x_train[y_train==1,0],x_train[y_train==1,1],color="r") #将其散点图输出x=np.array([8.093607318,3.365731514]) #定义一个新的点,需要判断它到底属于哪一类数据类型plt.scatter(x[0],x[1],color="b") #在算点图上输出这个散点,看它在整体散点图的分布情况#kNN机器算法的使用from math import sqrtdistance=[]for x_train in x_train: d=sqrt(np.sum((x_train-x)**2)) distance.append(d) print(distance)d1=np.argsort(distance) #输出distance排序的索引值print(d1)k=6n_k=[y_train[(d1[i])] for i in range(0,k)]print(n_k)from collections import Counter #导入Counter模块c=Counter(n_k).most_common(1)[0][0] #Counter模块用来输出一个列表中元素的个数,输出的形式为列表,其里面的元素为不同的元组#另外的话对于Counter模块它有.most_common(x)可以输出统计数字出现最多的前x个元组,其中元组的key是其元素值,后面的值是出现次数print(Counter(n_k))y_predict=cprint(y_predict)plt.show() #输出点的个数 #1-2KNN算法在scikitlearn中的调用import matplotlibimport matplotlib.pyplot as pltfrom sklearn import datasetsfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.model_selection import train_test_splitiris=datasets.load_iris() #鸢尾花数据集(150,4)x=iris.datay=iris.targetdata=datasets.load_digits() #手写字体识别的数据集(1797,64),8x8的像素点数据,0-16之间表示灰度x=data.datay=data.targetprint(x.shape)print(y.shape)shuffle_index=np.random.permutation(len(x)) #对索引进行随机打乱x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=666)knn_classifier=KNeighborsClassifier(n_neighbors=4)knn_classifier.fit(x_train,y_train)y_predict=knn_classifier.predict(x_test)p=(sum(knn_classifier.predict(x_test)==y_test)/len(x_test))*100print("准确度为:%d"% (p)+"%")#输出1个数字字体的实例s=x[666]s=s.reshape(8,8)plt.imshow(s,cmap=matplotlib.cm.binary)plt.show()from sklearn import metricsprint(metrics.accuracy_score(y_test,y_predict)) #输出准确度print(metrics.accuracy_score(y_test,y_predict,normalize=False)) #输出准确的预测个数print(metrics.confusion_matrix(y_test,y_predict)) #输出混淆矩阵的大小print(knn_classifier.score(x_test,y_test))#超参数调节寻找最好的K值,weights=["uniform","distance"],p=0-10#采用for循环来进行寻找最好的超参数k与pbest_score=0.0best_k=1best_p=0for k in range(1,11): for p in range(1,6): knn=KNeighborsClassifier(n_neighbors=k,weights="distance",p=p) knn.fit(x_train,y_train) score=knn.score(x_test,y_test) if score>best_score: best_score=score best_k=k best_p=pprint("best_k=%d" % k)print("best_score=",best_score)print("best_p=",p)#网格搜索方法寻找最优的超参数,它采用的评价指标是预测准确度,采用的方式是交叉验证CV#导入scikitlearn中的网格搜索函数GridSearchCVparam_grid=[ { "weights":["uniform"], "n_neighbors":[i for i in range(1,11)] }, { "weights":["distance"], "n_neighbors":[i for i in range(1,11)], "p":[i for i in range(1,5)] }]k=KNeighborsClassifier()from sklearn.model_selection import GridSearchCV#定义相应网格搜索方式(输入初始化参数:1机器学习算法、2超参数组合列表、3n_jobs(选择并行内核数目,-1表示全部是用),4verbose=2表示输出相应搜索过程)grid_search=GridSearchCV(k,param_grid,n_jobs=-1)grid_search.fit(x_train,y_train)print(grid_search.best_estimator_)print(grid_search.best_params_) #输出最好的超参数组合print(grid_search.best_score_) #输出最好的模型的时候的准确度knn_best=grid_search.best_estimator_ #定义出最好的分类器y_pre=knn_best.predict(x_test)print(y_pre)print(knn_best.score(x_test,y_test)) #数据归一化,将数据映射到同一个尺度,降低数据引起的偏差#最值归一化,收到outline影响比较大,有明显边界,成绩等#均值方差归一化:均值为0,方差为1,数据没有明显边界,有可能存在极端的数据值,收入等#最值归一化import randomx=np.random.randint(0,100,size=100)x=(x-np.min(x))/(np.max(x)-np.min(x))print(x)x=np.random.randint(0,100,size=(50,2))x=np.array(x,dtype=float)x[:,0]=(x[:,0]-np.min(x[:,0]))/(np.max(x[:,0]-np.min(x[:,0]))) #将0列第一个特征数据进行最值归一化处理print(x[:,0])print(np.mean(x[:,0]))print(np.std(x[:,0]))#均值方差归一化函数x[:,1]=(x[:,1]-np.mean(x[:,1]))/np.std(x[:,1])print(x[:,1])print(np.mean(x[:,1]))print(np.std(x[:,1])) #数据归一化的使用#scikit-learn中的StandardScaler均值方差归一化from sklearn import datasetsfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.model_selection import train_test_splitiris=datasets.load_iris() #鸢尾花数据集(150,4)x=iris.datay=iris.targetx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=666)from sklearn.preprocessing import StandardScalers=StandardScaler()s.fit(x_train)print(s.mean_)print(s.scale_)#将两个数据集进行归一化处理x_train=s.transform(x_train)x_test=s.transform(x_test)#使用归一化的数据集进行模型训练k=KNeighborsClassifier(n_neighbors=3)k.fit(x_train,y_train)print(k.predict(x_test))print(k.score(x_test,y_test))#scikit-learn中的MinMaxScaler最值归一化from sklearn.preprocessing import MinMaxScalers=MinMaxScaler()s.fit(x_train)#将两个数据集进行归一化处理x_train=s.transform(x_train)x_test=s.transform(x_test)#使用归一化的数据集进行模型训练k=KNeighborsClassifier(n_neighbors=3)k.fit(x_train,y_train)print(k.predict(x_test))print(k.score(x_test,y_test))实现效果如下所示:
原文地址:https://www.cnblogs.com/Yanjy-OnlyOne/p/12506816.html
时间: 2024-10-06 02:47:42