1

I have data that looks like the following:

x = np.array([('joe', [1, 3, 5, 7, 9, 3, 2, 4]),\
              ('sally', [7, 3, 5, 1, 4, 3, 2, 4])],\
              dtype=np.dtype([('name', 'a10'), ('scores', (float, 8))]))

and I want to replace values in the 'scores' column with something like the result from np.maximum.accumulate(). If I had a numpy array y with values:

[1, 3, 5, 7, 9, 3, 2, 4]

I get output like:

[1 3 5 7 9 9 9 9]

but I can't seem to get this to work on numpy arrays that have complex or custom data types. I assume this has something to do with creating views vs. copies of the data, but I can't seem to figure it out.

I've tried several different methods:

x['scores'] = np.maximum.accumulate(x['scores'])

and

for i, score in enumerate(x):
   x[i]['scores'] = np.maximum.accumulate(x[i]['scores'])

but neither seem to be able to replace the values in place. Any suggestions for how I can accomplish this would be greatly appreciate. Thanks!

1 Answer 1

2

It's not the assignment, it's np.maximum.accumulate which isn't returning what you think:

>>> np.maximum.accumulate(x["scores"])
array([[ 1.,  3.,  5.,  7.,  9.,  3.,  2.,  4.],
       [ 7.,  3.,  5.,  7.,  9.,  3.,  2.,  4.]])

If you specify axis=1, though:

>>> np.maximum.accumulate(x["scores"], axis=1)
array([[ 1.,  3.,  5.,  7.,  9.,  9.,  9.,  9.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.]])
>>> x["scores"] = np.maximum.accumulate(x["scores"], axis=1)
>>> x
array([('joe', [1.0, 3.0, 5.0, 7.0, 9.0, 9.0, 9.0, 9.0]),
       ('sally', [7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0])], 
      dtype=[('name', 'S10'), ('scores', '<f8', (8,))])

PS: when you're working with named columns, using pandas makes life much more convenient than bare numpy, and I recommend it heartily.

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

2 Comments

Thank you! I was so very confused as to why none of the assignment methods I was trying were working. Is it actually doing anything in the default axis = 0 case then?
@notlink: yep, it's taking the maximum along axis=0. :^) Compare [7,3,5,7,9,3,2,4] with [7,3,5,1,4,3,2,4].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.