m******t 发帖数: 273 | 1 【 以下文字转载自 Statistics 讨论区 】
发信人: myregmit (myregmit), 信区: Statistics
标 题: long time generating gamma distribution in python
发信站: BBS 未名空间站 (Thu Mar 13 19:04:24 2014, 美东)
I need to generate a truncated gamma distribution pdf curve and histogram in
python 3.
2 on win7.
import numpy as np
import matplotlib.pyplot as plt
import scipy.special as sps
shape, scale = 2., 2. # mean and dispersion
counter =0
s = []
upper_bound = 4
lower_bound = 0.5
while (counter <= 1000):
t = np.random.gamma(shape, scale, 1)
if (lower_bound <= t <= upper_bound) :
s.append(t)
counter += 1
count, bins, ignored = plt.hist(s, 50, normed=True)
// this part take s very long time
y = bins**(shape-1)*(np.exp(-bins/scale) /
(sps.gamma(shape)*scale**shape))
plt.plot(bins, y, linewidth=2, color='r')
plt.show()
I find that "y = bins*(shape-1)(np.exp(-bins/scale) / (sps.gamma(shape)scale
*shape))"
take very long time and the pop-up figure window becomes no-responding.
If I remove the lower and upper bounds for the gamma distribution, it runs
very fast.
Any help would be appreciated.
Thanks ! | c*******y 发帖数: 1630 | 2 我这里显示瓶颈在 上面一行
%time count, bins, ignored = plt.hist(s, 50, normed=True)
CPU times: user 36.4 s, sys: 751 ms, total: 37.2 s
Wall time: 37.2 s
3.
【在 m******t 的大作中提到】 : 【 以下文字转载自 Statistics 讨论区 】 : 发信人: myregmit (myregmit), 信区: Statistics : 标 题: long time generating gamma distribution in python : 发信站: BBS 未名空间站 (Thu Mar 13 19:04:24 2014, 美东) : I need to generate a truncated gamma distribution pdf curve and histogram in : python 3. : 2 on win7. : import numpy as np : import matplotlib.pyplot as plt : import scipy.special as sps
| c*******y 发帖数: 1630 | 3 贴到stackoverflow去问问,很多是写library的在上面晃
%prun -l 5 count, bins, ignored = plt.hist(s, 50, normed=True)
45928256 function calls (45910189 primitive calls) in 52.608
seconds
Ordered by: internal time
List reduced from 243 to 5 due to restriction <5>
ncalls tottime percall cumtime percall filename:lineno(function)
366366 14.342 0.000 14.342 0.000 weakref.py:267(__init__)
50050 6.618 0.000 15.825 0.000 artist.py:883(get_aliases)
50050 3.088 0.000 3.088 0.000 {dir}
15215201 2.842 0.000 2.842 0.000 {method 'startswith' of 'str'
objects}
467467 1.866 0.000 2.471 0.000 weakref.py:47(__init__)
3.
【在 m******t 的大作中提到】 : 【 以下文字转载自 Statistics 讨论区 】 : 发信人: myregmit (myregmit), 信区: Statistics : 标 题: long time generating gamma distribution in python : 发信站: BBS 未名空间站 (Thu Mar 13 19:04:24 2014, 美东) : I need to generate a truncated gamma distribution pdf curve and histogram in : python 3. : 2 on win7. : import numpy as np : import matplotlib.pyplot as plt : import scipy.special as sps
| m******t 发帖数: 273 | 4 Thanks for your reply !
Would you please tell me how to get the detailed timing information and run-
time profiles for python ?
Thanks !
【在 c*******y 的大作中提到】 : 贴到stackoverflow去问问,很多是写library的在上面晃 : %prun -l 5 count, bins, ignored = plt.hist(s, 50, normed=True) : 45928256 function calls (45910189 primitive calls) in 52.608 : seconds : Ordered by: internal time : List reduced from 243 to 5 due to restriction <5> : ncalls tottime percall cumtime percall filename:lineno(function) : 366366 14.342 0.000 14.342 0.000 weakref.py:267(__init__) : 50050 6.618 0.000 15.825 0.000 artist.py:883(get_aliases) : 50050 3.088 0.000 3.088 0.000 {dir}
|
|