KNN演算法學習

2021-09-09 02:30:40 字數 2079 閱讀 9254

knn演算法,我的理解是在有標籤的訓練樣本分佈中,有n個需要測試的樣本,通過尤拉距離

通過計算每個測試樣本得出離訓練最近的k個樣本,同過k個樣本中某一類標籤所佔的比例最高,則將該測試值**為該標籤。

import numpy as np

from math import sqrt

from collections import counter

class knnclassifier:

def __init__(self, k):

"""初始化knn分類器"""

assert k >= 1, "k must be valid"

self.k = k

self._x_train = none

self._y_train = none

def fit(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 y_train"

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

"the size of x_train must be at least k."

self._x_train = x_train

self._y_train = y_train

return self

def predict(self, x_predict):

"""給定待**資料集x_predict,返回表示x_predict的結果向量"""

assert self._x_train is not none and self._y_train is not none, \

"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], \

"the 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]#返回乙個投票數最高的元組,其中位於第乙個最高投票數的對應的label

def score(self, x_test, y_test):

"""根據測試資料集 x_test 和 y_test 確定當前模型的準確度"""

y_predict = self.predict(x_test)

assert len(y_test) == len(y_predict), \

"the size of y_test must be equal to the size of y_predict"

return np.sum(y_test == y_predict) / len(y_test)

knn演算法學習

k近鄰近鄰演算法 多數表決規則等價於經驗風險最小化 損失函式最小化 一訓練資料為基礎,通過此類分類規則計算得到 一塊區域,有k個訓練資料,1,2,3個類別。1 2 3 當此區域判為1,損失函式等於2,3個數和 當此區域判為2,損失函式等於1,3個數和 當此區域判為3,損失函式為1,2個數和 選取損失...

KNN演算法學習筆記

概念 k近鄰 knn,k nearestneighbor 所謂k最近鄰,就是k個最近的鄰居的意思,說的是每個樣本都可以用它最接近的k個鄰居來代表。核心思想 如果乙個樣本在特徵空間中的k個最相鄰的樣本中的大多數屬於某乙個類別,則該樣本也屬於這個類別,並具有這個類別上樣本的特性。最簡單的機器學習演算法之...

演算法學習2 KNN 演算法

昨天找到乙個資料分析的眾包 kaggle 希望藉此來練習一下前段時間學習到的ml演算法。今天就來回顧一下之前學習到的 knn 演算法,並用knn演算法進行手寫數字的識別。knn演算法,即 k最近鄰 k nearestneighbor 分類演算法,是最簡單的機器學習演算法之一,演算法思想很簡單 從訓練...