Python numpy 和 scipy 中的因子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21753841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Factorial in numpy and scipy
提问by MOON
How can I import factorial function from numpy and scipy separately in order to see which one is faster?
如何分别从 numpy 和 scipy 导入阶乘函数以查看哪个更快?
I already imported factorial from python itself by import math. But, it does not work for numpy and scipy.
我已经通过导入数学从 python 本身导入了阶乘。但是,它不适用于 numpy 和 scipy。
采纳答案by Ashwini Chaudhary
You can import them like this:
您可以像这样导入它们:
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.factorialand numpy.math.factorialseem to simply be aliases/references for/to math.factorial, that is scipy.math.factorial is math.factorialand numpy.math.factorial is math.factorialshould both give True.
scipy.math.factorial并且numpy.math.factorial似乎只是对/to 的别名/引用math.factorial,即scipy.math.factorial is math.factorial并且numpy.math.factorial is math.factorial应该都给True.
回答by Janne Karila
SciPy has the function scipy.special.factorial(formerly scipy.misc.factorial)
SciPy 有这个功能scipy.special.factorial(以前的scipy.misc.factorial)
>>> import math
>>> import scipy.special
>>> math.factorial(6)
720
>>> scipy.special.factorial(6)
array(720.0)
回答by Yuxiang Wang
The answer for Ashwini is great, in pointing out that scipy.math.factorial, numpy.math.factorial, math.factorialare the same functions. However, I'd recommend use the one that Janne mentioned, that scipy.special.factorialis different. The one from scipy can take np.ndarrayas an input, while the others can't.
对于阿什维尼答案是伟大的,在指出scipy.math.factorial,numpy.math.factorial,math.factorial有相同的功能。但是,我建议使用 Janne 提到的那个,那scipy.special.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])
So, if you are doing factorial to a np.ndarray, the one from scipy will be easier to code and faster than doing the for-loops.
因此,如果您对 np.ndarray 进行阶乘,那么来自 scipy 的将比执行 for 循环更容易编码且速度更快。
回答by Stefan Gruenwald
from numpy import prod
def factorial(n):
print prod(range(1,n+1))
or with mul from operator:
或使用来自运营商的 mul:
from operator import mul
def factorial(n):
print reduce(mul,range(1,n+1))
or completely without help:
或完全没有帮助:
def factorial(n):
print reduce((lambda x,y: x*y),range(1,n+1))
回答by SeF
You can save some homemade factorial functions on a separate module, utils.py, and then import them and compare the performance with the predefinite one, in scipy, numpy and math using timeit. In this case I used as external method the last proposed by Stefan Gruenwald:
您可以将一些自制的阶乘函数保存在一个单独的模块 utils.py 中,然后将它们导入并使用 timeit 在 scipy、numpy 和 math 中将它们与预先确定的函数进行比较。在这种情况下,我使用了 Stefan Gruenwald 最后提出的外部方法:
import numpy as np
def factorial(n):
return reduce((lambda x,y: x*y),range(1,n+1))
Main code (I used a framework proposed by JoshAdel in another post, look for how-can-i-get-an-array-of-alternating-values-in-python):
主要代码(我在另一篇文章中使用了 JoshAdel 提出的框架,查找 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)
Which provides:
其中提供:
method1 0.0195569992065
method2 0.00638914108276
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Process finished with exit code 0


