在numpy和scipy中的因子

问题描述 投票:36回答:5

如何从numpy和scipy中分别导入阶乘函数,以便查看哪一个更快?

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

python numpy scipy
5个回答
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.factorialnumpy.math.factorial似乎只是math.factorial的别名/参考,即scipy.math.factorial is math.factorialnumpy.math.factorial is math.factorial都应该给True


36
投票

阿什维尼的答案很棒,指出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循环更快。


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
投票
    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
投票

您可以将一些自制的factorial函数保存在单独的模块utils.py上,然后导入它们并将其性能与prefinite函数进行比较,使用timeit进行scipy,numpy和math。在这种情况下,我使用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-array-of-alternating-values-in-python):

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
© www.soinside.com 2019 - 2024. All rights reserved.