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.
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_arraysfunction in themultiply_arrays.pyfile.
Requirements
- The function must be named
multiply_arrays. - It must accept two NumPy arrays,
aandb, as input. - It must return a new NumPy array which is the result of the element-wise multiplication of
aandb. - 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]
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_multiplyfunction in thematrix_multiply.pyfile.
Requirements
- The function must be named
matrix_multiply. - It must accept two NumPy arrays,
aandb, as input. - It must return a new NumPy array which is the result of the matrix product of
aandb. - 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]]
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_arrayfunction in thetranspose_array.pyfile.
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]]
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_arrayfunction in thereshape_array.pyfile.
Requirements
- The function must be named
reshape_array. - It must accept a NumPy array
aand a tupleshapeas input. - It must return a new array with the data from
abut with the new dimensions specified byshape.
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]]
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_distancefunction in theeuclidean_distance.pyfile.
Requirements
- The function must be named
euclidean_distance. - It must accept two 1D NumPy arrays,
aandb, 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
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.



