NumPy: Convert a given vector of integers to a matrix of binary representation
NumPy: Array Object Exercise-187 with Solution
Write a NumPy program to convert a given vector of integers to a matrix of binary representation.
Pictorial Presentation:
Sample Solution:
Python Code:
# Importing the NumPy library
import numpy as np
# Creating a NumPy array 'nums' containing a set of integers
nums = np.array([0, 1, 3, 5, 7, 9, 11, 13, 15])
# Displaying the original vector 'nums'
print("Original vector:")
print(nums)
# Creating a binary representation of 'nums' using bitwise operations and reshaping
# The resultant array represents the binary representation of each element in 'nums'
bin_nums = ((nums.reshape(-1, 1) & (2 ** np.arange(8))) != 0).astype(int)
# Displaying the binary representation of the vector 'nums'
print("\nBinary representation of the said vector:")
print(bin_nums[:, ::-1]) # Reversing the columns to display binary digits in the correct order
Sample Output:
Original vector: [ 0 1 3 5 7 9 11 13 15] Binary representation of the said vector: [[0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 1] [0 0 0 0 0 0 1 1] [0 0 0 0 0 1 0 1] [0 0 0 0 0 1 1 1] [0 0 0 0 1 0 0 1] [0 0 0 0 1 0 1 1] [0 0 0 0 1 1 0 1] [0 0 0 0 1 1 1 1]]
Explanation:
In the above exercise -
nums = np.array([0, 1, 3, 5, 7, 9, 11, 13, 15]): Creates a Numpy array with specified values.
bin_nums = ((nums.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
In the above code -
- nums.reshape(-1,1): Reshapes nums into a 2D column array of shape (9, 1).
- 2**np.arange(8): Creates an array of powers of 2 from 2^0 to 2^7.
- nums.reshape(-1,1) & (2**np.arange(8)): Applies the bitwise AND operation between the reshaped nums array and the powers of 2 array using broadcasting. This operation is a way to extract the binary digits for each number in nums.
- ((nums.reshape(-1,1) & (2**np.arange(8))) != 0): Compares the result of the bitwise AND operation with 0. If the result is not equal to 0, it returns True; otherwise, it returns False.
- ((nums.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int): Converts the boolean array obtained in the previous step into an integer array, where True becomes 1 and False becomes 0. This array represents the binary representation of the numbers in nums, but the order of the bits is reversed.
bin_nums[:,::-1]: Reverses the order of the bits in each row of the bin_nums array to obtain the correct binary representation.
print(bin_nums[:,::-1]): Prints the resulting array containing the binary representations of the numbers in “nums” with 8 bits.
Python-Numpy Code Editor:
Previous:Write a NumPy program to multiply an array of dimension (2,2,3) by an array with dimensions (2,2).
Next: Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/numpy/python-numpy-exercise-187.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics