day4 python学习笔记

【numpy  tile】

np.tile([1,1],(3,2))
Out[161]: 
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])

np.tile([1,1],(row,column)) : 将[1,1] 在行方向重复三次,列方向重复2次

【numpy sum】

np.sum(a,axis=0): axis=0 按行加 axis=1 按列加

【error】
np.zeros(3,2)

TypeError: data type not understood :  

np.zeros([3,2])

【List array matrix 的区别】
https://blog.csdn.net/u011730199/article/details/78202016
【范数】

np.linalg.norm(a)

【欧氏距离】
用范数算:

for i in range(n):
      for j in range(t):
           a=xtrain[i,:]-xtest[j,:]
           distance[i,j]=np.linalg.norm(a)
用平方和公式算:(结果为开根号前)
def euclidean_dist_squared(X, Xtest):
   return np.sum(X**2, axis=1)[:,None] + np.sum(Xtest**2, axis=1)[None] - 2 * np.dot(X,Xtest.T)

【sklearn   datasets】

train error is 0.012369
test error is 0.016667

猜你喜欢

转载自blog.csdn.net/weixin_41632154/article/details/80221024