NumPy Math Games

PythonBeginner
Practice Now

Introduction

In this challenge, you will practice using the NumPy module in Python. You will implement several common functions to manipulate NumPy arrays, covering fundamental mathematical and array operations. The necessary files have been created for you in the file explorer on the left.

This is a Challenge, which differs from a Guided Lab in that you need to try to complete the challenge task independently, rather than following the steps of a lab to learn. Challenges are usually a bit difficult. If you find it difficult, you can discuss with Labby or check the solution. Historical data shows that this is a advanced level challenge with a 50% pass rate. It has received a 97% positive review rate from learners.

Element-wise Multiplication

Your first task is to create a function that multiplies two NumPy arrays element-wise. This means that each element in the first array is multiplied by the corresponding element in the second array.

TODO

  • Complete the multiply_arrays function in the multiply_arrays.py file.

Requirements

  • The function must be named multiply_arrays.
  • It must accept two NumPy arrays, a and b, as input.
  • It must return a new NumPy array which is the result of the element-wise multiplication of a and b.
  • The input arrays will have the same shape.

Example

After implementing the function, run the script to see the result:

python3 multiply_arrays.py

Output:

Input a: [1 2 3]
Input b: [4 5 6]
Element-wise multiplication result: [4 10 18]
Expected: [4 10 18]
✨ Check Solution and Practice

Matrix Multiplication

Next, you will implement matrix multiplication. Unlike element-wise multiplication, matrix multiplication follows specific rules of linear algebra and requires the inner dimensions of the two matrices to be compatible.

TODO

  • Complete the matrix_multiply function in the matrix_multiply.py file.

Requirements

  • The function must be named matrix_multiply.
  • It must accept two NumPy arrays, a and b, as input.
  • It must return a new NumPy array which is the result of the matrix product of a and b.
  • The input arrays will have compatible shapes for matrix multiplication.

Example

After implementing the function, run the script to see the result:

python3 matrix_multiply.py

Output:

Input matrix a:
[[1 2]
 [3 4]]
Input matrix b:
[[5 6]
 [7 8]]
Matrix multiplication result:
[[19 22]
 [43 50]]
Expected:
[[19 22]
 [43 50]]
✨ Check Solution and Practice

Transposing an Array

In this step, you will write a function to transpose a NumPy array. Transposing an array swaps its rows and columns.

TODO

  • Complete the transpose_array function in the transpose_array.py file.

Requirements

  • The function must be named transpose_array.
  • It must accept a single NumPy array, a, as input.
  • It must return the transposed version of the input array.

Example

After implementing the function, run the script to see the result:

python3 transpose_array.py

Output:

Original array:
[[1 2 3]
 [4 5 6]]
Transposed array:
[[1 4]
 [2 5]
 [3 6]]
Expected:
[[1 4]
 [2 5]
 [3 6]]
✨ Check Solution and Practice

Reshaping an Array

Now, you will create a function to reshape a NumPy array. Reshaping changes the dimensions of an array without changing its data. The total number of elements must remain the same.

TODO

  • Complete the reshape_array function in the reshape_array.py file.

Requirements

  • The function must be named reshape_array.
  • It must accept a NumPy array a and a tuple shape as input.
  • It must return a new array with the data from a but with the new dimensions specified by shape.

Example

After implementing the function, run the script to see the result:

python3 reshape_array.py

Output:

Original array: [1 2 3 4 5 6]
New shape: (2, 3)
Reshaped array:
[[1 2 3]
 [4 5 6]]
Expected:
[[1 2 3]
 [4 5 6]]
✨ Check Solution and Practice

Calculating Euclidean Distance

The Euclidean distance is a common way to measure the straight-line distance between two points. Your task is to implement a function that calculates this distance between two 1D NumPy arrays.

The formula for the Euclidean distance between two vectors a and b is:

d(a, b) = \sqrt{\sum\_{i=1}^{n}(a_i - b_i)^2}

TODO

  • Complete the euclidean_distance function in the euclidean_distance.py file.

Requirements

  • The function must be named euclidean_distance.
  • It must accept two 1D NumPy arrays, a and b, of the same length.
  • It must return a single floating-point number representing the Euclidean distance between them.

Example

After implementing the function, run the script to see the result:

python3 euclidean_distance.py

Output:

Point a: [1 2 3]
Point b: [4 5 6]
Euclidean distance: 5.196152422706632
Expected: 5.196152422706632
✨ Check Solution and Practice

Summary

In this challenge, you have practiced fundamental NumPy operations. You implemented functions for element-wise multiplication, matrix multiplication, array transposition, reshaping, and calculating Euclidean distance. These skills are essential for data analysis, machine learning, and scientific computing in Python.