#----------------------------------Sklearn实现Knn------------------------------
# KNeighborsClassifier(n_neighbors=5, weights=’uniform’,
# algorithm=’auto’, leaf_size=30, p=2, metric=’minkowski’,
# metric_params=None, n_jobs=None, **kwargs)[source]
#--------------------------------------实例一-----------------------------------------------
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import train_test_split
import numpy as np
np.random.seed(0)#设置随机种子,设置后每次产生的随机数都一样
#加载数据集
iris=datasets.load_iris() #样本是150*4二维数据,代表150个样本,每个样本有4个特征
X=iris.data
y=iris.target
#分割数据集
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=1)
#print('训练集:',X_train,'测试集:',X_test)
#print('训练集',len(X_train),'测试集:',len(X_test))
#训练分类器
knn=KNeighborsClassifier()
knn.fit(X_train,y_train)
#预测
y_predict=knn.predict(X_test)
probility=knn.predict_proba(X_test)#计算各样本基于概率的预测
#计算准确率
score=knn.score(X_test,y_test,sample_weight=None)
print('预测值:',y_predict,'实际值:',y_test,'得分:',score)
#----------------------------实例二----------------------------------
from sklearn.neighbors import KNeighborsClassifier
X=[[0],[1],[2],[3]]
y=[0,0,1,1]
neigh=KNeighborsClassifier(n_neighbors=3)
neigh.fit(X,y)
print(neigh.predict([[1.1]]))
print(neigh.predict_proba([[0.9]]))
#-----------------------------------实例三---------------------------
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors,datasets
iris=datasets.load_iris()
X=iris.data[:,:2]
y=iris.target
h=0.2#网格中的步长
#颜色图
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])
for weights in ['uniform','distance']:
clf=neighbors.KNeighborsClassifier(15,weights=weights)
clf.fit(X,y)
#画决策的边界
x_min,x_max=X[:,0].min()-1,X[:,0].max()+1
y_min,y_max=X[:,1].min()-1,X[:,1].max()+1
xx,yy=np.meshgrid(np.arange(x_min,x_max,h),
np.arange(y_min,y_max,h))
Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=20)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("3-Class classification (k = %i, weights = '%s')"
% (15, weights))
plt.show()
|
|