Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Find chains of identical elements in ndarray and modify them

$
0
0

I have ndarrays like [1,1,1,1,3,3,3].

I want that, if there are consecutive elements with the same value like the case of the example, the content of the identical chain is modified in this way: we calculate delta as the 0.1% of the identical value, subtract it to the first element of the chain and add it to the last, obtaining in the example:[0.9999,1,1,1.0001,2.9997,3,3.0003].

Then I want to further modify the values inside the chain to make the distance balanced. For example there is a distance of 0.0002 between 0.9999 and 1.0001 and I have a total of 4 elements in the chain, so 0.0002/(4-1) = 0.000067. Finally I get [0.9999, 0.999967, 1.000034, 2.9997, 3, 3.0003].

How can I do this?I tried with this simple code just to modify the extremes but it doesn't work:

def modify_extremes(arr):    modified_arr = arr.astype(float)    # Find indices where consecutive elements are not equal    indices = np.where(np.diff(arr) == 0)[0] + 1    # Iterate over the found indices and modify extremes    for i in indices:        modified_arr[i - 1] += 0.01        modified_arr[i] -= 0.01    return modified_arr

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>