In this post, we are going to Explained Python NumPy Log with examples. By using the Numpy logarithmic Function we calculate the logarithmic value of the array, it can be a one, two, three-dimensional array.To calculate the logarithmic NumpPy has functions like np.log() ,log2() ,log10(),log1p().
Lists of NumPy log Functions
We will understand all these logarithmic functions of the NumPy library step by step with examples.
- np.log()
- np.log2()
- np.log10()
- np.log1p()
- How to calculate custom base
1.Numpy log() function
The Numpy np.log() function allows us to calculate the nature logarithmic value of each element of the passed array. The Natural Logarithm of any value is the inverse of its exponential value.
Example :Calculate Numpy log base
In this example, we are calculating the Natural log of a one-dimensional array using np.log()
#program to calculate base using log() function
import numpy as np
nparray = np.array([12, 15, 16, 18])
print('original array:',nparray)
print('Logarithmic Values of nparray element :',np.log(nparray))
Output
original array: [12 15 16 18]
Logarithmic Values of nparray element : [2.48490665 2.7080502 2.77258872 2.89037176]
Example : Calculating 2D array log base
In this example, we are calculating the Natural log of a two-dimensional array using np.log()
#program to calculate base of 2d array using log() function
import numpy as np
nparray = np.arange(1,10).reshape(3,3)
print("Original np 2D Array:\n",nparray)
print("\n Logarithmic value of 2D np array elements:\n",np.log(nparray))
Output
Original np 2D Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Logarithmic value of 2D np array elements:
[[0. 0.69314718 1.09861229]
[1.38629436 1.60943791 1.79175947]
[1.94591015 2.07944154 2.19722458]]
2. Numpy log2() function
Numpy log2() function allow us to calculate the base 2 value of each element of the passed array.
Example : Calculating Numpy log2 base
In this example, we are calculating the Natural log of a one-dimensional array using np.log2()
#program to calculate base using Numpy log2() function
import numpy as np
nparray = np.array([12, 15, 16, 18])
print('original Numpy array:',nparray)
print('Base 2 Values of array element :',np.log2(nparray))
Output
original Numpy array: [12 15 16 18]
Base 2 Values of array element : [3.5849625 3.9068906 4. 4.169925 ]
Example :Calculating base log2() of 2-D array
In this example, we are generating 2D a random ndarray using randint() function and calculating the base using log2() function.
import numpy as np
nparray = np.random.randint(1, 12, size = (3, 3))
print('original 2D random array:',nparray)
print('base2 value 2D random Numpy array elements: ',np.log2(nparray))
Output
original 2D random array: [[ 9 8 6]
[ 7 10 2]
[ 3 1 7]]
base2 value 2D random Numpy array elemnts : [[3.169925 3. 2.5849625 ]
[2.80735492 3.32192809 1. ]
[1.5849625 0. 2.80735492]]
3. Numpy log10() function
Numpy log10() function allow us to calculate the base 10 value of each element of the passed array. In this example, we are calculating the base10 value of a random 2D array element using the log10() function.
import numpy as np
nparray = np.random.randint(1, 12, size = (3, 3))
print('original 2D random array:',nparray)
print('base10 value 2D random Numpy array :\n ',np.log10(nparray))
Output
original 2D random array: [[10 1 9]
[ 6 5 5]
[ 4 5 4]]
base10 value 2D random Numpy array :
[[1. 0. 0.95424251]
[0.77815125 0.69897 0.69897 ]
[0.60205999 0.69897 0.60205999]]
4. Numpy log1p() function
The numpy np.log1p() allow us to calculate plus 1 the value for each element of the passed array. In this example, we will understand how to calculate 1 plus value of Numpy array element using log1p() function of NumPy library.
import numpy as np
nparray = np.random.randint(1, 12, size = (3, 3))
print('original 2D random array:',nparray)
print('base10 value 2D random Numpy array :\n ',np.log1p(nparray))
Output
original 2D random array: [[ 3 8 7]
[ 1 10 11]
[ 8 11 1]]
base10 value 2D random Numpy array :
[[1.38629436 2.19722458 2.07944154]
[0.69314718 2.39789527 2.48490665]
[2.19722458 2.48490665 0.69314718]]
5. NumPy log using custom base
The Numpy libaray log() function allow us calculate base of user passed value. In this example we will understand how to find the base of custom passed values
In mathematics, the formula used to calculate the base is
logb(a) = logx(a)/logx(b)
Let us understand with example how to achieve this
#log arithmetic using custom base
import numpy as np
value = 200
base = 10
calu_base = np.log(value)/np.log(base)
print('calculate base of custom values :',calu_base)
Output
calculate base of custom values : 2.301029995663981
Summary :
In this post,we have understood NumPy Log Functions with base n with examples that includes np.log() ,log2() ,log10(),log1p() and Calculate log arithmetic using custom base.We can use any of them as per our requirement.