python-numpy

来源:互联网 发布:百度云域名转出 编辑:程序博客网 时间:2024/05/18 06:28

1.sum

a

1, 2    |   axis 13, 4    |--------| axis 0 

a.sum(axis=0)
array([4, 6])
a.sum(axis=1)
array([3, 7])

2. np.square

每个元素

3. 加维度

a=[1,2]
aa=a[:,None]
aa.shape=(2,1)

b=[1,2,3]
bb=b[None,:]
bb.shape=(1,3)

4. 两个基底构成矩阵

aa+bb

       1,2,3       -------   1 | 2,3,4   2 | 3,4,5

5. 计算距离矩阵(norm2, cosine)

a : array_like    An NxM matrix of N samples of dimensionality M.b : array_like    An LxM matrix of L samples of dimensionality M.Returns a matrix of size len(a), len(b), ret[i][j]    contains the squared distance between `a[i]` and `b[j]`.
def norm2_mat(a,b):    a2,b2=np.square(a).sum(axis=1),np.square(b).sum(axis=1)    r2=a2[:,None]+b2[None,:] -2*np.dot(a,b.T)    return r2

等价于

def norm2(a,b):    n,m=a.shape    l,m=b.shape    ret=np.zeros((n,l),dtype=np.float)    for i in range(n):        for j in range(l):            ret[i][j]=np.square(a[i]-b[j]).sum()    return ret

cosine_mat

def _cosine_distance(a, b, data_is_normalized=False):
if not data_is_normalized:
a = np.asarray(a) / np.linalg.norm(a, axis=1, keepdims=True)
b = np.asarray(b) / np.linalg.norm(b, axis=1, keepdims=True)
return 1. - np.dot(a, b.T)

原创粉丝点击