Reputation: 452
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
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