Calculating cumulative minimum with numpy arrays
arrays, numpy, python
Solution
For any 2-argument NumPy universal function, its `accumulate` method is the cumulative version of that function. Thus, `numpy.minimum.accumulate` is what you're looking for:
>>> numpy.minimum.accumulate([5,4,6,10,3])
array([5, 4, 4, 4, 3])
Problem
I'd like to calculate the "cumulative minimum" array--basically, the minimum value of an array up to each index such as: ``` import numpy as np nums = np.array([5.,3.,4.,2.,1.,1.,2.,0.]) cumulative_min = np.zeros(nums.size, dtype=float) for i,num in enumerate(nums): cumulative_min[i] = np.min(nums[0:i+1]) ``` This works (it returns the correct array([ 5., 3., 3., 2., 1., 1., 1., 0.]) ), but I'd like to avoid the for loop if I can. I thought it might be faster to construct a 2-d array and use the np.amin() function, but I needed a loop for that as well.