# np.log2: How to Implement Numpy log2() in Python

NumPy is a third-party library for the Python language, adding support for large, multi-dimensional arrays and matrices, accompanied by many high-level mathematical functions to execute on these arrays.

## np.log2

The np.log2() is a mathematical function that helps the user calculate the Base-2 logarithm of x where x belongs to all the input array elements. The numpy log2() function accepts two parameters and returns an array of natural logarithms where the base is 2.

The np.log2() function is used to get the natural logarithm of any object or an array. In this case, we have a base of 2 when we find the logarithm of the array.

### Syntax

`numpy.log2(array[, out] = ufunc ‘log2’)`

### Parameters

The log2() function can take up to two main arguments:

1. array: This is the input array or the object whose log is calculated.
2. out: This is an optional field. A position the result is stored in. If given, it must have a form to which the inputs convey. If not provided or None, it returns a freshly-allocated list. A tuple must have the same length as the number of outputs (possible only as a keyword argument).

### Return Value

The log2() function returns an array of natural logarithms of the given array elements where the base is 2.

## Program to show the working of numpy.log()

See the following code.

```# Program to show working of numpy.log

# Importing numpy
import numpy as np

# We will create an 1D array
arr = np.array([4, 14, 10, 63, 11, 4, 64])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Calculating natural log of value arr[i]+1
out = np.log2(arr)
print("Natural logarithm of the given array of base 2 is ")
print(out)
```

#### Output

```The array is:  [ 4 14 10 63 11  4 64]
Shape of the array is :  (7,)
Natural logarithm of the given array of base 2 is
[2.         3.80735492 3.32192809 5.97727992 3.45943162 2.
6.        ]```

#### Explanation

In this program, we have first declared an array of shape 7, we have printed the array. Then we have called numpy.log2() to calculate the natural logarithm of the elements of the given array.

## Graphical representation of log2()

See the following code.

```# Program to show Graphical representation

# Importing numpy
import numpy as np
import matplotlib.pyplot as plt

# We will create an 1D array
arr = np.array([40, 2.4, 14, 63, 1.2, 1, 4])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Calculating natural log of value arr[i]+1
out = np.log2(arr)
print("Natural logarithm of the given array of base 2 is ")
print(out)

# Ploting of original array in Graph
# Color will be in Green
plt.plot(arr, arr, color='green', marker='x')

# Ploting of natural log array in Graph
# Color will be in blue
plt.plot(out, arr, color='blue', marker='o')

# Showing the Graphical represntation
plt.title("numpy.log2()")
plt.xlabel("Natural Log Array")
plt.ylabel("Original Array")
plt.show()```

#### Output

```The array is:  [40.   2.4 14.  63.   1.2  1.   4. ]
Shape of the array is :  (7,)
Natural logarithm of the given array of base 2 is
[5.32192809 1.26303441 3.80735492 5.97727992 0.26303441 0.
2.        ]```

#### Explanation

In this program, we have first declared an array of shape 7; we have printed the array where array elements are in float data type. Then we have called numpy.log2() to calculate the natural logarithm of the elements of the given array.

After that, we have plotted the original array in a 2D graph which indicates using the Greenline. Then, finally, we plotted the out array, which we got after finding the natural logarithm, which shows using the blue line.

We can see the result in the above-given image.

That is it for the np.log2() function example.