如何从numpy和scipy中分别导入阶乘函数,以便查看哪一个更快?
我已经通过导入数学从python本身导入了factorial。但是,它不适用于numpy和scipy。
答案 0 :(得分:46)
您可以像这样导入它们:
In [7]: import scipy, numpy, math
In [8]: scipy.math.factorial, numpy.math.factorial, math.factorial
Out[8]:
(<function math.factorial>,
<function math.factorial>,
<function math.factorial>)
scipy.math.factorial
和numpy.math.factorial
似乎只是/ math.factorial
的别名/引用,scipy.math.factorial is math.factorial
和numpy.math.factorial is math.factorial
都应该True
}。
答案 1 :(得分:36)
Ashwini的答案很棒,指出scipy.math.factorial
,numpy.math.factorial
,math.factorial
功能相同。但是,我建议使用Janne提到的那个,scipy.misc.factorial
是不同的。来自scipy的那个可以将np.ndarray
作为输入,而其他人则不能。
In [12]: import scipy.misc
In [13]: temp = np.arange(10) # temp is an np.ndarray
In [14]: math.factorial(temp) # This won't work
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-039ec0734458> in <module>()
----> 1 math.factorial(temp)
TypeError: only length-1 arrays can be converted to Python scalars
In [15]: scipy.misc.factorial(temp) # This works!
Out[15]:
array([ 1.00000000e+00, 1.00000000e+00, 2.00000000e+00,
6.00000000e+00, 2.40000000e+01, 1.20000000e+02,
7.20000000e+02, 5.04000000e+03, 4.03200000e+04,
3.62880000e+05])
所以,如果你对np.ndarray进行阶乘,那么来自scipy的那个将更容易编码并且比执行for循环更快。
答案 2 :(得分:17)
SciPy具有scipy.special.factorial
功能(以前为scipy.misc.factorial
)
>>> import math
>>> import scipy.special
>>> math.factorial(6)
720
>>> scipy.special.factorial(6)
array(720.0)
答案 3 :(得分:3)
from numpy import prod
def factorial(n):
print prod(range(1,n+1))
或来自运营商的mul:
from operator import mul
def factorial(n):
print reduce(mul,range(1,n+1))
或完全没有帮助:
def factorial(n):
print reduce((lambda x,y: x*y),range(1,n+1))
答案 4 :(得分:1)
你可以在一个单独的模块utils.py上保存一些自制的阶乘函数,然后导入它们,并使用timeit在scipy,numpy和math中比较性能与prefinite的性能。 在这种情况下,我使用Stefan Gruenwald最后提出的外部方法:
import numpy as np
def factorial(n):
return reduce((lambda x,y: x*y),range(1,n+1))
主要代码(我在另一篇文章中使用了JoshAdel提出的框架,在python中查找how-can-i-get-an-arrays-of-altern-values-in):
from timeit import Timer
from utils import factorial
import scipy
n = 100
# test the time for the factorial function obtained in different ways:
if __name__ == '__main__':
setupstr="""
import scipy, numpy, math
from utils import factorial
n = 100
"""
method1="""
factorial(n)
"""
method2="""
scipy.math.factorial(n) # same algo as numpy.math.factorial, math.factorial
"""
nl = 1000
t1 = Timer(method1, setupstr).timeit(nl)
t2 = Timer(method2, setupstr).timeit(nl)
print 'method1', t1
print 'method2', t2
print factorial(n)
print scipy.math.factorial(n)
提供:
method1 0.0195569992065
method2 0.00638914108276
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Process finished with exit code 0
答案 5 :(得分:0)