tags:

views:

518

answers:

4

I am working at some plots and statistics for work and I am not sure how I can do some statistics using numpy: I have a list of prices and another one of basePrices. And I want to know how many prices are with X percent above basePrice, how many are with Y percent above basePrice.

Is there a simple way to do that using numpy?

+4  A: 

Say you have

>>> prices = array([100, 200, 150, 145, 300])
>>> base_prices = array([90, 220, 100, 350, 350])

Then the number of prices that are more than 10% above the base price are

>>> sum(prices > 1.10 * base_prices)
2
dF
Oh, I thought that there's something more complicated than that. Looks good enough. Thanks, dF!
hyperboreean
A: 

In addition to df's answer, if you want to know the specific prices that are above the base prices, you can do:

prices[prices > (1.10 * base_prices)]

Pesto
A: 

I don't think you need numpy ...

prices = [40.0, 150.0, 35.0, 65.0, 90.0]
baseprices = [45.0, 130.0, 40.0, 80.0, 100.0]
x = .1
y = .5

# how many are within 10%
len([p for p,bp in zip(prices,baseprices) if p <= (1+x)*bp]) # 1

# how many are within 50%
len([p for p,bp in zip(prices,baseprices) if p <= (1+y)*bp]) # 5
rz
Yes, you're right, I said numpy 'cause I was going to use their implementation of array which seems faster. I have about 800k prices, so I hope to get a gain of speed using that. Thanks.
hyperboreean
+1  A: 

Just for amusement, here's a slightly different take on dF's answer:

>>> prices = array([100, 200, 150, 145, 300])
>>> base_prices = array([90, 220, 100, 350, 350])
>>> ratio = prices / base_prices

Then you can extract the number that are 5% above, 10% above, etc. with

>>> sum(ratio > 1.05)
2
>>> sum(ratio > 1.10)
2
>>> sum(ratio > 1.15)
1
David Zaslavsky