# Numpy log Base 2 | How to Implement Numpy log2()

We are all familiar with a logarithmic function. In mathematics, it plays a vital role. But many of us may think finding the log is something tedious process. Yes, I agree with it. But finding logarithmic using python is the easiest way. To find the logarithmic, we can use a NumPy module. The module has a function numpy log base 2. This function is useful to find the logarithmic function with a base value of 2.

NumPy log2() is a mathematical function in python. This is useful to find the base 2 log of x. In this x is an input array. This function accepts two parameters. One is an array, and the other is out, which is useful to store the output result. And the out parameter is an optional one. The function numpy log2() is useful to get the natural logarithm of any object or an array.

Contents

## What are the Syntax, Parameters, and Return type of Numpy log base 2?

### Syntax

`numpy.log2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'log2'>`

### Parameters

• x: input array
• out: location to save the result.
• where: If the value is True calculate the univerfunction. Otherwise, leave the value.
• **kwargs: arguments

### Returns

y: Base-2 logarithm of x.

## Numpy log base 2 for a single element

```import numpy as np
value = 8
print(f'The log value for {value} is: {np.log2(value)}')
```

Explanation

Import a numpy module. Declaring a value as 8. So now we are going to calculate log 8 base 2. Using np.log2() to get the log of the value.

Output

`The log value for 8 is: 3.0`

## Numpy log base 2 for an array of elements

### Example 1

```import numpy as np
input_array = [1,4,8,16]
print ("Given input ", input_array)
result = np.log2(input_array)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
```

Explanation

Import a numpy module. Declare an array of elements. So now we have to find the log value for each element that is present in an array. Using the formatted function to print the result for a better understanding.

Output

```Given input  [1, 4, 8, 16]
Result for 1 is: 0.0
Result for 4 is: 2.0
Result for 8 is: 3.0
Result for 16 is: 4.0```

### Example 2

```import numpy as np
array = np.arange(1,5).reshape(2,2)
print("Given array:\n",array)
print("Log value:\n",np.log2(array))
```

Explanation

Import a numpy module. Declaring an array of elements. Using reshape() to give the new shape to an array. So now we have to find the log value for each element that is present in an array.

Output

```Given array:
[[1 2]
[3 4]]
Log value:
[[0.        1.       ]
[1.5849625 2.       ]]```

## Graphical Representation of Log base 2

```import numpy as np
import matplotlib.pyplot as plt
input_array = [1,2,3,4]
result= np.log2(input_array)
plt.plot(input_array, input_array, color = "green", marker = ".")
plt.plot(result, input_array, color = "blue", marker = "*")
plt.title("numpy log base 2 graph")
plt.ylabel("input_array")
plt.xlabel("output")
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
plt.show()
```

Output

```Result for 1 is: 0.0
Result for 2 is: 1.0
Result for 3 is: 1.584962500721156
Result for 4 is: 2.0```

Graphical representation

## Calculate Numpy.log2() for a value e

```import numpy as np
value = np.e
print(np.log2(value))
```

Output

`1.4426950408889634`

## Numpy log base 2 for Complex Numbers

```import numpy as np
input_array=([6+4.j, 4+0.j, 0+8.j, 0+9.j])
result=np.log2(input_array)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
print (f'Result for {input_array} is:',result)
```

Till we saw how to calculate the log value for natural numbers, now we will move to complex numbers. Following the same steps, the only change is in the input value.

Output

```Result for (6+4j) is: (2.850219859070546+0.8483084401678749j)
Result for (4+0j) is: (2+0j)
Result for 8j is: (2.9999999999999996+2.2661800709135966j)
Result for 9j is: (3.1699250014423126+2.2661800709135966j)```

## Numpy logarithm with a Custom Base

This is another method to calculate the log value with any base value. The value with any base is possible in this method. Our article is about numpy base 2. So I am giving the base value as 2. To get the result, we can use the np.log value. But we have to give like:

`np.log(value)/np.log(base)`

### Code

```import numpy as np
value = 100
base=2
print("The log value is:",np.log(value)/np.log(base))
```

Output

`The log value is: 4.605170185988092`

## Bonus: Find Log base 2 by using math.log

```import math
a=int(input("Enter a value: "))
print(f'The log value for {a} is {math.log2(a)}')
```

Using the math.log() function to find the log value.

Output

`Enter a value: 8The log value for 8 is 3.0`

1. Is it possible to find the log value for complex numbers?

Yes, it is possible to find the log value for complex numbers.

2. How can we find the log value with any base?

np.log(value)/np.log(base) is useful to find the log value with any base.

## Conclusion

Finding a log is a little bit difficult process. But using numpy module. It becomes easier to find the logarithm of a number. We hope this article is beneficial. Kindly let us know if you have any queries regarding this article. We will be here to help you. Learn with us.

Subscribe
Notify of 