Numpy log2 – Base-2 logarithm of x

Numpy Log2

Hello again! In this tutorial, we would be understanding the numpy.log2() function which is an inbuilt function of the NumPy library.

Also read: Numpy log10 – Return the base 10 logarithm of the input array, element-wise.

What is a Logarithm?

The exponent or power to which a base value is raised to obtain a number is called a Logarithm of that number. Confused yet? Let us understand with a simple example to clarify.

Let us consider the equation, bx = a, where x is the logarithm of a and b is the base. We express the logarithmic function as x = logb a.

For example, 53 = 125; therefore, 3 is the logarithm of 125, and 5 is the base value, or 3 = log5 125.

Note: The default base of a logarithm is 10 and is assumed when the base is not specified. It is written as log a.

What is Numpy Log2 – Base 2?

Numpy.log2() is one of the Mathematical Functions of the NumPy Library that computes the logarithmic value of the input number for base 2. It can be expressed mathematically as x = log2 a.

It can be used for both array-like inputs as well as single-object inputs. Numpy.log2() is a ufunc function; the ufunc function works on ndarrays in an element-by-element way. The function requires a set of parameters as listed below.

Syntax:

numpy.log2( input, out=None, *, where=True) 

Parameters:

  • input : Input values that can be arrays or objects whose logarithm to the base 2 is to be calculated.
  • out : [Optional] ndarray, tuple of ndarray or None . It the stores the result and the length is the same as the input. If none is provided a newly-allocated array is returned. A tuple (possible only as **kwargs) must be the same length as the number of outputs.
  • **kwargs are keyword arguments, used to handle named arguments in a function. They enable you to pass keyword variable length of argument to a function. Refer to the ufunc docs.
  • where : [Optional] array_like. If the condition is True, the ufunc will be calculated for the selected input. If it is set to false, no calculation will be made.

Output :

  • output : ndarray, Returns an array with Base-2 logarithmic value of x; where x belongs to all elements of the input array.

The logarithm is multifunction, meaning it is a many-valued function. For each a there is an infinite number of b; such that 2**b = a. The convention is to return the b whose imaginary part lies in [-π, π].

If the input is a real value, log2 always returns the real output. It returns a floating-point nan (Not a Number) value if the output cannot be expressed as a real number or infinity. Log2 treats the floating-point negative zero as an infinitesimal negative number if the input is a complex value.

Working with NumPy log2()

Let’s now start working on the numpy.log2() method to understand it through examples.

Example 1: Numpy log2() for an array of numbers

#includes numpy.log2() function
import numpy as np  

#pyplot for plotting the points on a graph
import matplotlib.pyplot as plt   

# declare an array list for input                 
x = np.array([0, 1, 2, 4, 9,])  

# use numpy.log2() on input array
out =np.log2(x)        
print(out)

#visualize output using plot function in matplotlib
plt.plot(out,x, color = 'orange', marker = ".")  

Note: The plot() function in the matplotlib library is used to plot points. In this case, we plot the output array. The plot() function by default connects the points with a line. The parameters are plot(x-coordinates, y-coordinates, colour, marker_type, linestyle, linewidth, marker size, markerfacecolor).

Output :

Log2 Output For Array 1
Log2 Output For Array 1

Example 2: Numpy log2() for exponentiated input

 #includes numpy.log2() function
import numpy as np  
import matplotlib.pyplot as plt   

# declare an array list for input                 
x = np.array([2**4, 3**3, 1**5, 4**2 ])  

# use numpy.log2() on input array
out =np.log2(x)        
print(out)

#visualize output using plot function in matplotlib
plt.plot(out,x, color = 'orange', marker = ".")  

The numpy and matplotlib.pyplot libraries are imported by this code. Matplotlib is utilized for data visualization, while the NumPy package is used for numerical operations.

  • The base-2 logarithm of each element in the “x” array is determined using the numpy.log2() method when the array is declared and includes some values. The variable “out” holds the log2 function’s output.
  • The output of the log2 function is shown using the print function.
  • The log2 function’s output is plotted on the x-axis and the original x array is plotted on the y-axis in the final line. The plot is configured to be orange, and the data points are represented on the plot by markers.
  • The base-2 logarithm of each element in the input array is returned by the code’s use of numpy.log2(), which is the natural logarithm (base 2) of the input array x. Then, using matplotlib.pyplot, the acquired output is plotted.

Output :

Log2 Output For Exponential Input 1
Log2 Output For Exponential Input

Example 3: Numpy log2() for real and complex numbers

 #includes numpy.log2() function
import numpy as np 
import matplotlib.pyplot as plt    

# declare an array list for input                 
x = np.array([2+4j, 0+1.j, 1, 2+0.j, 16.j])

# use numpy.log2() on input array
out =np.log2(x)        
print(out)

#visualize output using plot function in matplotlib
plt.plot(out,x, color = 'orange', marker = ".")  

Output :

Log2 Output For Complex Input 1
Log2 Output for real and complex  Input

Summary

In this article, We have covered the numpy.log2() function of the NumPy library. This function helps to calculate the logarithm of number with 2 as the base value. Various logarithm functions with different bases are available in NumPy. You can refer to the Numpy Mathematical functions for more such functions.

Reference:

You can also refer the documentation on the numpy.log2() function to gather more insight.