using masks and numpy record arrays

C

Catherine Moroney

Hello,

I am trying to work with a structured array and a mask, and am
encountering some problems.

For example:
xtype = numpy.dtype([("n", numpy.int32), ("x", numpy.float32)])
a = numpy.zeros((4), dtype=xtype)
b = numpy.arange(0,4)
a2 = numpy.zeros((4), dtype=xtype)
mask = numpy.where(b%2 == 0)
a2[:]["n"] += b ! this changes the values of a2
a[mask]["n"] += b[mask] ! this does not change the values of a
a2
array([(0, 0.0), (1, 0.0), (2, 0.0), (3, 0.0)],
dtype=[('n', '<i4'), ('x', '<f4')])array([(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
dtype=[('n', '<i4'), ('x', '<f4')])

Why do the values of a2 get updated, and yet the values of a do not?
How do I update a's contents using a mask?

If "a" is not a record array, but instead simply an array of ints, then
the a[mask] += b[mask] statement does alter the values of a.
a = numpy.zeros((4))
a[mask] += b[mask]
a
array([ 0., 0., 2., 0.])

What is it about a numpy record array that prevents the mask statement
from working, and how do I get around this?

Thanks,

Catherine
 
R

Robert Kern

Hello,

I am trying to work with a structured array and a mask, and am encountering some
problems.

You will want to ask numpy questions on the numpy mailing list:

http://www.scipy.org/Mailing_Lists
For example:
xtype = numpy.dtype([("n", numpy.int32), ("x", numpy.float32)])
a = numpy.zeros((4), dtype=xtype)
b = numpy.arange(0,4)
a2 = numpy.zeros((4), dtype=xtype)
mask = numpy.where(b%2 == 0)
a2[:]["n"] += b ! this changes the values of a2
a[mask]["n"] += b[mask] ! this does not change the values of a
a2
array([(0, 0.0), (1, 0.0), (2, 0.0), (3, 0.0)],
dtype=[('n', '<i4'), ('x', '<f4')])array([(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
dtype=[('n', '<i4'), ('x', '<f4')])

Why do the values of a2 get updated, and yet the values of a do not?

Only the final [] on the left-hand side of the assignment actually turns into a
..__setitem__() call to the object that is the result of the expression to its
left. a[mask] makes a copy while a2[:] makes a view.

You could do

a["n"][mask] += b[mask]

since a["n"] will also make a view and the .__setitem__() on it will propagate
back to the original memory.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top