Introduction
In this article, we will look at the NumPy Python library’s numpy.log2() function, which is used to calculate the logarithm base 2 of its inputs. With the help of several examples of the np.log2() function, we will explore its syntax and different uses.
Syntax of Numpy.log2()
The syntax of the Numpy Log2 function is as below –
numpy.log2(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.Log2() Function
Import Library
We begin by importing the Numpy library as follows.
In [0]:
import numpy as np
Example 1: Simple Usage of np.log() Function on Scalar
arr = np.array(4) result = np.log2(arr) print(result)
Out[1]:
2.0
Example 2: Using numpy.log2() on 1D Array
This example explains how to use the np.log2() function on a 1-D NumPy array. The log base 2 is calculated element by element in this case, and the final array is also one dimension.
In [2]:
arr = np.array([5,6,7]) result = np.log2(arr) print(result)
Out[2]:
[2.32192809 2.5849625 2.80735492]
Example 3: Using numpy.log2() on 2D Array
In this example, we apply element-wise log base 2 to a 2D array using the numpy.log2() method.
In [3]:
arr = np.array([[5, 3, 7], [7, 5, 6]]) result = np.log2(arr) print(result)
[[2.32192809 1.5849625 2.80735492] [2.80735492 2.32192809 2.5849625 ]]
Example 4: Calculating NumPy Log2 for 1
Using the NumPy log function, below we calculate the base 2 log of 1.
In [4]:
arr = np.array([1]) result = np.log2(arr) print(result)
[0.]
Example 5: Calculating NumPy Log2 for 0
arr = np.array([0]) result = np.log2(arr) print(result)
[-inf]
<ipython-input-6-57e42a678fd6>:2: RuntimeWarning: divide by zero encountered in log2 result = np.log2(arr)
Example 6: Calculating NumPy Log2 for Negative Number
The logarithm of a negative number results in a complex number. However, we must provide dtype=np.complex128 in the np.log2() function, or else the output will be nan.
In [6]:
negative_arr = np.array([-6, -3, -7]) log_negative = np.log2(negative_arr,dtype=np.complex128) print(log_negative)
[2.5849625 +4.53236014j 1.5849625 +4.53236014j 2.80735492+4.53236014j]
Example 7: Using Out Parameter
To use the out parameter, we first generate an empty array of the same size as the input array and provide it to the np.log2() function as an out parameter. The log output is saved in the out array, as shown below.
In [7]:
arr = np.array([3,6,9]) out_arr = np.empty(3) np.log2(arr, out=out_arr) print(out_arr)
[1.5849625 2.5849625 3.169925 ]
Example 8: Using Where Parameter
The where option supports conditional logarithm computation based on another boolean array. In this example, we set a condition arr > 2 and passed it in the where parameter to ensure that log is calculated only for array elements bigger than 2.
In [8]:
arr = np.array([1, 2, 3, 4, 5]) condition = arr > 2 where_result = np.log2(arr, where=condition) print(where_result)
[1. 1. 1.5849625 2. 2.32192809]