For a comprehensive introcution of NumPy, refer to:

**Introduction to Array Comparison**

Imagine you’re trying to find out which students scored **above average** in a test.

Instead of checking scores one by one, wouldn’t it be convenient to compare the entire list at once? That’s where array comparison comes into play.

**Element-wise Comparison**

Now, let’s get our hands dirty with some coding! When you compare arrays element-wise, you’re checking each element against a corresponding element in another array.

```
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([1, 0, 3, 4])
print(a == b)
```

**Understanding **`==`

`==`

**Function**: Compares two arrays element-wise.**Arguments**: Two arrays you want to compare.**Output**: A boolean array, where`True`

indicates the elements are equal, and`False`

indicates they are not.

Output:

`[ True False True True]`

In the above code, the second element of `a`

and `b`

are different (2 and 0), hence the `False`

in the output.

**Array-wise Comparison**

While element-wise comparison checks each corresponding element, array-wise comparison checks if two arrays are entirely identical.

`print(np.array_equal(a, b))`

Output:

`False`

**Understanding **`np.array_equal`

`np.array_equal`

**Function**: Compares if two arrays are completely identical.**Arguments**: The two arrays you want to compare.**Output**: A single boolean value.`True`

if the arrays are identical,`False`

otherwise.

In our example, since `a`

and `b`

are not identical, the output is `False`

.

**Comparison with Broadcasting**

Broadcasting is a powerful feature in NumPy that lets you compare arrays of different shapes. It’s like fitting mismatched puzzle pieces together seamlessly.

```
c = np.array([[1, 2], [3, 4]])
print(c > 2)
```

Output:

```
[[False False]
[ True True]]
```

Here, each element of `c`

is compared to `2`

. Elements greater than `2`

return `True`

, and the rest return `False`

.

**Understanding Broadcasting in Comparisons**

**Function**: Compares each element of the larger array with the entire smaller array.**Arguments**: Two arrays of different shapes.**Output**: A boolean array based on the comparison.

**Logical Operations on Boolean Arrays**

Once you have boolean arrays, you can perform logical operations like `and`

, `or`

, and `not`

.

```
x = np.array([True, False, True])
y = np.array([False, False, True])
print(np.logical_and(x, y))
```

Output:

`[False False True]`

**Understanding **`np.logical_and`

`np.logical_and`

**Function**: Performs element-wise logical`and`

operation.**Arguments**: Two boolean arrays.**Output**: A boolean array based on the logical`and`

operation.

In the example, the last elements of both `x`

and `y`

are `True`

, so the `and`

operation returns `True`

for them.

**Conditional Selection**

Once you have a boolean array from comparisons, you can use it to conditionally select elements from an array. This is a powerful way to filter data.

```
data = np.array([10, 20, 30, 40, 50])
mask = data > 30
print(data[mask])
```

Output:

`[40 50]`

**Understanding Conditional Selection**

**Function**: Filters elements from an array based on a condition.**Arguments**: A data array and a boolean mask.**Output**: An array with elements where the mask is`True`

.

In our example, we selected only the elements of `data`

that are greater than `30`

.

`np.where`

Function

`np.where`

Function`np.where`

is a versatile function that returns indices where a condition is `True`

.

```
result = np.where(data > 30)
print(result)
```

Output:

`(array([3, 4]),)`

**Understanding **`np.where`

`np.where`

**Function**: Finds indices where a condition is`True`

.**Arguments**: A condition.**Output**: A tuple containing indices.

Here, the elements `40`

and `50`

(at indices `3`

and `4`

) satisfy the condition.

**Using Masks with Multi-dimensional Arrays**

Masks aren’t limited to one-dimensional arrays. They work seamlessly with multi-dimensional arrays too.

```
matrix = np.array([[5, 10], [15, 20], [25, 30]])
mask_matrix = matrix > 15
print(mask_matrix)
```

Output:

```
[[False False]
[False True]
[ True True]]
```

**Understanding Multi-dimensional Masks**

**Function**: Applies a condition to a multi-dimensional array.**Arguments**: A multi-dimensional array.**Output**: A boolean matrix based on the condition.

In the example, elements greater than `15`

in the `matrix`

are marked as `True`

.