173

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?

6 Answers 6

273

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

Sign up to request clarification or add additional context in comments.

1 Comment

For those who were wondering what np is, like myself "import numpy as np"
24

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

1 Comment

what about loss of precision?
23

I usually do like this:

from numpy import log as ln 

Perhaps this can make you more comfortable.

1 Comment

Even more consequent from numpy import log as ln, log10 as log; but probably not so advisable.
1

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 

Comments

-4
from numpy.lib.scimath import logn from math import e #using: x - var logn(e, x) 

Comments

-4

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 

1 Comment

note math.e exists and math.log takes the base 2nd. so math.log(10, math.e) is correct, while the above would actually return ~0.43...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.