What is numpy.log() in Python?

Learn via video courses
Topics Covered

Python has a library called Numpy for working with numerical data. As is well known, there are numerous functions in NumPy that can be used to perform mathematical operations on an array of data. One such function is called the numpy.log() function.

NumPy.log() is used to determine the natural logarithm of x, where x is one of the input array elements. We can calculate the natural log of single elements and elements of one-dimensional and two-dimensional arrays too.

Graphical Representation of NumPy log()

As discussed above, the natural logarithm of a number is the power to which e would have to be raised to be equal to the number. In this section, we will visualize natural logarithms graphically using Matplotlib.

Here's a plot of a strictly-increasing array of integers:

Output

Now, we will use the numpy.log() function to compute the natural logarithms of the array of integers:

Output

Hence, we have used the numpy.log() function to create an array of logarithms.

Syntax

The syntax for numpy log() is:

numpy.log(x, /, out=None, *, where=True)

Parameters

The parameters that numpy log() takes in are:

ParameterMeaning
arrThis mandatory parameter represents the input array for calculating log values.
outThis optional parameter represents the location where the result must be stored.
whereThis optional parameter, when set to True, the output array will be set to the result function.

Return Value

The numpy log() function returns a NumPy array which consists of Natural Logarithmic values of the input array.

How to Use numpy.log() in Python

Let's look at the usage of the NumPy log() function with the help of some examples.

Examples of NumPy log()

  • Using numpy.log() to compute the natural logarithm of two numbers

    With the help of numpy log(), we can calculate the natural logarithm of a scalar number, as well as elements of an array. In this example, we will calculate the natural logarithm of two numbers using numpy.log().

    Output

  • Using numpy.log() to compute element-wise natural logarithm of the array

    Just as we used numpy log() to compute the natural logarithm of two numbers, we can use numpy log() to calculate element-wise natural logarithm of an array too.

    Output

  • Using numpy.log() to compute the natural logarithm of a 2-D array

    We can calculate the natural logarithms of two-dimensional arrays using the numpy log() function. In this example, we will create a two-dimensional array using arange() and reshape() functions and then apply the numpy log() function to compute element-wise natural logarithms.

    Output

  • Log Values using logspace()

    Similar to numpy.log(), numpy.logspace() is used to generate an array of evenly spaced values between the given two numbers on a logarithmic scale. This function returns 50 values in the returned array only.

    Output

Conclusion

  • In this article, we learned about numpy.log(); a function used to calculate the natural logarithm of x, where x is a single number or an array of integers.
  • The graphical representation of numpy.log() is somewhat similar to that of the integer array itself.
  • For a better understanding, we looked through a lot of examples in which we used the numpy.log() function over a lot of different scenarios.