NumPy does not provide a factorial function, so np.factorial(...) raises an error. The factorial function you want is in SciPy: scipy.special.factorial. It accepts single numbers and arrays, and it lets you choose between exact integer results and fast floating-point approximations.
Why np.factorial fails
When you call np.factorial(5), Python reports that the numpy module has no attribute named factorial. The message usually looks like this:
As an Amazon Associate I earn from qualifying purchases.
AttributeError: module 'numpy' has no attribute 'factorial'
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallThis is a missing name, not a bug in NumPy’s arithmetic. The NumPy API reference, version 2.5 (released June 28, 2026), documents its routines by group and does not list a dedicated factorial function. The conclusion that the name is absent is an inference from checking that reference, not a statement from a NumPy page that discusses factorials directly. Confirm it on your own installation with the check below.
#1 Best Overall
Confirm the name is missing in your environment
- Open a Python shell or script.
- Run the following:
import numpy as np print(np.__version__) print(hasattr(np, "factorial")) - On NumPy 2.5 as checked, the second print should show
False. If it showsTrue, your code is using a different module or a third-party package that defines the name, so check the import at the top of the file.
Use scipy.special.factorial instead
SciPy’s scipy.special module documents factorial for scalar and array inputs. Install SciPy if it is not already present (python -m pip install scipy), then import the function directly:
import numpy as np
from scipy.special import factorial
values = np.array([3, 4, 5])
exact_values = factorial(values, exact=True)
approx_values = factorial(values) # exact=False is the default
print(exact_values) # [ 6 24 120]
print(approx_values) # [ 6. 24. 120.]
The SciPy 1.18.0 manual shows this same array input, [3, 4, 5], returning [6, 24, 120] in both modes. The difference is in the type of the output, which is explained next.
Rank #2
Choose exact or approximate mode
The exact keyword controls how the factorial is computed and what type the result has. Choose the mode based on whether your later calculations need exact integers.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →| Mode | How to call it | How it is computed | Output type | Use it when |
|---|---|---|---|---|
| Exact | factorial(values, exact=True) |
Integer arithmetic | Integers; dtype becomes int64 or object when the values require it |
You need exact results, such as combinatorial counts or comparisons for equality |
| Approximate (default) | factorial(values) or exact=False |
Gamma-function approximation | Floating-point values | A floating-point approximation is acceptable and speed or simplicity matters more than exactness |
Exact mode and dtype
With exact=True, SciPy uses integer arithmetic. The SciPy reference notes that the output dtype increases to int64 or object when necessary. Do not assume a fixed-width integer result: for large inputs, the values may be stored as Python integers inside an object array, which is correct but slower to process. Check the result with result.dtype before relying on a particular type.
Approximate mode and floats
The default mode computes the factorial through the gamma function and returns floats. Those floats are not guaranteed to equal the true integer, so do not use == to compare them with integer expectations. Very large inputs can also overflow floating point, so the exact mode is the safer choice when the magnitude is high and the result must be correct.
How array input behaves
The SciPy scipy.special reference states that most of its special functions accept NumPy arrays and follow NumPy’s broadcasting and automatic array-looping rules. The overview lists some exceptions, so do not assume that every function in the module behaves this way. Test any function you rely on with an array of your real shape. For factorial, passing a NumPy array as shown above is the documented pattern.
A NumPy-only table for consecutive integers
If you need the factorials of every integer in a consecutive range that starts at 1, you can build the table with NumPy’s cumulative product. This works only for that case. It does not replace factorial for arbitrary inputs, and this article does not establish a speed comparison between the two methods.
Free tools Windows power users keep installed
One-click scans. No signup required.
import numpy as np
n = 10
table = np.cumprod(np.arange(1, n + 1)) # 1!, 2!, ..., 10!
print(table)
The result uses the default integer dtype, which is int64 on most 64-bit platforms. A signed 64-bit integer holds values only up to 20! (2,432,902,008,176,640,000). Starting at 21!, the values overflow silently, so keep n at or below 20 for this method, or use scipy.special.factorial(..., exact=True) for larger values. The range must start at 1; the value 0! equals 1 and must be added separately if you need it.
Best Value
Troubleshooting checklist
- AttributeError on np.factorial: replace the call with
from scipy.special import factorial. - ImportError on scipy.special: SciPy is not installed in the active environment. Run
python -m pip install scipyin the same interpreter you use to run your script. - Floats where you expected integers: you are using the default approximate mode. Pass
exact=True. - Object dtype in the output: the exact mode needed Python integers for large values. Convert with care, or keep the object array if you only need Python-level arithmetic.
- Wrong values from a cumulative product: the range does not start at 1, or it extends past 20 with the default 64-bit integers.
Version notes
The NumPy statements here apply to the version 2.5 reference checked here, and the SciPy statements apply to the 1.18.0 manual. Both projects release updates, so check the current documentation for your installed versions before relying on the behavior described in this article.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




