Practical Exercises for Chapter 5
Exercise 1: Create an Array
Create a NumPy array containing integers from 1 to 10. Then reshape it into a 2x5 matrix.
Solution:
import numpy as np
arr = np.array(range(1, 11))
reshaped_arr = arr.reshape(2, 5)
print("Reshaped Array:\\\\n", reshaped_arr)
Exercise 2: Array Arithmetic
Given two arrays A = [1, 2, 3, 4, 5] and B = [5, 4, 3, 2, 1], perform element-wise addition, subtraction, multiplication, and division.
Solution:
A = np.array([1, 2, 3, 4, 5])
B = np.array([5, 4, 3, 2, 1])
addition = A + B
subtraction = A - B
multiplication = A * B
division = A / B
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
Exercise 3: Handling Missing Data
Create an array with the elements [1, 2, np.nan, 4, 5]. Compute the mean of the array, ignoring the np.nan value.
Solution:
arr_with_nan = np.array([1, 2...