http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
我有一個列表a
,我想用這樣的:
numpy.histogram(a,bins=[0.1,0.2,0.3,0.4...6], range=[0:6])
- 我怎麼包括以0.1的間隔設置0.1到6的分箱?
- 我如何指定0到6的範圍?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
我有一個列表a
,我想用這樣的:
numpy.histogram(a,bins=[0.1,0.2,0.3,0.4...6], range=[0:6])
也許你正在尋找np.linspace(0,6,num=61)
或np.arange(0,6.1,0.1)
:
import numpy as np
a=np.random.random(100)*6
hist=np.histogram(a,bins=np.linspace(0,6,num=61))
如果你確定浮點數,你可以這樣做:[x/10.0 for x in range(61)]
給你(省略中間元素)[0.0, 0.10000000000000001, 0.20000000000000001, ... 5.7000000000000002, 5.7999999999999998, 5.9000000000000004, 6.0]
否則,請參閱decimal
模塊。
range(7)
下面是一個例子:pop
包含序列0,0.01,0.02,...,作爲指定5.99,6垃圾桶1000張隨機數。您可以添加一個範圍,無論如何,在這種情況下,終點很容易。
>>> import numpy
>>> import random
>>> pop = []
>>> for i in range(1000):
... pop.extend([random.choice(range(600))/100.0])
...
>>> bins = [x/10.0 for x in range(61)]
>>> hist, bin_edges = numpy.histogram(pop, bins)
>>> bin_edges
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4,
5.5, 5.6, 5.7, 5.8, 5.9, 6. ])
>>> hist
array([20, 11, 22, 17, 25, 11, 15, 15, 13, 18, 21, 21, 16, 13, 12, 18, 16,
19, 11, 14, 15, 20, 20, 9, 13, 16, 20, 19, 23, 11, 19, 12, 21, 15,
16, 24, 24, 16, 19, 18, 10, 14, 29, 11, 16, 15, 14, 19, 11, 15, 16,
12, 17, 18, 12, 14, 27, 12, 21, 19])
謝謝你,你能不能讓我看看會是什麼樣的所有參數numpy.histogram什麼(一,箱= [0.1,0.2 ,0.3,0.4 ... 6],範圍= [0:6]) – 2010-08-03 18:01:43
已添加示例。 Poster〜ubuntu有創建隨機序列的好方法 - 我更習慣於使用隨機模塊。 – chryss 2010-08-03 18:45:27