Introduction
In this article, we will learn about numpy.log() function of NumPy Python library which is used to calculate the natural logarithm (base e) of its inputs. We shall see its syntax and its various usages with the help of multiple examples of np.log() function.
Syntax of Numpy.log()
The syntax of the Numpy Log function is as below –
numpy.log(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj])
- x: It denotes the input array or object that can be converted to an array.
- out: It is the optional parameter that specifies the array where the result will be stored as output.
- Other parameters like where, casting, order, dtype, subok, signature, and extobj are optional and not much used.
Examples of Numpy Log Function
Import Library
We start by importing the Numpy library as follows.
In [0]:
import numpy as np
Example 1: Simple Usage of np.log() Function on Scalar
In this example, we apply numpy.log() function on scalar value and hence the output is also scalar.
In [1]:
arr = np.array(6) result = np.log(arr) print(result)
1.791759469228055
Example 2: Using numpy.log() on 1D Array
This example shows the use of np.log() function on a 1-D NumPy array. Here the natural log is calculated element-wise and the resulting array is also of one dimension.
In [2]:
arr = np.array([4,5,6]) result = np.log(arr) print(result)
[1.38629436 1.60943791 1.79175947]
Example 3: Using numpy.log() on 2D Array
In this example, we use the Numpy Log function on a 2D array which applies element-wise natural log on it.
In [3]:
arr = np.array([[5, 2, 4], Â Â Â Â Â Â Â Â [4, 5, 6]]) result = np.log(arr) print(result)
Out[3]:
[[1.60943791 0.69314718 1.38629436] [1.38629436 1.60943791 1.79175947]]
Example 4: Calculating NumPy Log for 1
Below we are calculating the natural log of 1 by using the NumPy log function.
In [4]:
arr = np.array([1]) result = np.log(arr) print(result)
Out[4]:
[0.]
Example 5: Calculating NumPy Log for 0
In this example, we are calculating the natural log of 0 with the NumPy log function. The output is infinity and a warning is also printed.
In [5]:
arr = np.array([0]) result = np.log(arr) print(result)
Out[5]:
[-inf]
<ipython-input-10-db880431ab61>:2: RuntimeWarning: divide by zero encountered in log result = np.log(arr)
Example 6: Calculating NumPy Log for Negative Number
The logarithm of a negative number results in a complex number. However, while using the np.log() function we have to pass dtype=np.complex128 otherwise the output will be nan.
In [6]:
negative_arr = np.array([-2, -3, -4]) log_negative = np.log(negative_arr,dtype=np.complex128) print(log_negative)
Out[6]:
[0.69314718+3.14159265j 1.09861229+3.14159265j 1.38629436+3.14159265j]
Example 7: Using Out Parameter
For using the out parameter, we first create an empty array of the same size as the input array and pass it as an out parameter to the np.log() function. We can see below that the log output is saved in the out array.
In [7]:
arr = np.array([5,6,7]) out_arr = np.empty(3) np.log(arr, out=out_arr) print(out_arr)
Out[7]:
[1.38629436 1.60943791 1.79175947]
Example 8: Using Where Parameter
The where parameter allows conditional computation of the logarithm based on another boolean array. In this example, we have created a condition arr > 2 such and pass it in where parameter so that log is calculated only for array elements that are greater than 2.
In [8]:
arr = np.array([1, 2, 3, 4, 5]) condition = arr > 2 where_result = np.log(arr, where=condition) print(where_result)
[1. 1. 1.09861229 1.38629436 1.60943791]