Python

NumPy np.log() Function

This article will discuss how to compute the natural logarithms of an array using the NumPy log functions.

Numpy np.log() Function

The np.log() function in NumPy allows you to calculate the natural logarithm of all the elements in an array.

The function syntax is shown below:

np.log(array, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'log'>

The function parameters are explored, as shown below:

  1. array – the input array under which the function is applied
  2. out – allows you to specify an output array with the same shape as the input. This value is set to None by default, and the function returns a new array
  3. dtype – the data type of the output array

The previous function parameters are essential parameters of the log function.

Example

Consider the following example that illustrates how to use the np.log() function on a 1-dimensional array.

Start by importing NumPy, as provided below:

# import numpy
import numpy as np

Create a 1D array, as shown below:

arr = [2, 8, 32, 128, 512]

Next, we can call the log function and pass the previous array, as shown below:

print(f"output: {np.log(arr)}")

The previous code should return an array with the natural logarithm of each element in the input array.

The corresponding output array is shown below:

output: [0.69314718 2.07944154 3.4657359  4.85203026 6.23832463]

Example

The same case applies to a multidimensional array.

Start by creating a 3D array as shown below:

# 3d array
arr = [[ 3,  2,  4,  8],
       [ 10,  45,  3,  16],
       [27,  9,  6,  3],
       [64, 16,  4,  1]]

Next, run the log function with the previous array as the input, as shown in the following example code:

print(f"output: {np.log(arr)}")

The resulting array is as shown:

Log to Base 2

NumPy provides us with the np.log2() function that allows you to calculate the logarithm of an input array to base 2.

The syntax is as shown below:

numpy.log2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'log2'>

We can illustrate how to use this function with the following example:

Example

Create a 3D array, as shown below:

# 3d array
arr = [[ 3,  2,  4,  8],
       [ 10,  45,  3,  16],
       [27,  9,  6,  3],
       [64, 16,  4,  1]]

Run the np.log2 function against the array to return the logarithm of the elements to base 2, as shown below:

display(np.log2(arr))

This should return:

array([[1.5849625 , 1.        , 2.        , 3.        ],
       [3.32192809, 5.4918531 , 1.5849625 , 4.        ],
       [4.7548875 , 3.169925  , 2.5849625 , 1.5849625 ],
       [6.        , 4.        , 2.        , 0.        ]])

Log to Base 10

Similarly, you can determine the logarithm of elements to base 10 using the np.log10 function.

An example usage is shown below:

display(np.log10(arr))

This should return an array, as shown below:

array([[0.47712125, 0.30103   , 0.60205999, 0.90308999],
       [1.        , 1.65321251, 0.47712125, 1.20411998],
       [1.43136376, 0.95424251, 0.77815125, 0.47712125],
       [1.80617997, 1.20411998, 0.60205999, 0.        ]])

Conclusion

In this article, we discussed how to determine the natural logarithm of an array using the log() function in NumPy. We also covered how to calculate the logarithm of an array to base 2 and base 10 using the log2() and log10() functions, respectively. Check out other Linux Hint articles or https://en.wikipedia.org/wiki/Logarithm for more tips and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list