2

I want to represent the columns of one 2d array as the sum of the subset of the columns of another matrix. What is the most efficient way ?.

Right now, what I does is,

for i in xrange(U.shape[1])
    U[:,i] = X[:,np.random.choice(X.shape[1], 10)].sum(axis=1)/10.0;

Is there a faster and better non-loop method ?

1 Answer 1

2

Generate all indices in one go, index into input 2D array to give us a 3D array and finally sum along the last axis axis=2, like so -

indx = np.random.randint(0,X.shape[1], (U.shape[1],10))
Uout = X[:,indx].sum(axis=2)/10.0

If you want to use np.random.choice to get indx -

np.random.choice(X.shape[1], size=(U.shape[1],10))

Indexing with np.take seems faster for large arrays -

In [63]: X = np.random.rand(1000,1000)

In [64]: indx = np.random.randint(0,1000, (1000,10))

In [67]: %timeit X[:,indx]
10 loops, best of 3: 69.9 ms per loop

In [68]: %timeit np.take(X, indx, axis=1)
10 loops, best of 3: 22.9 ms per loop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.