14

One may select elements in numpy arrays as follows

a = np.random.rand(100)
sel = a > 0.5 #select elements that are greater than 0.5
a[sel] = 0 #do something with the selection

b = np.array(list('abc abc abc'))
b[b==a] = 'A' #convert all the a's to A's

This property is used by the np.where function to retrive indices:

indices = np.where(a>0.9)

What I would like to do is to be able to use regular expressions in such element selection. For example, if I want to select elements from b above that match the [Aab] regexp, I need to write the following code:

regexp = '[Ab]'
selection = np.array([bool(re.search(regexp, element)) for element in b])

This looks too verbouse for me. Is there any shorter and more elegant way to do this?

2
  • You many have seen this docs.scipy.org/doc/numpy/reference/generated/… , which would be useful if your starting point was a string or a file instead of an array. Commented Jul 6, 2011 at 13:31
  • Paul, thank you. Yes I saw this (this is the first google hit for "numpy regexp"), but this doesn't solve my problem Commented Jul 6, 2011 at 19:57

1 Answer 1

22

There's some setup involved here, but unless numpy has some kind of direct support for regular expressions that I don't know about, then this is the most "numpytonic" solution. It tries to make iteration over the array more efficient than standard python iteration.

import numpy as np
import re

r = re.compile('[Ab]')
vmatch = np.vectorize(lambda x:bool(r.match(x)))

A = np.array(list('abc abc abc'))
sel = vmatch(A)
Sign up to request clarification or add additional context in comments.

2 Comments

from numpy docs: The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.
Link to documentation concerning the performance of vectorize.

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.