Calculation/manipulation of numpy array
<p>Looking to make the this calculation as quickly as possible. I have X as n x m numpy array. I want to define Y to be the following:</p>

<p><code>Y_11 = 1 / (exp(X_11-X_11) + exp(X_11-X_12) + ... exp(X_11 - X_1N) ).</code></p>

<p>or for Y_00</p>

<p><code>1/np.sum(np.exp(X[0,0]-X[0,:]))</code></p>

<p>So basically, Y is also n x m where the i,j element is the 1 / sum_j' exp(X_ij - X_ij') </p>

<p>Any tips would be great! Thanks.</p>

<p><em>Sample code as requested:</em></p>

<pre><code>np.random.seed(111)
J,K = 110,120
X = np.random.rand(J,K)
Y = np.zeros((J,K))
for j in range(J):
    for k in range(K):
        Y[j,k] = 1/np.sum(np.exp(X[j,k]-X[j,:]))

# note each row will sum to 1 under this operation
np.sum(Y,axis=1)
</code></pre>

<p>Here's a first step at reducing the double loop:</p>

<pre><code>def foo2(X):
    Y = np.zeros_like(X)
    for k in range(X.shape[1]):
        Y[:,k]=1/np.exp(X[:,[k]]-X[:,:]).sum(axis=1)
    return Y
</code></pre>

<p>I suspect I can also remove the <code>k</code> loop, but I have to spend more time figuring out just what it is doing.  That <code>X[:,[k]]-X[:,:]</code> isn't obvious (in the big picture).</p>

<p>Another step:</p>

<pre><code>Z = np.stack([X[:,[k]]-X for k in range(X.shape[1])],2)
Y = 1/np.exp(Z).sum(axis=1)
</code></pre>

<p>which can be further refined (with too much trial and error) with</p>

<pre><code>Z = X[:,None,:]-X[:,:,None]
</code></pre>

python performance numpy sum numpy-einsum