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

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?

This question is related to python numpy logarithm natural-logarithm

The answer is


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

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

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

I usually do like this:

from numpy import log as ln

Perhaps this can make you more comfortable.


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


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to numpy

Unable to allocate array with shape and data type How to fix 'Object arrays cannot be loaded when allow_pickle=False' for imdb.load_data() function? Numpy, multiply array with scalar TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'" Pytorch tensor to numpy array Numpy Resize/Rescale Image what does numpy ndarray shape do? How to round a numpy array? numpy array TypeError: only integer scalar arrays can be converted to a scalar index

Examples related to logarithm

ValueError: math domain error How do you do natural logs (e.g. "ln()") with numpy in Python? Python math module How to expand and compute log(a + b)? Log to the base 2 in python How do you calculate log base 2 in Java for integers? Histogram with Logarithmic Scale and custom breaks Plot logarithmic axes with matplotlib in python

Examples related to natural-logarithm

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