[Numpy-discussion] Boolean indexing

Sam Tygier Sam.Tygier@hep.manchester.ac...
Tue Mar 8 07:59:32 CST 2011


I am having an  issue with boolean slicing. it seems to work fine for
reading a value, but I can use it to set a value:

import numpy
b =  numpy.array([[1,2],[3,4],[5,6],[7,8],[9,10]])
m = numpy.array([0,1,0,0,0], dtype=bool)
print b[m]
print b[m][0,0]
b[m][0,0] = -1
print b[m][0,0]

I think the boolean slicing is making a copy instead of a view. is there
a way around this? 

My full problem is to modify a column of a rec array, if another column
has a condition. ie, a neat way of doing

for n in xrange(len(a)):
  if a[n]['name'] == "foo":
    a[n]['x'] += 1

i have been trying things like:
a[a[n]['name'] == "foo"]['x'] += 1
but does not work.

I am using
Python version 2.7 (r27:82500, Sep 16 2010, 18:02:00)
numpy 1:1.4.1-6.fc14 (fedora package)



More information about the NumPy-Discussion mailing list