Python Numpy.log() - Logarithm

Jinku Hu Jan 30, 2023
  1. Syntax of numpy.log()
  2. Example Codes: numpy.log()
  3. Example Codes: numpy.log2() - Numpy Log Base 2
  4. Example Codes: numpy.log10() - Numpy Log Base 10
Python Numpy.log() - Logarithm

Numpy.log() function calculates the natural logarithm of every element in the given array.

numpy natural logarithm

Syntax of numpy.log()

numpy.log(arr)

Parameters

arr input array

Return

It returns an array of the natural logarithm of each element in the input array.

Example Codes: numpy.log()

import numpy as np

arr = [1, np.e, np.e**2, np.e**3]
print(np.log(arr))

Output:

[0. 1. 2. 3.]

Example Codes: numpy.log2() - Numpy Log Base 2

numpy.log2() is simliar to numpy.log(), but has the base as 2 instead of e.

numpy log2

import numpy as np

arr = [1, 2, np.e, 4]
print(np.log2(arr))

Output:

[0. 1. 1.44269504 2.]

Example Codes: numpy.log10() - Numpy Log Base 10

numpy.log10() is similar to numpy.log(), but has the base as 10 instead of e.

numpy log10

import numpy as np

arr = [1, np.e, 10, 100]
print(np.log10(arr))

Output:

[0. 0.43429448 1. 2.]
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook