# Python Numpy Factorial

In this Python tutorial, we will learn about Python numpy factorial. Also, we will cover these topics.

• Python Numpy Factorial
• Python Numpy Factorial of Array
• Using Numpy Factorial
• Numpy Factorial Function
• Numpy Factorial Vector
• Numpy Double Factorial
• Numpy Factorial Example

## Python Numpy Factorial

In this section, we will learn how to find python numpy factorial. The formula for factorial is `n! = n(n-1) (n-2) (n-3) .... n`. Here n is the number whose factorial is to be calculated.

• Factorial is the product of integer and all the integers below it. Example, factorial of 5 is 1x2x3x4x5 = 120.
• Factorial displays number of possiblities to perform a task or to choose an item from the collection.
• Here is the syntax to calculate python numpy factorial. Here number is the value whose factorial is to be calculated.
``numpy.math.factorial(number)``

Source Code:

In this code, we have import the python numpy module and then using `numpy.math.factorial()` method we have calculated the factorial of number 5.

``````import numpy as np

numpy.math.factorial(5)``````

Output:

In this output, the factorial of 5 is calculated as 5x4x3x2x1=120. we have performed this on jupyter notebook. In case you are performing it on any other code editor then use `print() `function to display the result.

## Python Numpy Factorial of Array

In this section, we will learn how to calculate python numpy factorial of an array. An array is the collection of homogenous data.

• We can only pass positive integers in factorial() function and array can have same kind data type.
• So all the conditions seems to be in our favour. We will createPython Numpy Factorial
• Numpy Factorial of Array
• Python Numpy Factorial of Array
• Using Numpy Factorial
• Numpy Factorial Function an array or python list with integer number and then calculate the factorial of each item in that array.
• Using numpy we can calculate factorial of python list or one-dimensional array only.
• While working with multiple dimensional array we have to use python scikit-learn epts positive integer as an input.
• Using this method we can calcluate Numpy factorial in python.
• To perform it on multiple numbers we can pass the numbers through loop and keep on applying this function on each number.
• In our example, we have taken numbers ‘67895’. The result is going to be verlibrary to perform factorial of an array. See our Using Numpy Factorial section to see the demnostration.

Source Code:

In this code, we have calculated and displayed the factorial of each number in the array using nump.math.factorial().

``````import numpy

#array of numbers
arr = [5, 6, 7, 8]

#empty array
new_arr = []

#loop through each item in array (arr)
for num in arr:

#calculate factorial of each item
res = numpy.math.factorial(num)

new_arr.append(res)

#display result
print('Before:',arr)
print('After:',new_arr)
``````

Output:

In this output, the first array is the array we passed to the program and in the next array, Python Numpy Factorial of Array is displayed.

## Using Numpy Factorial

In this section, we will learn to use numpy factorial in python. In our other sections, we have shown how to calculate python numpy factorial. But in this section, we will do something different. We will calculate the factorial of an array with multiple dimensions.

• We will learn to use python numpy factorial to calculate multidimensional array.
• Numpy offers a module `numpy.math.factorial(int)`but it cannot calculate an array. so we will use another module to calculate factorial of multidimensional array.
• Scipy module provide a method `scipy.math.factorial(int/array)` using which we can calculate foctorial of an array.
• Please note that large number will produce really large factorials so please use small numbers if you are getting started.
• Scipy can be installed using either conda or pip. Below is the code to install scipy module.
``````# for pip users
pip install scipy

# conda users
conda install scipy``````
• In our example, we have calculated factorial of 2 dimensional array. Here is the source code of our example.
``````# importing modules
from scipy.special import factorial
import numpy as np

# creating 2D array
array = np.array([
[5,3,1],
[2,6,4]
])

# printing factorial
print(factorial(array))

``````

Output:

In this output, we have created 2-dimensional array using numpy and then using scipy we have calculated the factorial of an array.

## Numpy Factorial Function

In this section, we will learn about the python numpy factorial function. Numpy provides a built-in function `numpy.math.factorial()` using which we can calculate the factorial of a number.

• Python numpy `numpy.math.factorial(num)` method accepts a positive integer number as a argument.
• If you are getting a below error that means you have entered negative number. Factorial can be performed only on positive integer.
`ValueError: factorial() not defined for negative values`
• If you are getting below error that means either you have entered a string, alphanumeric or number with decimal (float). Change it to positive integer to fix this error.
`ValueError: factorial() only accepts integral values`

Source code:

In our example, we have created a simple demonstration of python numpy numpy.math.factorial() method wherein we have calculated factorial of number 6. Also, we have tried to find factorial of negative and non-integer values.

``````import numpy

#factorial of integer number
numpy.math.factorial(6)

#factorial of negative number
numpy.math.factorial(-6)

#factorial of float number
numpy.math.factorial(6.5)

#factorial of string
numpy.math.factorial("6.5")``````

Output:

This is the output performed on the jupyter notebook. In this output, we have tried calculating the factorial by providing variations of input. the correct answer is displayed only when we have entered a positive integer number in line 2 rest of all the test cases threw an error.

## Numpy Factorial Vector

In this section, we will learn about python mupy factorial vector. Vector and array has thin line difference. Vector can be treated as array in the case of factorial. The major difference between both is vector has dynamic values whereas arrays have fixed values.

Here is the example of calculating numpy factorial using vector in python. In this example, using numpy we have created 2D vector and using python scipy module we have calculated the factor.

``````# importing modules
from scipy.special import factorial
import numpy as np

# creating 2D vector
array = np.array([
[5,3,1],
[2,6,4]
])

# printing factorial
print(factorial(array))
``````

Output:

In this output, you can see the result that all the values have their factorials calculated in the vector form.

## Numpy Double Factorial

Factorial is used to display the possibilities for selecting a thing from the collection and is represented as `n!` where n is the number whose factorial is to be calculated. But in the case of python numpy double factorial, it is represented as `n!!`.

• If we have to calculate factorial of 5 it will be 5 x 4 x 3 x 2 x 1 = 120. But in case of double factorial, same statement will be 5 x 3 x 1 = 15.
• In double factorial, the number jumps 2 steps in reverse order.
• Python numpy do not provide any built-in library using which we can calculate double factorial.
• But using Scikit-learn or sklearn module we can perform double factorial of a number.
• Sklearn provides a module `factorial2` using which we can calculate double factorial in python numpy.

Syntax:

Here is the syntax of sklearn module to perform double factorial in python. We assume that you have installed sklearn module in your system.

``````from scipy.special import factorial2

factorial(number, exact=True/False)``````

Source Code:

Here is the implementation of the numpy double factorial. In this example, we have used sklearn module to calculate the double factorial in python.

``````# import sklearn module
from scipy.special import factorial2

# calculating factorial of number 9
factorial2(9, exact=False)

# get the exact value
factorial2(9, exact=True)
``````

Output:

In this output, we have displayed the double factorial of number in python using scipy or sklearn library.

## Numpy Factorial Example

In this section, we have demonstrated a numpy factorial example using python Tkinter.

• We have created a GUI based application for finding factorial using python tkinter module.
• The application accepts positive integer number and prints the result. In case the number is not positive integer then error will be threw on the terminal.
• This can be used as a small project and as a task you can add error popup for incorrect input.

Source Code:

In this source code, we have used the Tkinter module of python to create GUI based application for calculating factorial.

We have created a function cal_factorial() inside this function using `numpy.math.factorial()` module we have returned the factorial of a number entered by the user.

``````import numpy
from tkinter import *

f = ('sans-serif', 14)
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F27405')

def cal_factorial():
num = var.get()
res = numpy.math.factorial(num)

var = IntVar()

Label(
ws,
text='Factorial Calculator',
font = ('sans-serif', 22),
relief=SOLID,

Label(
ws,
text='Enter Number',
font= f,
bg='#F27405'

Entry(
ws,
textvariable=var,
font = f
).pack()

Button(
ws,
text='Calculate',
command=cal_factorial,
font = f,
bg='#060126',
fg='white'

ws.mainloop()``````

Output:

In this output, GUI based application is displayed created using the python Tkinter module. It accepts a positive integer number and displays the result at the bottom. In this example, we have entered positive integer 5 and result 120 is displayed.