hyperboreean
hyperboreean

Reputation: 8333

Statistics with numpy

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?

Upvotes: 1

Views: 1867

Answers (4)

David Z
David Z

Reputation: 131600

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

Upvotes: 2

rz.
rz.

Reputation: 20037

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

Upvotes: 0

Pesto
Pesto

Reputation: 23900

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)]

Upvotes: 1

dF.
dF.

Reputation: 75785

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

Upvotes: 8

Related Questions