Numpy Broadcast to perform euclidean distance vectorized

后端 未结 5 2122
无人共我
无人共我 2020-12-04 14:44

I have matrices that are 2 x 4 and 3 x 4. I want to find the euclidean distance across rows, and get a 2 x 3 matrix at the end. Here is the code with one for loop that compu

5条回答
  •  不知归路
    2020-12-04 15:34

    I had the same problem recently working with deep learning(stanford cs231n,Assignment1),but when I used

     np.sqrt((np.square(a[:,np.newaxis]-b).sum(axis=2)))
    

    There was a error

    MemoryError
    

    That means I ran out of memory(In fact,that produced a array of 500*5000*1024 in the middle.It's so huge!)

    To prevent that error,we can use a formula to simplify:

    code:

    import numpy as np
    aSumSquare = np.sum(np.square(a),axis=1);
    bSumSquare = np.sum(np.square(b),axis=1);
    mul = np.dot(a,b.T);
    dists = np.sqrt(aSumSquare[:,np.newaxis]+bSumSquare-2*mul)
    

提交回复
热议问题