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 of a equal number of true in b

output:

  • int numpy array: c = [1, 1, 7, 1000, 1000, 1500, 1500, 1500] length of c same length of b

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.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo