python - Constructing piecewise function from changepoints in Numpy -
i want construct piecewise function changepoints in python. expect inputs , outputs large, speed important.
input:
- int numpy array:
a = [1,7, 1000, 1500] - bool numpy array:
b = [true, false, true, true, false, true, false, false]length ofaequal number oftrueinb
output:
- int numpy array:
c = [1, 1, 7, 1000, 1000, 1500, 1500, 1500]length ofcsame length ofb
essentially each element of a repeated until next true in b shows in case next element of a used.
in [1]: import numpy in [2]: = numpy.array([1, 7, 1000, 1500]) in [3]: b = numpy.array([true, false, true, true, false, true, false, false]) in [4]: a[b.cumsum() - 1] out[4]: array([ 1, 1, 7, 1000, 1000, 1500, 1500, 1500]) b.cumsum() - 1 computes element of use each element of output, , a[b.cumsum() - 1] extracts elements. work out way use numpy.repeat this.