使用KNN算法进行分类

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 from sklearn.datasets.samples_generator import make_blobs
 5 # 生成数据
 6 centers = [[-2, 2], [2, 2], [0, 4]]
 7 X, y = make_blobs(n_samples=600, centers=centers, random_state=0, cluster_std=0.60)
 8 # 画出数据
 9 plt.figure(figsize=(16, 10), dpi=144)
10 c = np.array(centers)
11 plt.scatter(X[:, 0], X[:, 1], c=y, s=100, cmap='cool');         # 画出样本
12 plt.scatter(c[:, 0], c[:, 1], s=100, marker='^', c='orange');   # 画出中心点
13 
14 from sklearn.neighbors import KNeighborsClassifier
15 from numpy as np
16 # 模型训练
17 k = 5
18 clf = KNeighborsClassifier(n_neighbors=k)
19 clf.fit(X, y);
20 
21 # 进行预测
22 # X_sample = [[0,2],[1,1],[-1,3]]
23 X_sample = np.array([[0,2],[1,1],[-1,3]],dtype=int)
24 
25 y_sample = clf.predict(X_sample);
26 neighbors = clf.kneighbors(X_sample, return_distance=False);
27 
28 X_sample_disp_x = np.array(X_sample[:,0],dtype=int)
29 X_sample_disp_y = np.array(X_sample[:,1],dtype=int)
30 # 画出示意图
31 plt.figure(figsize=(16, 10), dpi=144)
32 plt.scatter(X[:, 0], X[:, 1], c=y, s=100, cmap='cool');    # 样本
33 plt.scatter(c[:, 0], c[:, 1], s=100, marker='^', c='k');   # 中心点
34 plt.scatter(X_sample_disp_x, X_sample_disp_y, marker="x", 
35             c=y_sample, s=100, cmap='cool')    # 待预测的点
36 
37 
38 
39 for i in neighbors[0]:
40     plt.plot([X[i][0], X_sample[0][0]], [X[i][1], X_sample[0][1]],
41              'k--', linewidth=0.8);    # 预测点与距离最近的 5 个样本的连线
42 for i in neighbors[1]:
43     plt.plot([X[i][0], X_sample[1][0]], [X[i][1], X_sample[1][1]],
44              'k--', linewidth=0.8);
45 for i in neighbors[2]:
46     plt.plot([X[i][0], X_sample[2][0]], [X[i][1], X_sample[2][1]],
47              'k--', linewidth=0.8);

posted @ 2018-04-07 14:24  DUDU1992  阅读(507)  评论(0编辑  收藏  举报