2014-02-13 19 views
23

Wie kann ich faktorielle Funktion von numpy und scipy separat importieren, um zu sehen, welche schneller ist?Factorial in numpy und scipy

Ich importierte Fakultät bereits von Python selbst durch Import Mathe. Aber es funktioniert nicht für numpy und scipy.

Antwort

30

Sie können sie wie folgt importieren:

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 und numpy.math.factorial scheinen einfach Aliase/Verweise auf sein/zu math.factorial, dass scipy.math.factorial is math.factorial und numpy.math.factorial is math.factorial beide True geben sollte.

10

SciPy hat die Funktion scipy.special.factorial (früher scipy.misc.factorial)

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

Die Antwort für Ashwini groß ist, darauf hinweist, dass scipy.math.factorial, numpy.math.factorial, math.factorial die gleichen Funktionen sind. Allerdings würde ich empfehlen, die zu verwenden, die Janne erwähnte, dass scipy.misc.factorial anders ist. Die von scipy kann np.ndarray als Eingabe nehmen, während die anderen nicht können.

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

Also, wenn Sie faktorielles zu einem np.ndarray tun, wird derjenige von scipy zu Code und schneller einfacher sein, als die for-Schleifen zu tun.

+3

Das gute an 'scipy.misc.factorial' ist, dass es nur ein einziges Mal die Fakultät berechnet - für von die größte Zahl r im Array. Alle anderen werden dabei als Nebeneffekt berechnet. –

+0

Verfallswarnung: in scipy 1.0.0. Verwende 'scipy.special.factorial' – lincolnfrias

1
from numpy import prod 

    def factorial(n): 
     print prod(range(1,n+1)) 

oder mit mul von Betreiber:

from operator import mul 

    def factorial(n): 
     print reduce(mul,range(1,n+1)) 

oder ganz ohne Hilfe:

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

Sie können auf einem separaten Modul, utils.py, einige hausgemachte faktorielles Funktionen speichern und dann importiere sie und vergleiche die Performance mit der vordefinierten, in scipy, numpy und math mit timeit. In diesem Fall habe ich als externe Methode verwendet, die von Stefan Gruenwald vorgeschlagen zuletzt:

import numpy as np 


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

Hauptcode (ich einen Rahmen von JoshAdel in einem anderen Beitrag vorgeschlagen verwendet, suchen Sie nach wie-can-i-get-an-Array -von-Wechsel Werte-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) 

dem es heißt:

method1 0.0195569992065 
method2 0.00638914108276 

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 


Process finished with exit code 0 
Verwandte Themen