More efficient way of computing distance matrix in Python
<p>Hi Everyone I am trying to write code (using python 2) that returns a matrix that contains the distance between all pairs of rows. Below is an implementation that I have written. It works as expected but can get very slow as the number of rows gets large. Hence I was wondering if anyone has any suggestions as to how the code can be made more efficient for large number of rows.</p>

<p>Thanks in advance</p>

<pre><code>def gendist(x,alpha=2):
    (n,p) = x.shape
    len = 0
    for ii in range(1,n):
        len = len + ii
    d = np.empty((len,p))
    ind = 0
    for ii in range(0,n):
        for jj in range(1,n):
            if ii &lt; jj:
                d[ind,] = (x[ii,]-x[jj,])**alpha
                ind = ind + 1
    return d
</code></pre>

<p>Without scipy (it is possible to get numpy without scipy, for instance with an Abaqus install)  it's a bit more difficult.</p>

<pre><code>def gendist(x,alpha=2):
    xCopies=x.repeat(x.shape[0],axis=0).reshape(np.conatenate(([a.shape[0]],a.shape))
    #n x n x p matrix filled with copies of x
    xVecs=xCopies-xCopies.swapaxes(0,1) #matrix of distance vectors
    xDists=np.sum(xVecs**alpha,axis=-1)**(1/alpha) #n x n matrix of distances
    Return xDists
</code></pre>

<p>That should be robust, at least it's what I had to use.</p>

python performance distance-matrix