2009-02-20 122 views
1

我在一些情节和统计工作中工作,我不知道如何使用numpy做一些统计信息:我有一个价格列表和另一个basePrices。我想知道有多少价格高于basePrice的百分之X,有多少百分比高于basePrice的Y百分比。numpy的统计信息

有没有简单的方法来做到这一点使用numpy?

回答

7

说你有

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

然后价格是基础价格之上10%以上是

>>> sum(prices > 1.10 * base_prices) 
2 
+0

哦,我觉得还有一些比这更复杂的东西。看起来不错。谢谢,dF! – hyperboreean 2009-02-20 16:17:35

+1

你应该接受这个答案。 – 2011-09-30 15:30:20

1

除了DF的答案,如果你想知道具体的数目价格高于基准价格,你可以这样做:

价格[价格>(1.10 * base_prices)]

0

我不认为你需要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 
2

只是为了取乐,这里有一个稍微不同的看法上DF的回答是:

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

然后你就可以提取高于5%的数量,10%以上等

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