2014-02-13 145 views
23

如何从numpy和scipy分别导入析因函数以查看哪一个更快?Factorial in numpy and scipy

我已经通过导入数学从python本身导入了阶乘。但是,它不适用于numpy和scipy。

回答

30

可以导入他们是这样的:

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.factorialnumpy.math.factorial似乎仅仅是别名/参考/来math.factorial,即scipy.math.factorial is math.factorialnumpy.math.factorial is math.factorial都应该给予True

10

SciPy的具有功能scipy.special.factorial(以前scipy.misc.factorial

>>> import math 
>>> import scipy.special 
>>> math.factorial(6) 
720 
>>> scipy.special.factorial(6) 
array(720.0) 
27

为阿什维尼答案是伟大的,在指出scipy.math.factorialnumpy.math.factorialmath.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循环更容易代码和更快。

+3

约'scipy.misc.factorial'的好处是,它仅计算一次阶乘 - 为最大的数量数组中。所有其他的计算是在这个过程中的副作用。 –

+0

弃用警告:在scipy 1.0.0中。使用'scipy.special.factorial' – lincolnfrias

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

可以保存在单独的模块,utils.py一些自制的阶乘函数,然后将它们导入并使用timeit将其与预定义的表现进行比较,以scipy,numpy和math进行比较。 在这种情况下,我作为外部方法最后由斯特凡·古伦华德建议:

import numpy as np 


def factorial(n): 
    return reduce((lambda x,y: x*y),range(1,n+1)) 

主代码(我曾经在另一篇由JoshAdel提出了一个框架,寻找如何,可以-I-GET-的阵列-of交替值功能于蟒蛇):

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