NumPy Assignment– 2
Array Indexing & Slicing
Basic Questions
- Create a 1D array of numbers 0–9 and print the 3rd element using indexing.
- From a 1D array of numbers 10–19, slice and print elements from index 2 to 7.
- Generate a 1D array of numbers 0–20. Print elements from index 5 to the end.
- From the same array, print the first 5 elements using slicing.
- Demonstrate negative indexing: print the last element of a 1D array from 1–10.
- Slice with negative indices: print the last 3 elements of a 1D array from 1–10.
- Create a 2D 3×3 array with numbers 1–9. Print the element at row 1, column 2.
- From the same 2D array, extract the entire first row.
- Extract the entire second column from the 2D array.
- Print the sub-matrix containing the first two rows and first two columns.
- Create a 3D array of shape (2, 3, 4). Print the element at index [1, 2, 3].
- Print the second 2D slice (all rows, all columns) from the 3D array.
- Slice with step: from a 1D array of numbers 0–20, print every 2nd element.
- From the same array, print every 3rd element starting from index 2.
- Demonstrate reverse slicing: print a 1D array [1,2,3,4,5] in reverse order.
- Create a 1D array of numbers 1–10. Extract elements greater than 5 using boolean indexing.
- From the same array, extract only even numbers using boolean indexing.
- Create a 1D array [10, 20, 30, 40, 50]. Use fancy indexing to pick elements at indices [0, 2, 4].
- From the same array, use fancy indexing to repeat indices [1, 1, 3].
- Create a 2D 4×4 array with numbers 1–16. Extract elements at positions (0,0), (1,1), (2,2), (3,3) using fancy indexing.
Intermediate Questions
- Create a 1D array of numbers 50–100. Slice from index 10 to 20 with step 2.
- From the same array, extract the last 10 elements using slicing.
- Generate a 2D 5×5 array of numbers 1–25. Extract the last row.
- From the same array, extract the first column.
- Extract the middle 3×3 sub-matrix.
- From the 5×5 array, extract alternate rows (row 0, 2, 4).
- Extract alternate columns (column 0, 2, 4) from the 5×5 array.
- From a 2D array of numbers 1–16 reshaped to 4×4, print the elements at row indices [0,2] and column indices [1,3].
- Create a 3D array of shape (3,3,3). Extract all elements from the second layer.
- From the same 3D array, print the first row of the last layer.
- Using step slicing, reverse every row of a 2D array of numbers 1–12 reshaped to 3×4.
- From the same array, reverse the entire matrix (rows and columns both).
- Create a 1D array of numbers 1–15. Use boolean indexing to select elements divisible by 3.
- From the same array, select elements between 5 and 10 using boolean indexing.
- Create a 2D 4×4 array with random integers 1–50. Use boolean indexing to select all values greater than 25.
- Create an array [5, 10, 15, 20, 25]. Use fancy indexing to rearrange it as [25, 20, 15, 10, 5].
- From the same array, use fancy indexing to duplicate [10, 10, 25, 25].
- Create a 6×6 array using ‘np.arange(36).reshape(6,6)’. Extract a checkerboard pattern of even rows and odd columns.
- Demonstrate fancy indexing on a 2D array: pick elements from positions (0,1), (1,2), (2,3), (3,0).
- Compare the result of boolean indexing vs fancy indexing by selecting odd numbers from an array of 1–10 in both ways.
Advanced Questions
- Create a 10×10 array with numbers 1–100. Extract the border elements (first row, last row, first column, last column).
- From the same 10×10 array, extract the diagonal elements using fancy indexing.
- From a 3D array of shape (4,4,4), extract all elements where the sum of indices (i+j+k) is even.
- Generate a 2D 8×8 array and extract a chessboard pattern of 0s and 1s using slicing.
- Create a 1D array of numbers 1–50. Extract prime numbers using boolean indexing with a helper prime-checking function.
- Create a 6×6 array of numbers 1–36. Extract the central 2×2 block using slicing.
- From the same 6×6 array, extract all rows where the first element is greater than 20.
- Demonstrate fancy indexing to swap two columns of a 2D array (e.g., swap col 0 and col 2).
- Use boolean indexing to set all negative numbers of a random 5×5 integer array (-10 to 10) to zero.
- Create a 2D 7×7 array. Extract upper triangular elements (above the main diagonal) using fancy indexing.