How do you do natural logs (e.g. "ln()") with numpy in Python?

PythonNumpyLogarithmNatural Logarithm

Python Problem Overview


Using numpy, how can I do the following:

ln(x)

Is it equivalent to:

np.log(x)

I apologise for such a seemingly trivial question, but my understanding of the difference between log and ln is that ln is logspace e?

Python Solutions


Solution 1 - Python

np.log is ln, whereas np.log10 is your standard base 10 log.

Solution 2 - Python

Correct, np.log(x) is the Natural Log (base e log) of x.

For other bases, remember this law of logs: log-b(x) = log-k(x) / log-k(b) where log-b is the log in some arbitrary base b, and log-k is the log in base k, e.g.

here k = e

l = np.log(x) / np.log(100)

and l is the log-base-100 of x

Solution 3 - Python

I usually do like this:

from numpy import log as ln

Perhaps this can make you more comfortable.

Solution 4 - Python

Numpy seems to take a cue from MATLAB/Octave and uses log to be "log base e" or ln. Also like MATLAB/Octave, Numpy does not offer a logarithmic function for an arbitrary base.

If you find log confusing you can create your own object ln that refers to the numpy.log function:

>>> import numpy as np
>>> from math import e
>>> ln = np.log  # assign the numpy log function to a new function called ln
>>> ln(e)
1.0

Solution 5 - Python

from numpy.lib.scimath import logn
from math import e

#using: x - var
logn(e, x)

Solution 6 - Python

You could simple just do the reverse by making the base of log to e.

import math

e = 2.718281

math.log(e, 10) = 2.302585093
ln(10) = 2.30258093

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1220022View Question on Stackoverflow
Solution 1 - PythonJoshAdelView Answer on Stackoverflow
Solution 2 - PythonkavemanView Answer on Stackoverflow
Solution 3 - PythonVincentView Answer on Stackoverflow
Solution 4 - PythonbfrisView Answer on Stackoverflow
Solution 5 - PythonoutoftimeView Answer on Stackoverflow
Solution 6 - PythonRavioleView Answer on Stackoverflow