Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconData Analysis Foundations with Python
Data Analysis Foundations with Python

Chapter 5: NumPy Fundamentals

5.4 Practical Exercises for Chapter 5: NumPy Fundamentals

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, np.nan, 4, 5])
mean_without_nan = np.nanmean(arr_with_nan)
print("Mean without NaN:", mean_without_nan)

Exercise 4: Advanced NumPy Functions

Create a 3x3 matrix with random integers between 1 and 10. Then find the minimum and maximum values in the matrix.

Solution:

random_matrix = np.random.randint(1, 11, size=(3, 3))
print("Random Matrix:\\n", random_matrix)

min_value = np.min(random_matrix)
max_value = np.max(random_matrix)

print("Minimum Value:", min_value)
print("Maximum Value:", max_value)

These exercises should give you a practical understanding of the NumPy topics discussed in this chapter. Feel free to modify these exercises to better suit the context of your book.

5.4 Practical Exercises for Chapter 5: NumPy Fundamentals

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, np.nan, 4, 5])
mean_without_nan = np.nanmean(arr_with_nan)
print("Mean without NaN:", mean_without_nan)

Exercise 4: Advanced NumPy Functions

Create a 3x3 matrix with random integers between 1 and 10. Then find the minimum and maximum values in the matrix.

Solution:

random_matrix = np.random.randint(1, 11, size=(3, 3))
print("Random Matrix:\\n", random_matrix)

min_value = np.min(random_matrix)
max_value = np.max(random_matrix)

print("Minimum Value:", min_value)
print("Maximum Value:", max_value)

These exercises should give you a practical understanding of the NumPy topics discussed in this chapter. Feel free to modify these exercises to better suit the context of your book.

5.4 Practical Exercises for Chapter 5: NumPy Fundamentals

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, np.nan, 4, 5])
mean_without_nan = np.nanmean(arr_with_nan)
print("Mean without NaN:", mean_without_nan)

Exercise 4: Advanced NumPy Functions

Create a 3x3 matrix with random integers between 1 and 10. Then find the minimum and maximum values in the matrix.

Solution:

random_matrix = np.random.randint(1, 11, size=(3, 3))
print("Random Matrix:\\n", random_matrix)

min_value = np.min(random_matrix)
max_value = np.max(random_matrix)

print("Minimum Value:", min_value)
print("Maximum Value:", max_value)

These exercises should give you a practical understanding of the NumPy topics discussed in this chapter. Feel free to modify these exercises to better suit the context of your book.

5.4 Practical Exercises for Chapter 5: NumPy Fundamentals

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, np.nan, 4, 5])
mean_without_nan = np.nanmean(arr_with_nan)
print("Mean without NaN:", mean_without_nan)

Exercise 4: Advanced NumPy Functions

Create a 3x3 matrix with random integers between 1 and 10. Then find the minimum and maximum values in the matrix.

Solution:

random_matrix = np.random.randint(1, 11, size=(3, 3))
print("Random Matrix:\\n", random_matrix)

min_value = np.min(random_matrix)
max_value = np.max(random_matrix)

print("Minimum Value:", min_value)
print("Maximum Value:", max_value)

These exercises should give you a practical understanding of the NumPy topics discussed in this chapter. Feel free to modify these exercises to better suit the context of your book.