sklearn库调用k近邻算法
python实现KNN算法的全体流程代码
#1-1KNN算法的原理底层代码
import numpy as np
import 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 sqrt
distance=[]
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=6
n_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=c
print(y_predict)
plt.show() #输出点的个数
#1-2KNN算法在scikitlearn中的调用
import matplotlib
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
iris=datasets.load_iris() #鸢尾花数据集(150,4)
x=iris.data
y=iris.target
data=datasets.load_digits() #手写字体识别的数据集(1797,64),8x8的像素点数据,0-16之间表示灰度
x=data.data
y=data.target
print(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))*100
print("准确度为:%d"% (p)+"%")
#输出1个数字字体的实例
s=x[666]
s=s.reshape(8,8)
plt.imshow(s,cmap=matplotlib.cm.binary)
plt.show()
from sklearn import metrics
print(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与p
best_score=0.0
best_k=1
best_p=0
for 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=p
print("best_k=%d" % k)
print("best_score=",best_score)
print("best_p=",p)
#网格搜索方法寻找最优的超参数,它采用的评价指标是预测准确度,采用的方式是交叉验证CV
#导入scikitlearn中的网格搜索函数GridSearchCV
param_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 random
x=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 datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
iris=datasets.load_iris() #鸢尾花数据集(150,4)
x=iris.data
y=iris.target
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=666)
from sklearn.preprocessing import StandardScaler
s=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 MinMaxScaler
s=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))
实现效果如下所示: