Lab 1

Exercise 1: Getting Started with NumPy

  1. Install NumPy using pip and verify the installation.
  2. 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.
  3. 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

  1. Create a 2D array A of shape (4, 4) with values ranging from 1 to 16.
  2. Create a 1D array B with values [1, 2, 3, 4].
  3. Perform the following operations:
    • Add B to each row of A using broadcasting.
    • Subtract B from each column of A using array transposition.
    • Compute the element-wise product of A and B.

Bonus: Verify the shape compatibility for each operation using the .shape property.


Exercise 3: Matrix Operations

  1. Generate two random matrices ( A ) and ( B ) of shape (3, 3).
  2. Compute the following:
    • Matrix multiplication ( C = A \cdot B ).
    • The transpose of ( A ) and ( B ).
    • The determinant and rank of ( A ).
  3. 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

  1. Create a symmetric matrix ( A ) of size ( 3 \times 3 ) with random values.
  2. Compute the eigenvalues and eigenvectors of ( A ).
  3. Perform the following decompositions:
    • Singular Value Decomposition (SVD).
    • QR decomposition.
  4. 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

  1. Define the function ( f(x) = x^3 - 2x + 1 ).
  2. 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.

Exercise 6: Data Analysis with Masked Arrays

  1. 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.
  2. Create a masked array to handle the missing values.
  3. 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.
  4. 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.