1

I'm interested in the performance of NumPy, when it comes to algorithms that check whether a condition is True for an element and its affiliations (e.g. neighbouring elements) and assign a value according to the condition.

An example may be: (I make this up now)

  • I generate a 2d array of 1's and 0's, randomly.
    • Then I check whether the first element of the array is the same with its neighbors.
    • If the similar ones are the majority, I switch (0 -> 1 or 1 -> 0) that particular element.
  • And I proceed to the next element.

I guess that this kind of element wise conditions and element-wise operations are pretty slow with NumPy, is there a way that I can make the performance better?

For example, would creating the array with type dbool and adjusting the code, would it help?

Thanks in advance.

2 Answers 2

1

Maybe http://www.scipy.org/Cookbook/GameOfLifeStrides helps you.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like your are doing some kind of image processing, you can try scipy.ndimage.

from scipy.ndimage import convolve
import numpy as np
np.random.seed(0)
x = np.random.randint(0,2,(5,5))

print x

w = np.ones((3,3), dtype=np.int8)
w[1,1] = 0
y = convolve(x, w, mode="constant")

print y

the outputs are:

[[0 1 1 0 1]
 [1 1 1 1 1]
 [1 0 0 1 0]
 [0 0 0 0 1]
 [0 1 1 0 0]]

[[3 4 4 5 2]
 [3 5 5 5 3]
 [2 4 4 4 4]
 [2 3 3 3 1]
 [1 1 1 2 1]]

y is the sum of the neighbors of every element. Do the same convolve with all ones, you get the number of neighbors number of every element:

>>> n = convolve(np.ones((5,5),np.int8), w, mode="constant")
>>> n
[[3 5 5 5 3]
 [5 8 8 8 5]
 [5 8 8 8 5]
 [5 8 8 8 5]
 [3 5 5 5 3]]

then you can do element-wise operations with x, y, n, and get your result.

2 Comments

Yes, that is a slick answer to my made-up case. But it was rather a general question, I didn't know that this could have been done with a method convolve. My question is more or less about an arbitrary condition and an arbitrary elementwise operation.
It isn't a common solution, if you want do some operation that can only be calculated by element-wise for loop, you can build an extention module by cython, SWIG, or embed c code directly into you python code by weave. But before doing that, try image processing methods first, such as convolve, morphology image process.

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.