Exercise 1: Getting Started with NumPy
- Install NumPy using
pip
and verify the installation. - Create the following arrays using NumPy:
- A 1D array of integers from 1 to 10.
- A 2D array of shape (3, 4) filled with random values between 0 and 1.
- A 3D array of shape (2, 3, 4) filled with zeros.
- Print the properties of each array:
- Shape, size, number of dimensions, and data type.
Bonus: Modify the 2D array to round all values to 2 decimal places.
Exercise 2: Array Manipulations and Broadcasting
- Create a 2D array
A
of shape (4, 4) with values ranging from 1 to 16. - Create a 1D array
B
with values[1, 2, 3, 4]
. - Perform the following operations:
- Add
B
to each row ofA
using broadcasting. - Subtract
B
from each column ofA
using array transposition. - Compute the element-wise product of
A
andB
.
- Add
Bonus: Verify the shape compatibility for each operation using the .shape
property.
Exercise 3: Matrix Operations
- Generate two random matrices ( A ) and ( B ) of shape (3, 3).
- Compute the following:
- Matrix multiplication ( C = A \cdot B ).
- The transpose of ( A ) and ( B ).
- The determinant and rank of ( A ).
- Solve the linear system ( Ax = b ), where:
- ( A ) is one of the matrices.
- ( b = \begin{bmatrix} 1 \ 2 \ 3 \end{bmatrix} ).
Bonus: Validate the solution by checking if ( Ax \approx b ).
Exercise 4: Eigenvalues, Eigenvectors, and Matrix Decompositions
- Create a symmetric matrix ( A ) of size ( 3 \times 3 ) with random values.
- Compute the eigenvalues and eigenvectors of ( A ).
- Perform the following decompositions:
- Singular Value Decomposition (SVD).
- QR decomposition.
- Verify the decompositions:
- For SVD: Check if ( A \approx U \Sigma V^T ).
- For QR: Check if ( A \approx Q \cdot R ).
Exercise 5: Numerical Methods with SciPy
- Define the function ( f(x) = x^3 - 2x + 1 ).
- Perform the following:
- Compute the integral of ( f(x) ) from ( x = 0 ) to ( x = 2 ) using
scipy.integrate.quad
. - Solve the differential equation ( \frac{dy}{dt} = -y^2 + 1 ) with the initial condition ( y(0) = 0.5 ), over the interval ( t \in [0, 5] ) using
scipy.integrate.solve_ivp
. - Plot the solution of the differential equation.
- Compute the integral of ( f(x) ) from ( x = 0 ) to ( x = 2 ) using
Exercise 6: Data Analysis with Masked Arrays
- Simulate a dataset representing daily temperature readings for a year (365 values) using random values centered around ( 20^\circ C ) with a small random fluctuation.
- Introduce missing values (e.g., ( 10% )) represented as
np.nan
.
- Introduce missing values (e.g., ( 10% )) represented as
- Create a masked array to handle the missing values.
- Perform the following tasks:
- Calculate the mean and standard deviation of the valid data, ignoring the missing values.
- Replace the missing values with the average of the nearest valid neighbors.
- Plot:
- The original dataset with missing values (mark missing points distinctly).
- The cleaned dataset after filling in the missing values.
Bonus: Compare the cleaned dataset’s mean and standard deviation with those of the original (valid) data.