
距离公式
L1距离
d
1
(
I
1
,
I
2
)
=
∑
p
∣
I
1
p
−
I
2
p
∣
d_1(I_1,I_2) = \sum_p|I_1^p - I_2^p|
d1(I1,I2)=∑p∣I1p−I2p∣
import numpy asnp
class NearestNeighbor:
def __inti__(self):
pass
def train(self,X,y):
"""X is N x D where each row is an example. Y is 1-dimension od size N"""
# memorize training data
self.Xtr = x
self.Ytr = y
def predict(self, X):
"""X is N xD where each row is an example we wish to predice lable for """
num_test = X.shape[0]
Ypre = np.zeros(num_test, dtype = self.Yter.dtype)
# For each test image ,find cloest train image ,predict label of nearst image
for i in xrang(num_test):
# find the nearest training image to the i'th test image
distances = np.sum(np.abs(self.Xter - X[i,:]),axis=1)
min_index = np.argmin(distances)
Ypred[i] = self.Ytr[min_index]
return Ypred
上面的算法有一点不好,训练是O(1)的复杂度, 预测是O(N)的复杂度。

增加了一个投票过程,选取最接近的K个图像,然后投票,选则投票数组多的一个类别

马汉顿距离的结果和坐标轴相关,欧拉距离和与坐标轴无关





假如说每张图像都只有4个像素,又有三个类别 猫/ 狗 / 船

b 是一个偏移量,如果说我们的数据中有更多的狗,狗这一分类的 b 可能会比较大
另:w 和 b 可以组成一个 m+1 维的向量, 可以构成一个 m维空间的平面
w
0
x
0
+
w
1
x
1
+
.
.
.
+
w
m
x
m
+
b
=
0
w_0x_0 +w_1x_1+...+w_mx_m+b=0
w0x0+w1x1+...+wmxm+b=0



总的loss可以是所有样本loss的均值
多分类 SVM loss