Issue
I need to get the cumulative maximum of array longband
until its [i-1]
value is >=
of RsiMa[i]
, then turn to its "real" [i]
value and restart cumulating.
Actually the only way I found to achieve this is by a for loop.
Is it translatable to numpy?
How can I convert it?
THE CODE:
import numpy as np
RsiMa = np.array([ 2, 4, 6, 9, 10, 8, 6, 4, 6, 8, 10, 12, 10, 8])
longband = np.array([1, 3, 2, 8, 7, 5, 4, 3, 4, 3, 7, 10, 7, 6])
for i in range(1, len(longband)):
if i > 1:
if longband[i-1] > longband[i] and longband[i-1] < RsiMa[i]:
longband[i] = longband[i-1]
EXPECTED OUTPUT:
[ 1 3 3 8 8 5 5 3 4 4 7 10 7 7 ]
I already tried with np.fmax.accumulate() but seems like it can't be restarted when longband[i] >= RsiMa[i]
...
EDIT:
I tried also with numba jit:
@numba.jit(nopython=True)
def loop(array1, arrayfilt):
for i in range(2, len(array1)):
if array1[i-1] > array1[i] and array1[i-1] <= arrayfilt[i]:
array1[i] = array1[i-1]
return array1
longband = loop(longband, RsiMa)
but IDK why, instead of accelerating it, it cause a slowdown from 15ms of the simple loop to 33ms of jit...
Solution
This method do not prodice any improvement, but at least works:
### vector function ###
def vect(array1, array2, counter):
if (array1[counter-1] > array1[counter] and array1[counter-1] < array2):
array1[counter] = array1[counter-1]
return array1[counter]
vvect = np.vectorize(vect, excluded=['array1'])
longband = vvect(array1=longband, array2=RsiMa, counter=np.arange(0, len(longband)))
OUTPUT: [ 1 3 3 8 8 5 5 3 4 4 7 10 7 7 ]
Answered By - Francesco Battisti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.