YKY
YKY

Reputation: 2683

Can't figure out why numpy.log10 outputs nan?

So I have an 500k array of floating values. When I am trying to:

np.log10(my_long_array)

270k numbers getting replaced to nan, and they are not that small. For example:

In [1]: import numpy as np

In [2]: t = -0.055488893531690543

In [3]: np.log10(t)
/home/aydar/anaconda3/bin/ipython:1: RuntimeWarning: invalid value encountered in log10
  #!/home/aydar/anaconda3/bin/python3
Out[3]: nan

In [4]: type(t)
Out[4]: float

What am I missing?

Upvotes: 4

Views: 12250

Answers (2)

tmdavison
tmdavison

Reputation: 69116

the logarithm of a negative number is undefined, hence the nan

From the docs to numpy.log10:

Returns: y : ndarray

The logarithm to the base 10 of x, element-wise. NaNs are returned where x is negative.

Upvotes: 11

Mastodon
Mastodon

Reputation: 151

Negative numbers always give undefined log,

The logarithmic function

y = logb(x)

is the inverse function of the exponential function

x = b^y

Since the base b is positive (b>0), the base b raised to the power of y must be positive (b^y>0) for any real y. So the number x must be positive (x>0).

The real base b logarithm of a negative number is undefined.

logb(x) is undefined for x ≤ 0

Upvotes: 5

Related Questions