0

Convert this implementation to a more efficient usage of numpy

Essentially for every instance D[i] <= num, I want to use that index to add numpy x[i] to nx and numpy y[i] to ny it to the new numpy array nx, ny.

row, = D.shape
for i in range(row):
    if D[i] <= num:
        nx.append(x[i])
        ny.append(y[i])

2 Answers 2

4

You can use masking -

mask = D<=num
nx, ny = x[mask], y[mask]
Sign up to request clarification or add additional context in comments.

Comments

2

You can use np.where

w = np.where(D <= num)[0]
nx, ny = x[w], y[w]

demo

D = np.arange(10)
x = np.arange(10)
y = np.arange(10)[::-1]
num = 4

w = np.where(D <= num)[0]
nx, ny = x[w], y[w]

print(nx, ny)

[0 1 2 3 4] [9 8 7 6 5]

You can also use np.flatnonzero to the same effect.

D = np.arange(10)
x = np.arange(10)
y = np.arange(10)[::-1]
num = 4

w = np.flatnonzero(D <= num)
nx, ny = x[w], y[w]

print(nx, ny)

[0 1 2 3 4] [9 8 7 6 5]

naive time testing
with large-ish data

k = 100000
D = np.arange(k)
x = np.arange(k)
y = np.arange(k)[::-1]
num = k // 2

enter image description here

2 Comments

There's no real reason to convert the mask to indices, whether with where, flatnonzero, or some other tool.
@user2357112 that's what I'm learning. My simple test demonstrates that the mask is quicker. I am realizing via attempting to answer this question what you have just stated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.