python-numpy

1.sum

a

1, 2    |   axis 1
3, 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值