import numpy as np
import matplotlib.pylab as pyb
%matplotlib inline
from sklearn.neighbors import kneighborsclassifier
from sklearn import datasets
from sklearn.model_selection import train_test_split
x, y = datasets.load_iris(
true
)x = x[:,
:2]x_train,x_test, y_train, y_test = train_test_split(x, y, train_size =
1, random_state =
100)
x_test
pyb.scatter(x[:,
0],x[:,1
], c =y)
pyb.show(
)
[外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳(img-rhxjhlsd-1591024383488)(output_3_0.png)]
x1 = np.linspace(4,
8,100)
y1 = np.linspace(2,
4.5,80)
x1,y1 = np.meshgrid(x1, y1)
x1 = x1.reshape(-1
,1)y1 = y1.reshape(-1
,1)data = np.concatenate(
(x1,y1)
,axis=1)
data.shape
(8000, 2)
%
%time
knn = kneighborsclassifier(n_neighbors=5)
knn.fit(x_train, y_train)
wall time: 3 ms
kneighborsclassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=none, n_jobs=none, n_neighbors=5, p=2,
weights='uniform')
y_ = knn.predict(data)
from matplotlib.colors import listedcolormap
lc = listedcolormap(
['#ffaaaa'
,'#aaffaa'
,'#aaaaff'])
lc2 = listedcolormap(
['#ff0000'
,'#00ff00'
,'#0000ff'
])
pyb.scatter(data[:,
0],data[:,
1],c = y_, cmap=lc)
pyb.scatter(x_train[:,
0],x_train[:,
1],c = y_train, cmap=lc2)
pyb.show(
)
[外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳(img-nc7p2lrg-1591024383493)(output_8_0.png)]
pyb.contourf(x1.reshape(80,
100)
,y1.reshape(80,
100)
,y_.reshape(80,
100)
,cmap=lc)
pyb.scatter(x_train[:,
0],x_train[:,
1],c = y_train, cmap=lc2)
pyb.show(
)
[外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳(img-swp9wqxm-1591024383498)(output_9_0.png)]
from sklearn.model_selection import cross_val_score
knn = kneighborsclassifier(
)score = cross_val_score(knn,x, y, scoring=
'accuracy'
,cv =6)
score.mean(
)
0.7466666666666666
from sklearn.model_selection import cross_val_score
x,y = datasets.load_iris(
true
)erros =
weights =
['distance'
,'uniform'
]for k in
range(1
,14):
for w in weights:
knn = kneighborsclassifier(n_neighbors=k,weights=w)
score = cross_val_score(knn,x,y,scoring=
'accuracy'
,cv =6)
.mean(
)# 誤差越小,說明k選擇越合適,越好
erros[w+
str(k)
]= score
erros
list
(erros)
['distance1',
'uniform1',
'distance2',
'uniform2',
'distance3',
'uniform3',
'distance4',
'uniform4',
'distance5',
'uniform5',
'distance6',
'uniform6',
'distance7',
'uniform7',
'distance8',
'uniform8',
'distance9',
'uniform9',
'distance10',
'uniform10',
'distance11',
'uniform11',
'distance12',
'uniform12',
'distance13',
'uniform13']
knn演算法 鳶尾花
knn分類演算法實現鳶尾花 對鳶尾花利用knn演算法進行分類,利用python的sklean庫極大方便了python的使用,減少了大量 的書寫。knn演算法的 是通過計算歐氏距離來確定 鳶尾花的結果,遍歷所有樣本集,計算待分類樣本與每個樣本的距離,計算並儲存後排序,尋找k個近鄰,最後輸出最小結果即為...
knn實現鳶尾花分類
依照歐氏距離計算公式計算測試資料與訓練集的距離 按照距離公升序排序,選取距離最小的k個點 確定前k個點所在類別的出現頻率 返回前k個點出現頻率最高的類作為測試資料的 分類 import operator import numpy as np from sklearn.datasets import ...
KNN實現鳶尾花分類
將其按照7 3的比例劃分為訓練集和測試集。前4列為特徵,第5列為類別,setosa 視為1,versicolor 視為2,virginica 視為3 測試集順序略有調整。最簡單最初級的分類器是將全部的訓練資料所對應的類別都記錄下來,當測試物件的屬性和某個訓練物件的屬性完全匹配時,便可以對其進行分類。...