# NumPy Log Base 2 Tutorial | numpy.log2() in Python ## 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 :

`import numpy as np`

### Example 1: Simple Usage of np.log() Function on Scalar

In this example, we use the numpy.log2() function on a scalar value, thus the result is also a scalar.
In :
```arr = np.array(4)
result = np.log2(arr)
print(result)```

Out:

`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 :

```arr = np.array([5,6,7])
result = np.log2(arr)
print(result)```

Out:

`[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 :

```arr = np.array([[5, 3, 7],
[7, 5, 6]])

result = np.log2(arr)
print(result)```
Out:
```[[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 :

```arr = np.array()
result = np.log2(arr)
print(result)```
Out:
`[0.]`

### Example 5: Calculating NumPy Log2 for 0

In this example, we use the NumPy log2 function to calculate the log base 2 of 0. The result is infinite, and a warning is also printed.
In:
```arr = np.array()
result = np.log2(arr)
print(result)```
Out:
```[-inf]
```
```<ipython-input-6-57e42a678fd6>:2: RuntimeWarning: divide by zero encountered in log2
result = np.log2(arr)
```
Out:
`[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 :

```arr = np.array([3,6,9])
out_arr = np.empty(3)
np.log2(arr, out=out_arr)
print(out_arr)```
Out:
`[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 :

```arr = np.array([1, 2, 3, 4, 5])
condition = arr > 2
where_result = np.log2(arr, where=condition)
print(where_result)```
Out:
`[1. 1. 1.5849625 2. 2.32192809]`