1

My problem is the following. I have a numpy array (it can be 1D for the sake of simplicity) of floats. I have to compare one element with the previous and if the element is lower than the previous, substitute it. This has to be done accumulating the maximum value.

I've been doing it with a loop (I think it also makes the question more clear) in the following way.

import numpy as np

a = np.random.random(100) # The original array
accum = 0.0
for i in range(1,len(a)):
    if a[i] < accum:
        a[i] = accum
    else:
        accum = a[i]

I was wondering if this could be done with some kind of array operation, similar to np.diff or np.cumsum.

2
  • I appreciate your being concrete, but I'm a bit worried your example isn't telling enough. Can you give a couple examples showing the input and output you want? May 15, 2013 at 14:26
  • Please, check DSM's answer. This is what I really wanted. Sorry for my bad explanation and thanks for your interest. May 15, 2013 at 14:41

1 Answer 1

4

Your code doesn't seem to match your description. The code right now will leave a = [3,2] untouched, but turn [3,2,1] into [3, 2, 2], which seems a little strange, and setting accum = 0 means negative numbers won't behave the same way.

[I've just noticed that one of your earlier questions was about Matlab, which starts counting from 1. Python is zero-indexed and so the first element is a[0], so maybe this was unintended?]

The more usual version can be computed using np.maximum.accumulate, for example:

>>> a
array([6, 0, 7, 9, 9, 6, 9, 5, 1, 8])
>>> your_code(a)
array([6, 0, 7, 9, 9, 9, 9, 9, 9, 9])
>>> np.maximum.accumulate(a)
array([6, 6, 7, 9, 9, 9, 9, 9, 9, 9])

If you really need your original behaviour, then maybe you could patch the first value to zero, call np.maximum.accumulate, and then reinsert a[0]. Something like that should work, anyhow.


[update]

This will propagate nan:

>>> a
array([  2.,   1.,  nan,   3.,   4.,   1.])
>>> np.maximum.accumulate(a)
array([  2.,   2.,  nan,  nan,  nan,  nan])

If you want these to be filled, you could use fmax instead:

>>> np.fmax.accumulate(a)
array([ 2.,  2.,  2.,  3.,  4.,  4.])

Or use np.nan_to_num() to set the nan values to zero:

>>> np.maximum.accumulate(np.nan_to_num(a))
array([ 2.,  2.,  2.,  3.,  4.,  4.])

and then you could use a cheap hack to restore the nan values if you wanted:

>>> np.fmax.accumulate(a) + (a*0)
array([  2.,   2.,  nan,   3.,   4.,   4.])

(You could also look into masked arrays, but I don't use them very often as they're typically overkill for my needs. Other people find them really useful, though.)

3
  • Thank you very much! This is working just as I wanted!Perhaps I explained myself a little bit bad... All values are between 0 and 1, that's why I set the accumulator to one. I tried it also in a 2D array (the real dataset) and I can perform this along the desired axis very easily. As an extra... ¿Could this be done with an array that includes nans? May 15, 2013 at 14:38
  • @IñigoHernáezCorres: you didn't say how you wanted the system to behave when nans were involved, so I took a few guesses.
    – DSM
    May 15, 2013 at 14:55
  • This is perfect, using fmax fills nans, which is exactly what I expected. May 16, 2013 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.