KNN algorithm in Python

This is a homework of Computer Vision class(2017) of Feifei Li, CS231n, Stanford University. I prefer to focus on single task of every assignment in one blog, so I will display my work for task1 in assignment1: create a function or class for K Nearest Neighbors algorithm by python and numpy library, here is my code.


import numpy as np

class KNN_classifier:
    def __init__(self):
        pass

    def KNN_train(self, X, Y, k):
    """
    X is trainning set attributes with size N*D
    Y is training set class lable with size N*1
    k is an integer of neighbors number when do classification
    """
    self.X_train = X
    self.Y_train = Y
    self.k = k

    sample_num = self.X_train.shape[0]
    Dis_train = np.zeros(sample_num, sample_num)
    K_neighbors = np.zeros(1, self.k)
    Lable_train = np.zeros(sample_num, 1)
    Correct_train = 0
    Acc_train = 0.0
    # calculate the distance of ith sample with all samples
    for i in range(sample_num):
        for j in range(sample_num):
        Dis_train[i, j] = np.abs(self.X_train[i, :], self.X_train[j, :])
        # sort the distance of ith sample by increasing
        sort_INC_index = np.argsort(Dis_train[i, :])
        # get the K closet neighbors' lable
        for c in range(self.k):
        K_neighbors[1, c] = self.Y_train[sort_INC_index[c], 1]
        # decide ith sample's lable by it's neighbors
        Lable_train[i, 1] = np.mode(K_neighbors)
               # estimate k value by classfy accuracy
        if(Lable_train[i, 1] == self.Y_train[i, 1]):
        Correct_train += 1
    # get training set accuracy in k neighbors
    Acc_train = Correct_train / sample_num
    
    return self.k, Acc_train


    def KNN_test(self, x):
    """
    x is sample to be classified with size M*D
    """
    sample_num = x.shape[0]
    test_space = self.X_train.shape[0]
    Dis_test = np.zeros(sample_num, test_space)
    K_neighbors = np.zeros(1, self.k)
    Lable_test = np.zeros(sample_num, 1)
    # calculate the distance of ith sample in test space
    for i in range(sample_num):
        for j in range(test_space):
        Dis_train[i, j] = np.abs(x[i, :], self.X_train[j, :])
        # sort the distance of ith sample by increasing
        sort_INC_index = np.argsort(Dis_train[i, :])
        # get the K closet neighbors' lable
        for c in range(k):
        K_neighbors[1, c] = self.Y_train[sort_INC_index[c], 1]
        # decide ith sample's lable by it's neighbors
        Lable_test[i, 1] = np.mode(K_neighbors)
    
    return Lable_test
       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值