Factorial in numpy en scipy

Hoe kan ik de faculteitsfunctie van numpy en scipy afzonderlijk importeren om te zien welke sneller is?

Ik heb de faculteit al uit Python zelf geïmporteerd door wiskunde te importeren. Maar het werkt niet voor numpy en scipy.


Antwoord 1, autoriteit 100%

Je kunt ze als volgt importeren:

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.factorialen numpy.math.factoriallijken gewoon aliassen/verwijzingen te zijn voor/naar math.factorial, dat is scipy.math.factorial is math.factorialen numpy.math.factorial is math.factorialmoeten beide Truegeven.


Antwoord 2, autoriteit 65%

Het antwoord voor Ashwini is geweldig, door erop te wijzen dat scipy.math.factorial, numpy.math.factorial, math.factorialzijn dezelfde functies. Ik zou echter aanraden om degene te gebruiken die Janne noemde, dat scipy.special.factorialanders is. De ene van scipy kan np.ndarrayals invoer gebruiken, terwijl de anderen dat niet kunnen.

In [12]: import scipy.special
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.special.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])

Dus, als je faculteit doet op een np.ndarray, zal die van scipy gemakkelijker en sneller te coderen zijn dan de for-loops.


Antwoord 3, autoriteit 33%

SciPy heeft de functie scipy.special.factorial(voorheen scipy.misc.factorial)

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

Antwoord 4, autoriteit 7%

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

of met mul van operator:

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

of helemaal zonder hulp:

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

Antwoord 5, autoriteit 4%

na het uitvoeren van verschillende bovengenoemde functies voor faculteit, door verschillende mensen, blijkt dat math.factorial de snelste is om de faculteit te berekenen.

vind looptijden voor verschillende functies in de bijgevoegde afbeelding


Antwoord 6

Je kunt enkele zelfgemaakte faculteitsfuncties opslaan op een aparte module, utils.py, en ze vervolgens importeren en de prestaties vergelijken met de vooraf bepaalde, in scipy, numpy en wiskunde met timeit.
In dit geval gebruikte ik als externe methode de laatste voorgesteld door Stefan Gruenwald:

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

Hoofdcode (ik gebruikte een raamwerk voorgesteld door JoshAdel in een andere post, zoek naar hoe-kan-ik-krijg-een-array-van-alternerende-waarden-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)

Wat zorgt voor:

method1 0.0195569992065
method2 0.00638914108276
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Process finished with exit code 0

Other episodes