機器學習KNN 2

2021-10-04 16:24:14 字數 2729 閱讀 2417

from sklearn.neighbors import kneighborsclassifier

from math import sqrt

import os

import pandas as pd

import numpy as np

from collections import counter

path=os.getcwd()+

'\\'

+'8c859681-527c-4d9b-a98a-f00e6169931f_.xls'

x1=pd.read_excel(path)

x1=np.array(x1)

x=x1[:,

:2]row_data_x=x

row_data_y=

for i in

range

(len

(x))

:if i<=

len(x)//2

:0)else:1

)row_data_y=np.array(row_data_y)

#訓練集

x_train=np.array(row_data_x)

y_train=np.array(row_data_y)

x=x1[:,

:2]knn_classfier=kneighborsclassifier(n_neighbors=6)

knn_classfier.fit(x_train,y_train)

x_predict=x

y_predict=knn_classfier.predict(x_predict)

class

knnclassfier

:def

__init__

(self,k)

:'''初始化knn分類器'''

assert k>=1,

"k must be valid"

self.k=k

self._x_train=

none

self._y_train=

none

deffit

(self,x_train,y_train)

:'''根據訓練資料集x_train和y_train訓練knn分類器'''

assert x_train.shape[0]

==y_train.shape[0]

,\ "the size of x_train must be equal to the size of train"

assert self.k<=x_train.shape[0]

,\ "the size of x_train must be at least l."

self._x_train=x_train

self._y_train=y_train

return self

defpredict

(self,x_predict)

:'''給定帶**資料集x)predict,返回表示x_predict的結果向量'''

assert self._x_train is

notnone

and self._y_train is

notnone

,\ "must fit before predict"

assert x_predict.shape[1]

==self._x_train.shape[1]

,\ "the feature number of x_predict must be equal to x_train"

y_predict=

[self._predict(x)

for x in x_predict]

return np.array(y_predict)

def_predict

(self,x)

:'''給定單個帶**資料x,返回x的**結果值'''

assert x.shape[0]

==self._x_train.shape[1]

, \ "must feature number of x must be equal to x_train"

distances=

[sqrt(np.

sum(

(x_train-x)**2

))for x_train in self._x_train]

nearest=np.argsort(distances)

topk_y=

[self._y_train[i]

for i in nearest[

:self.k]

] votes=counter(topk_y)

return votes.most_common(1)

[0][

0]def__repr__

(self)

:return

"knn(k=%d"

%self.k

if __name__==

'__main__'

: knn_clf= knnclassfier(k=6)

knn_clf.fit(x_train,y_train)

x_predict=x

y_predict=knn_clf.predict(x_predict)

print

(y_predict)

深度學習 學習筆記 之最鄰近演算法KNN(2)

1 cover和hart在1968年提出了最初的鄰近演算法 2 knn 演算法的核心思想是如果乙個樣本在特徵空間中的k個最相鄰的樣本中的大多數屬於某乙個類別,則該樣本也屬於這個類別,並具有這個類別上樣本的特性。3 類別決策時,只與極少量的相鄰樣本有關。4 距離的測量方法 euclidean dist...

機器學習之Knn演算法2

寫了knn演算法以後,再寫乙個二,一的連線 coding utf 8 import math import random import operator import csv import numpy as np defloaddataset filename,split,trainingset t...

機器學習 KNN

posted on 2015 05 21 20 34 in ml 0 comment views 3195 原始碼 決策樹和基於規則的分類器都是積極學習方法 eager learner 的例子,因為一旦訓練資料可用,他們就開始學習從輸入屬性到類標號的對映模型。乙個相反的策略是推遲對訓練資料的建模,直...