机器学习之-最邻近算法(Nearest Neighbor)

1.理论基础

测试图片(test image)与训练图片(training image)每个对应相同位置像素值之差的绝对值,然后求和。具体如下图所示:
这里写图片描述

2.代码

""" In this code, the parameter "X" in function "train" is different from "X" in function "predict" """
import numpy as np

class NearestNeighbor:
    def __init__(self):
        pass

    def train(self,X,y):
        """ X is N*D where each row is an example. Y is 1-dimension of size N"""
        # the nearest neighbor classifier simply remembers all the training data
        self.Xtr = X
        self.ytr = y

    def predict(self,X):
        """ X is N*D where each row is an example we wish to predict label for """
        num_test = X.shape[0]
        # lets make sure that the output type matches the input type
        Ypred = np.zeros(num_test,dtype=self.ytr.dtype)

        # loop over all test rows
        for i in xrange(num_test):
            # find the nearest training image to i'th test image
            # using the L1 distance (sum of absolute value differences)
            distances = np.sum(np.abs(self.Xtr - X[i,:]),axis=1) # get the index with smallest distance
            min_index = np.argmin(distances)
            Ypred = self.ytr[min_index] # predict the label of the nearest example

        return Ypred
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值