Return the base 2 logarithm of the input array in Python

PythonServer Side ProgrammingProgramming

To return the base 2 logarithm of the input array, use the numpy.log2() method in Python Numpy The method returns Base-2 logarithm of x. This is a scalar if x is a scalar. The 1st parameter, x is the input value, array-like. The 2nd parameter is out, a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshlyallocated array is returned. A tuple must have length equal to the number of outputs.

The 3rd parameter is where, the condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Steps

At first, import the required library −

import numpy as np

Create an array using the array() method −

arr = np.array([1, 2, 10])

Display the array −

print("Array...\n", arr)

Get the type of the array −

print("\nOur Array type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nOur Array Dimension...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

To return the base 2 logarithm of the input array, use the numpy.log2() method −

print("\nResult...\n",np.log2(arr))

Example

import numpy as np

# Create an array using the array() method
arr = np.array([1, 2, 10])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# To return the base 2 logarithm of the input array, use the numpy.log2() method in Python Numpy
# The method returns Base-2 logarithm of x. This is a scalar if x is a scalar.
print("\nResult...\n",np.log2(arr))

Output

Array...
[ 1 2 10]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(3,)

Result...
[0. 1. 3.32192809]
raja
Updated on 24-Feb-2022 10:13:17

Advertisements