3.14 Comparing Arrays and Advanced indexing Arrays: -
Comparing arrays: -
- A relational operator returns either TRUE or FALSE.
- Relational operator like >, >=, <, <=, ==, != can be used for comparing two arrays of same size.
Example:
-
Output:
-
Advanced
indexing: -
- Advanced indexing returns a copy of data.
- It is of two types:
1. Integer
indexing.
2. Boolean
indexing.
1) Integer
indexing: -
- Integer indexing in NumPy means accessing elements of an array using integer values that represent their positions (indices) in the array.
- It allows you to select specific elements from an array based on their index numbers, rather than slicing a continuous range.
Example:
-
Output:
-
2) Boolean
indexing: -
- Boolean indexing in NumPy means selecting elements from an array using True or False values.
- It allows you to filter elements that satisfy a condition.
Example:
-
Output:
-
v
Broadcasting in array: -
Ø
Broadcasting in NumPy means performing arithmetic
operations on arrays of different shapes (sizes) without actually copying data.
Ø
NumPy automatically “stretches” the smaller array so that
both arrays have the same shape for element-wise operations.
Ø
Broadcasting Rules: - When
operating on two arrays, NumPy compares their shapes from right to left:
1. If the
dimensions are equal, they are compatible.
Example: -
Output: -
2. If one of
them is 1, it can be stretched (broadcast).
Example: -
Output: -