使用KNN算法進行分類
阿新 • • 發佈:2018-04-07
orange clu log num sam width ssi numpy atp
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);
使用KNN算法進行分類