NumPy Exercises

Welcome to The Coding College, your hub for coding knowledge and practical learning! This page offers carefully designed NumPy exercises to help you master the Python library for numerical computing. Practice these exercises to strengthen your understanding of array manipulation, mathematical operations, and more.

Why Practice NumPy?

NumPy is a foundational library in Python for data science, machine learning, and numerical computing. By practicing, you’ll:

  • Understand core NumPy concepts.
  • Build confidence in solving real-world problems.
  • Improve your coding efficiency and logic.

Getting Started

Prerequisites

Ensure you have NumPy installed in your Python environment. You can install it with the following command:

pip install numpy

Alternatively, use online compilers like Google Colab or Replit to start coding instantly.

NumPy Exercises

Exercise 1: Create a NumPy Array

Create a NumPy array with the following elements: 10, 20, 30, 40, 50.

Solution

import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr)

Exercise 2: Array Dimensions

Create a 3×3 matrix with values ranging from 1 to 9.

Solution

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)

Exercise 3: Array Indexing

Given the array arr = np.array([5, 10, 15, 20, 25]), access the element at index 3.

Solution

arr = np.array([5, 10, 15, 20, 25])
print(arr[3])  # Output: 20

Exercise 4: Array Slicing

Extract the first three elements from the array arr = np.array([2, 4, 6, 8, 10]).

Solution

arr = np.array([2, 4, 6, 8, 10])
print(arr[:3])  # Output: [2, 4, 6]

Exercise 5: Generate Random Numbers

Generate an array of 5 random integers between 1 and 100.

Solution

random_arr = np.random.randint(1, 101, size=5)
print(random_arr)

Exercise 6: Array Shape

Reshape the array arr = np.array([1, 2, 3, 4, 5, 6]) into a 2×3 matrix.

Solution

arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3)
print(reshaped_arr)

Exercise 7: Mathematical Operations

Perform element-wise addition on two arrays:

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

Solution

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = arr1 + arr2
print(result)  # Output: [5, 7, 9]

Exercise 8: Broadcasting

Add 5 to each element of the array arr = np.array([1, 2, 3, 4, 5]) using broadcasting.

Solution

arr = np.array([1, 2, 3, 4, 5])
result = arr + 5
print(result)  # Output: [6, 7, 8, 9, 10]

Exercise 9: Array Filtering

Filter out all even numbers from the array arr = np.array([1, 2, 3, 4, 5, 6]).

Solution

arr = np.array([1, 2, 3, 4, 5, 6])
filtered = arr[arr % 2 != 0]
print(filtered)  # Output: [1, 3, 5]

Exercise 10: Sorting

Sort the array arr = np.array([30, 10, 50, 20, 40]) in ascending order.

Solution

arr = np.array([30, 10, 50, 20, 40])
sorted_arr = np.sort(arr)
print(sorted_arr)  # Output: [10, 20, 30, 40, 50]

Advanced Exercises

Exercise 11: Dot Product of Two Matrices

Compute the dot product of the following matrices:

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

Solution

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
dot_product = np.dot(A, B)
print(dot_product)

Exercise 12: Generate a Gaussian Distribution

Create an array of 1000 random numbers following a normal (Gaussian) distribution with a mean of 0 and a standard deviation of 1.

Solution

gaussian = np.random.normal(0, 1, 1000)
print(gaussian)

Exercise 13: Unique Elements

Find the unique elements and their counts in the array arr = np.array([1, 2, 2, 3, 3, 3]).

Solution

arr = np.array([1, 2, 2, 3, 3, 3])
unique, counts = np.unique(arr, return_counts=True)
print("Unique elements:", unique)
print("Counts:", counts)

Next Steps

  1. Try solving these exercises without looking at the answers.
  2. Explore variations of these problems to enhance your understanding.
  3. Share your solutions and challenges on forums like The Coding College for feedback.

For more practice and coding resources, visit The Coding College and take your programming skills to the next level!

Leave a Comment