cf2
cf2

Reputation: 591

Calculate percentile per set interval in python array

Simple question but I cannot seem to get it to work. I have an array with shape(2000, 1). From this array I want to calculate the 99th percentile per 40 rows and return a Boolean array of the same length where everything above the 99th percentile is true and the rest false. For the first 40 I can do:

a = np.random.rand(2000,1)
maxper40 = a[0:40] > np.percentile(a[0:40], 99)

I can do something like this to get a moving window:

windowSize = 40
for i in range(0,len(a)-windowSize+1):
    print max(a[i:i+windowSize])

How would I move through the array with an interval of 40, calculate the 99 percentile per interval and return a Boolean array of the same length as the input?

Many thanks!

Upvotes: 1

Views: 531

Answers (1)

user2285236
user2285236

Reputation:

You can reshape your array by 50x40 and get the percentile of each:

a.reshape(50, 40) > np.percentile(a.reshape(50, 40), 99, axis=0)

This will return an array of size (50, 40). If you want a 1D array you can call flatten afterwards:

(a.reshape(50, 40) > np.percentile(a.reshape(50, 40), 99, axis=0)).flatten()

Upvotes: 2

Related Questions