Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

NumPy Factorial: Why np.factorial Fails and What to Use

NumPy has no np.factorial function, so the call raises an AttributeError. Use scipy.special.factorial, with exact=True for integer results.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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'

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This 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.

Confirm the name is missing in your environment

  1. Open a Python shell or script.
  2. Run the following:
    import numpy as np
    print(np.__version__)
    print(hasattr(np, "factorial"))
  3. On NumPy 2.5 as checked, the second print should show False. If it shows True, 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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 scipy in 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.