Top Maths
Top Maths

Reputation: 61

Error on Importing module numpy.math

when I use

from numpy.math import factorial as fact 

I get : ImportError: No module named 'numpy.math'

but

import numpy
fact=numpy.math.factorial

works.

Why ? Is numpy.math really implemented like other modules ?

Upvotes: 4

Views: 2267

Answers (3)

Mr. T
Mr. T

Reputation: 12410

Arpad Horvath's answer has already shown, that numpy.math is not different from the math library and therefore won't work on numpy arrays that can't be converted to scalars. But you can use scipy.misc.factorial:

import scipy.misc
a = np.arange(5)
print(scipy.misc.factorial(a))
#output
#[ 1.  1.  2.  6. 24.]

This is deprecated in scipy 1.0.0, though still working. Use scipy.special.factorial instead.
Other mathematical functions implemented for arrays are listed here in numpy and here in scipy.specials

Upvotes: 2

numpy seems to import the standard math library:

In [8]: import numpy

In [9]: import math

In [10]: math is numpy.math
Out[10]: True

So it is not the submodule of numpy, just an imported module object. That is why you can not import like this: from numpy.math import something .

Upvotes: 3

JahKnows
JahKnows

Reputation: 2706

Don't need numpy, just use

from math import factorial as fact

Upvotes: 1

Related Questions