Class Activities Numpy Session 02-2
Class Activities Numpy Session 02-2
Task 1:
In this task, you will work with NumPy arrays and practice various slicing operations. Make sure to write your Python code
using NumPy to solve the following sub-parts. Use the provided NumPy arrays for your operations.
Extract elements from index 2 to 4. Store the result in a new array and display it.
2. Create a 2D NumPy array:
1 2 3
matrix = 4 5 6
7 8 9
Slice it to obtain the second column as a 1D array. Display the result.
Slice this tensor to get a 2D array with the shape (4, 5) by selecting the second 2D array along the first dimension.
Display the result.
Extract all even numbers from data using boolean indexing and store them in a new array. Display the resulting array.
1
Task 2:
Write Python code using NumPy to solve each sub-part. Use the provided NumPy arrays for your operations.
1. You are working with a grayscale image represented as a 2D NumPy array. The image is defined as follows:
[10 20 30 40 50]
[60 70 80 90 100]
image = [110 120 130 140 150]
[160 170 180 190 200]
[210 220 230 240 250]
Slice a specific region of interest (ROI) from the image (e.g., from row 2 to 4 and from column 2 to 4) and display only
that part.
2. Imagine you have a dataset of historical weather temperature records for a city stored in a 2D NumPy array. The dataset
is defined as follows:
[75.2 78.3 81.5 79.0 82.1]
[71.6 73.8 76.2 72.4 77.5]
temperature data = [69.3 70.7 73.2 71.1 74.8]
Slice the data to extract the stock prices for the second stock (row 1) and the time range from the first day to the third
day (columns 0 to 2).
4. You have a dataset of student exam scores stored in a 2D NumPy array, where each row represents a different student,
and each column is a different exam score. The dataset is defined as follows:
[85 92 78]
[90 88 76]
student scores = [78 85 92]
[95 91 89]
Slice the data to obtain the scores of the third student (row 2) and calculate their overall performance.
Task 3:
In this task, you will work with 3D NumPy arrays and practice slicing and indexing operations. Write Python code using
NumPy to solve the following sub-parts. Use the provided 3D NumPy arrays for your operations.
a) You have a 3D NumPy array cube with shape (3, 4, 5). The array is defined as follows:
[1 2 3 4 5] [21 22 23 24 25] [41 42 43 44 45]
[6 7 8 9 10] [26 27 28 29 30] [46 47 48 49 50]
cube =
[11
, ,
12 13 14 15] [31 32 33 34 35] [51 52 53 54 55]
[16 17 18 19 20] [36 37 38 39 40] [56 57 58 59 60]
2
• Extract the element at coordinates (2, 3, 4).
• Extract the second 2D array along the first dimension (at depth 1).
• Extract the first row of the second 2D array (at depth 1).
• Extract a 2x3 sub-matrix from the third 2D array (at depth 2) starting from the coordinates (1, 1).
b) You have a 3D NumPy array data cube with shape (4, 3, 3). The array is defined as follows:
[10 20 30] [110 120 130] [210 220 230] [310 320 330]
data cube = [40 50 60] , [140 150 160] , [240 250 260] , [340 350 360]
[70 80 90] [170 180 190] [270 280 290] [370 380 390]
Perform the following tasks:
• Extract the element at coordinates 2, 1, 2.
• Extract the entire 2D array at depth 3.
• Extract the last column of the first 2D array at depth 1.
• Extract a 2x2 sub-matrix from the third 2D array at depth 2 starting from the coordinates (0, 1).
c) You have a 3D NumPy array color cube with shape (2, 3, 4) representing a 3D RGB color image. The array contains
RGB values for a 2x3 grid of pixels for each of the 4 depth levels. The array is defined as follows:
[255 0 0] [128 128 0]
color cube = [0 255 0] , [0 128 128]
[0 0 255] [128 0 128]
• Extract the RGB value at coordinates (1, 2, 3).
• Extract the entire 2D array at depth 1.
• Extract the green channel (middle value) from the 2D array at depth 0.
• Extract a 2x2 sub-matrix from the first 2D array at depth 0 starting from the coordinates (0, 1).
Task 4:
In this task, you will work with NumPy arrays and practice boolean indexing operations. Write Python code using NumPy to
solve the following sub-parts. Use the provided arrays for your operations.
1. You have a 2D NumPy array matrix with shape (4, 4). The array is defined as follows:
1 2 3 4
5 6 7 8
matrix =
9 10 11 12
13 14 15 16
a) Find all elements in the 2D array matrix that are greater than 8.
b) Extract a sub-array from the 2D array matrix containing all elements that are divisible by 3.
2. You have a 3D NumPy array data cube with shape (4, 3, 3). The array is defined as follows:
10 20 30 110 120 130 210 220 230 310 320 330
data cube = 40 50 60 , 140 150 160 240 250 260 340 350 360
70 80 90 170 180 190 270 280 290 370 380 390
a) Find all elements in the 3D array data cube that are greater than 100.
b) Extract a sub-array from the 3D array data cube containing all elements that are divisible by 20.
c) Create a boolean mask for elements in the 3D array data cube greater than 250 and less than 350, and then use
the mask to extract the corresponding elements.
3
Task 5:
In this task, you will work with NumPy arrays and explore the concept of broadcasting. Write Python code using NumPy to
solve the following sub-parts.
array2 = [5]
3. Create a new NumPy array, array3, that is a result of adding a scalar value of 15 to array1 using broadcasting.
4. Create a new NumPy array, array4, that is a result of multiplying array1 by a scalar value of 3 using broadcasting.
5. Perform a subtraction operation using broadcasting to subtract array2 from array1.
3. Create a new NumPy array, matrix2, that is a result of adding a scalar value of 5 to matrix1 using broadcasting.
4. Create a new NumPy array, matrix3, that is a result of multiplying matrix1 by a scalar value of 2 using broadcasting.
5. Perform a division operation using broadcasting to divide each element of matrix1 by the corresponding element of
array3.