9  Learning Objective Check

Welcome to the learning objective check!

This self-assessment is designed to help you review your understanding of the topics covered so far and to allow you to independently evaluate your learning progress. It is structured so that you can identify your strengths and weaknesses and work specifically on areas that still need improvement.

You have two options to test your knowledge. You can use the quiz, which will guide you automatically through the different topics. Alternatively, you will find standard questions below, similar to those used in the script so far.

Please take enough time to work through the questions calmly. Be honest with yourself and try to solve the tasks without any aids in order to get a realistic picture of your current level of knowledge. If you encounter difficulties with a question, it is an indication that you should practice more in that area.

Good luck with the exercises and your continued learning!

Task 1

How is the NumPy package typically imported?

Task 2

Use NumPy to create the following arrays:

  1. Convert the list [1, 2, 3] into a NumPy array
  2. A one-dimensional array containing the numbers from 0 to 9
  3. A two-dimensional array of shape 3×33×3 filled with ones
  4. A one-dimensional array containing numbers from 10 to 50 (inclusive) in steps of 5

Task 3

What is the difference between the functions np.ndim, np.shape, and np.size?

Task 4

What data type does the following array have? Which function can be used to read the data type of an array?

vector = np.array([ 4.8,  8.2, 15.6, 16.6, 23.2, 42.8 ])

Task 5

Perform the following mathematical operations on these two arrays:

a = [5, 1, 3, 6, 4] and b = [6, 5, 2, 6, 9]

  1. Add both arrays
  2. Calculate the element-wise product of a and b
  3. Add 3 to each entry in array a

Task 6

a = [9, 2, 3, 1, 3]

  1. Calculate the mean and standard deviation of array a
  2. Determine the minimum and maximum of the array

Task 7

matrix = np.array([
    [ 1,  2,  3,  4,  5],
    [ 6,  7,  8,  9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
])
  1. Extract the first row
  2. Extract the last column
  3. Extract the submatrix consisting of rows 2 to 4 and columns 1 to 3

Task 8

array = np.arange(1, 21)
  1. Reshape the array into a two-dimensional matrix with shape 4×5
  2. Reshape the array into a two-dimensional matrix with shape 5×4
  3. Reshape the array into a three-dimensional matrix with shape 2×2×5
  4. Flatten the three-dimensional array from Task 3 back into a one-dimensional array
  5. Transpose the 4×5 matrix from Task 1

Task 9

Which two functions can be used to read data from a file and save data to a file?

Task 10

You want to isolate the pixel data of one color channel from an image. What do you need to do?

Task 1

import numpy as np

Task 2

# 1.
np.array([1, 2, 3])

# 2.
print(np.arange(10))

# 3.
print(np.ones((3, 3)))

# 4.
print(np.arange(10, 51, 5))
[0 1 2 3 4 5 6 7 8 9]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[10 15 20 25 30 35 40 45 50]

Task 3

  • np.ndim: Returns the number of dimensions
  • np.shape: Returns the lengths of each dimension
  • np.size: Returns the total number of elements

Task 4

Since these are floating-point numbers, the data type is float64.

vector = np.array([ 4.8,  8.2, 15.6, 16.6, 23.2, 42.8 ])
print(vector.dtype)
float64

Task 5

a = np.array([5, 1, 3, 6, 4])
b = np.array([6, 5, 2, 6, 9])

# 1.
result = a + b
print("The sum of both vectors is:", result)

# 2.
result = a * b
print("The product of both vectors is:", result)

# 3.
result = a + 3
print("a plus 3 is:", result)
The sum of both vectors is: [11  6  5 12 13]
The product of both vectors is: [30  5  6 36 36]
a plus 3 is: [8 4 6 9 7]

Task 6

a = np.array([9, 2, 3, 1, 3])

# 1.
mean = np.mean(a)
print("Mean:", mean)

std_dev = np.std(a)
print("Standard deviation:", std_dev)

# 2.
minimum = np.min(a)
print("Minimum:", minimum)

maximum = np.max(a)
print("Maximum:", maximum)
Mean: 3.6
Standard deviation: 2.8000000000000003
Minimum: 1
Maximum: 9

Task 7

matrix = np.array([
    [ 1,  2,  3,  4,  5],
    [ 6,  7,  8,  9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
])

# 1. First row
print(matrix[0, :])

# 2. Last column
print(matrix[:, -1])

# 3. Submatrix from rows 2 to 4 and columns 1 to 3
print(matrix[1:4, 0:3])
[1 2 3 4 5]
[ 5 10 15 20 25]
[[ 6  7  8]
 [11 12 13]
 [16 17 18]]

Task 8

array = np.arange(1, 21)

# 1. Reshape to 4x5
matrix_4x5 = array.reshape(4, 5)

# 2. Reshape to 5x4
matrix_5x4 = array.reshape(5, 4)

# 3. Reshape to 2x2x5
matrix_2x2x5 = array.reshape(2, 2, 5)

# 4. Flatten
flattened_array = matrix_2x2x5.flatten()

# 5. Transpose the 4x5 matrix
transposed_matrix = matrix_4x5.T

# Optional outputs
print("Original array:", array)
print("4x5 matrix:\n", matrix_4x5)
print("5x4 matrix:\n", matrix_5x4)
print("2x2x5 matrix:\n", matrix_2x2x5)
print("Flattened array:", flattened_array)
print("Transposed 4x5 matrix:\n", transposed_matrix)
Original array: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]
4x5 matrix:
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]]
5x4 matrix:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]
 [17 18 19 20]]
2x2x5 matrix:
 [[[ 1  2  3  4  5]
  [ 6  7  8  9 10]]

 [[11 12 13 14 15]
  [16 17 18 19 20]]]
Flattened array: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]
Transposed 4x5 matrix:
 [[ 1  6 11 16]
 [ 2  7 12 17]
 [ 3  8 13 18]
 [ 4  9 14 19]
 [ 5 10 15 20]]

Task 9

The appropriate functions are np.loadtxt() and np.savetxt().

Task 10

Image data is typically stored as a 3D matrix, where each color component (usually red, green, and blue) is stored in a separate channel. If the image is stored in a matrix called data, you can isolate a color channel using slicing: data[:, :, 0], where 0, 1, or 2 selects the red, green, or blue channel respectively.