0

I have a large two dimentional numpy array A of dimension around 250000 x 30 and two one dimensional numpy arrays x and y. I want to extract the sub-array of A with rows in x and columns in y. Which method is more efficient?

  1. A[x[:,np.newaxis], y]
  2. A[np.ix_(x,y)]

1 Answer 1

2

Benchmark it!

import numpy as np

# some data
A = np.random.random((250000, 30))

# some random indices
x = np.random.randint(0, 250000, 150000)
y = np.random.randint(0, 30, 10)

def method1(A, x, y):
    return A[x[:, np.newaxis], y]

def method2(A, x, y):
    return A[np.ix_(x,y)]

def method3(A, x, y):
    return A[x][:,y]

def method4(A, x, y):
    return A[:,y][x]

These three methods give the following benchmarks:

method1: 87.7 ms
method2: 89.2 ms
method3: 115 ms
method4: 141 ms

So, the answer is that there is not real difference between the two methods in the question.

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.