w3resource

NumPy: Check two random arrays are equal or not


10. Check Array Equality

Write a NumPy program to check two random arrays are equal or not.

Sample Solution:

Python Code :

# Importing the NumPy library as np
import numpy as np

# Generating the first array 'x' with 6 random integers (0 or 1) using np.random.randint()
x = np.random.randint(0, 2, 6)

# Printing the first generated array 'x'
print("First array:")
print(x)

# Generating the second array 'y' with 6 random integers (0 or 1) using np.random.randint()
y = np.random.randint(0, 2, 6)

# Printing the second generated array 'y'
print("Second array:")
print(y)

# Checking if the two arrays 'x' and 'y' are equal using np.allclose()
# This function returns True if the two arrays are element-wise equal within a tolerance
array_equal = np.allclose(x, y)

# Printing whether the two arrays are equal or not
print("Test above two arrays are equal or not!")
print(array_equal) 

Sample Output:

First array:                                                           
[1 0 0 0 1 0]                                                          
Second array:                                                          
[0 0 1 1 0 1]                                                          
Test above two arrays are equal or not!                                
False   

Explanation:

In the above exercise –

x = np.random.randint(0, 2, 6): This line creates a 1D array x with 6 random integers between 0 and 1 using the np.random.randint() function. The input values 0 and 2 specify the lower (inclusive) and upper (exclusive) boundaries for the random integers, and 6 is the size of the output array.

y = np.random.randint(0, 2, 6): This line creates another 1D array y with the same specifications as x.

array_equal = np.allclose(x, y): This line checks if the two arrays x and y are element-wise equal using the np.allclose() function. The function returns True if all elements in the arrays are approximately equal within a specified tolerance, and False otherwise. In this case, since we're comparing integers, the default tolerance is effectively zero, meaning that the function checks for exact equality.

Pictorial Presentation:

NumPy Random: Check two random arrays are equal or not.

For more Practice: Solve these Related Problems:

  • Write a function that compares two arrays element-wise and returns True if all elements match exactly.
  • Implement a solution that checks array equality using np.array_equal and np.allclose for floating-point arrays.
  • Create a program that reports the indices where two arrays differ if they are not equal.
  • Test the equality function on arrays with different shapes and dtypes to ensure robust error handling.

Go to:


PREV : Find Nearest Value in Array.
NEXT : Replace Maximum in Vector with -1.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.