eng3
eng3

Reputation: 452

numpy sort top bottom

I am trying to pull the top 5%, bottom 5% and the remaining out into separate arrays and save the average. My code is below.

    tg = 48000000 element float array
    tg.sort()
    pct = int(tg.size*0.05)
    high5 = tg[-pct:].mean()
    low5 = tg[:pct].mean()
    mid90 = tg[pct:-pct].mean()

I'd appreciate any suggestions on how to speed this up.

Upvotes: 2

Views: 304

Answers (1)

nullop
nullop

Reputation: 554

Actually you don't need to fully sort your array. You could just use partition method:

tg = 48000000 element float array    
pct = int(tg.size*0.05)

tg.partition([pct, tg.size - pct]) 

mean_low5 = tg[:pct].mean()
mean_high5 = tg[-pct:].mean()
mean_mid90 = tg[pct:-pct].mean()

(code was updated according to Jaime' comment)

Upvotes: 2

Related Questions