Understanding Python Numpy log

NumPy Log() Function

Hey, folks! In this article, we will be focusing on Python Numpy logarithm functions.


Getting started with Python NumPy log

Python NumPy module deals with creation and manipulation of array data elements.

The numpy.log() method is used calculate the natural logarithmic value of a data value of an element/array values.

Syntax:

numpy.log(element/array)

Example 1:

import numpy as np
data = 12.2
log_val = np.log(data)
print(log_val)

Output:

2.501435951739211

Example 2:

import numpy as np
data = np.e
log_val = np.log(data)
print(log_val)

Output:

1.0

NumPy logarithm with base 2

Apart from the default functionality of log() method, we can calculate the log value of a NumPy array or an element with the base 2 using the below command:

numpy.log2(element)

Example:

import numpy as np
data = 4
log_val = np.log2(data)
print(log_val)

Output:

2.0

NumPy logarithm with base 10

The numpy.log10() function is used to calculate the natural logarithmic value of an element to the base 10.

Syntax:

numpy.log10(data)

Example:

import numpy as np
data = 1000
log_val = np.log10(data)
print(log_val)

Output:

3.0

NumPy logarithm with a custom base

NumPy log() function offers a possibility of finding logarithmic value with respect to user-defined bases.

Syntax:

numpy.log(data)/numpy.log(base)

Example:

import numpy as np
data = 1000
base = 40
log_val = np.log(data)/np.log(base)
print(log_val)

In the above example, we have calculated the logarithmic value of 1000 with base 40.

As we all know,

Log Value With Custom Base
Log Value With Custom Base

Thus, the above mathematical concept is used to calculate the log value of a data value to custom base value.

Output:

1.8725890517453545

Performing NumPy log on a 2-D array

The numpy.log() method can be applied to a 2-D NumPy array to calculate the logarithmic values of all the array elements.

Syntax:

numpy.log(array)

Example:

import numpy as np
arr = np.arange(1,5).reshape(2,2)
print("Original Array:\n")
print(arr)
print("\Logarithmic value of array elements:\n")
log_val=np.log(arr)
print(log_val)

In the above example, we have created a 2×2 array using numpy.reshape() function and used random numbers to create data values using numpy.arange() method.

Further, numpy.log() method is used to find the log value of every element of the array.

Output:

Original Array:

[[1 2]
 [3 4]]

Logarithmic value of array elements:

[[0.         0.69314718]
 [1.09861229 1.38629436]]

Applying NumPy log on a NumPy array

Example:

import numpy as np
arr = np.array([10,20,30,4,5])
print("Original Array:\n")
print(arr)
print("\nLogarithm value of array elements:\n")
log_val=np.log(arr)
print(log_val)

We have created a NumPy array using numpy.array() function and used the numpy.log() method to calculate the log values of all the data items of the 1-D array.

Output:

Original Array:

[10 20 30  4  5]

Logarithm value of array elements:

[2.30258509 2.99573227 3.40119738 1.38629436 1.60943791]

Graphical representation of NumPy log

In order to have a better understanding of the calculated log values, we can plot the log values against the original values using Python Matplotlib module.

Example:

import numpy as np
import matplotlib.pyplot as plt
arr = np.array([10,20,30,40,50])
log_val=np.log(arr)
plt.plot(log_val,arr,marker='*',color='green')

In the above example, we have used pyplot.plot() method to plot the log values against the original array values.

Output:

Graphical Representation Of Numpy Log
Graphical Representation Of Numpy Log

Conclusion

Thus, in this article, we have understood the working of Python NumPy log method along with different cases.


References