kNN - k near algorithm

This article comes from liuyubobobo "classical algorithms and machine learning applications Python3 Getting Started"

k near algorithm over the interpretation, by convention, or should be given the official definition of k neighboring algorithm, I copied from Baidu Encyclopedia coming

K-nearest neighbor algorithm: in the feature space, if a majority of sample k nearest vicinity of (ie, the most feature space adjacent to) the sample belongs to a class, the sample also fall into this category.

The following step by step to achieve k near the algorithm in Jupyter Notebook

In my github can download notebook file

https://github.com/CodingSoldier/python-learn/blob/master/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/notebook/04-knn-k%E8%BF%91%E9%82%BB%E7%AE%97%E6%B3%95/01-knn%E5%AE%9E%E7%8E%B0-%E6%95%99%E7%A8%8B.ipynb

knn.py code is as follows:

# -*- coding: utf-8 -*-

import numpy as np
from math import sqrt
from collections import Counter

def knn_classify(k, X_train, y_train, x):

    assert 1 <= k <= X_train.shape[0], "k要大于等于1,小于等于数组X_train第一维大小"
    assert X_train.shape[0] == y_train.shape[0], "数组X_train第一维大小要等于数组y_train第一维大小"
    assert X_train.shape[1] == x.shape[0], "数组X_train第二维大小要等于预测点x第一维大小"

    distances = [sqrt(np.sum((dot -x)**2)) for dot in X_train]
    nearest = np.argsort(distances)

    top_k_y = [y_train[i] for i in nearest[:k]]
    votes = Counter(top_k_y)

    return votes.most_common(1)[0][0]

 

 

 

Published 51 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/u010606397/article/details/102917733