Fundamentals 13 min read

20 Essential NumPy Challenges with Complete Solutions

This article presents twenty classic NumPy problems covering array lookup, modification, conversion, sampling, slicing, string operations, rounding, reshaping, linear algebra, and more, each accompanied by concise Python code examples and visual illustrations to help you master advanced data manipulation techniques.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
20 Essential NumPy Challenges with Complete Solutions

Hello everyone, it's time for the NumPy advanced practice series.

01 Data Lookup

Question: How to obtain the common elements between two arrays?

Input:

import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)

Answer:

arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)
print("arr1: %s" % arr1)
print("arr2: %s" % arr2)
np.intersect1d(arr1, arr2)

02 Data Modification

Question: How to delete elements of one array that exist in another array?

Input:

arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)

Answer:

arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
print("arr1: %s" % arr1)
print("arr2: %s" % arr2)
np.setdiff1d(arr1, arr2)

03 Data Modification

Question: How to set an array to read‑only mode?

Input: arr1 = np.random.randint(1,10,10) Answer:

arr1 = np.random.randint(1,10,10)
arr1.flags.writeable = False

04 Data Conversion

Question: How to convert a Python list to a NumPy array?

Input: a = [1,2,3,4,5] Answer:

a = [1,2,3,4,5]
np.array(a)

05 Data Conversion

Question: How to convert a pandas DataFrame to a NumPy array?

Input:

df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})

Answer:

df.values

06 Data Analysis

Question: How to perform descriptive statistics on a NumPy array?

Input:

arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)

Answer:

print("Mean: %s" % np.mean(arr1))
print("Median: %s" % np.median(arr1))
print("Variance: %s" % np.var(arr1))
print("Std dev: %s" % np.std(arr1))
print("Covariance matrix: %s" % np.cov(arr1, arr2))
print("Correlation matrix: %s" % np.corrcoef(arr1, arr2))

07 Data Sampling

Question: How to perform probability sampling with NumPy?

Input:

arr = np.array([1,2,3,4,5])
np.random.choice(arr, 10, p=[0.1,0.1,0.1,0.1,0.6])

08 Data Creation

Question: How to create a copy of a NumPy array?

Input: arr = np.array([1,2,3,4,5]) Answer:

# Modifying the copy does not affect the original
arr = np.array([1,2,3,4,5])
arr1 = arr.copy()

09 Data Slicing

Question: How to slice an array from index 2 to 8 with a step of 2?

Input: arr = np.arange(10) Answer:

arr = np.arange(10)
a = slice(2,8,2)
arr[a]  # equivalent to arr[2:8:2]

10 String Operations

Question: How to concatenate and title‑case strings with NumPy?

Input:

str1 = ['I love']
str2 = [' Python']

Answer:

# Concatenate strings
str1 = ['I love']
str2 = [' Python']
print(np.char.add(str1, str2))
# Capitalize first letters
str3 = np.char.add(str1, str2)
print(np.char.title(str3))

11 Data Modification

Question: How to round numbers up or down in a NumPy array?

Input: arr = np.random.uniform(0,10,10) Answer:

arr = np.random.uniform(0,10,10)
print(arr)
# ceil
print(np.ceil(arr))
# floor
print(np.floor(arr))

12 Format Modification

Question: How to suppress scientific notation when printing NumPy arrays?

Answer:

np.set_printoptions(suppress=True)

13 Data Modification

Question: How to reverse rows or columns of a 2‑D NumPy array?

Input: arr = np.random.randint(1,10,[3,3]) Answer:

arr = np.random.randint(1,10,[3,3])
print(arr)
print('Column reverse')
print(arr[:, -1::-1])
print('Row reverse')
print(arr[-1::-1, :])

14 Data Lookup

Question: How to find elements in NumPy by position using np.take?

Input:

arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)

Answer:

arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)
print(arr1)
print(arr2)
print(np.take(arr2, arr1))

15 Data Calculation

Question: How to compute the remainder of two numbers with NumPy?

Input:

a = 10
b = 3

Answer:

np.mod(a, b)

16 Matrix SVD

Question: How to perform singular value decomposition on a matrix?

Input: A = np.random.randint(1,10,[3,3]) Answer:

np.linalg.svd(A)

17 Data Filtering

Question: How to filter NumPy data with multiple conditions?

Input: arr = np.random.randint(1,20,10) Answer:

arr = np.random.randint(1,20,10)
print(arr[(arr>1)&(arr<7)&(arr%2==0)])

18 Data Classification

Question: How to label elements greater than or equal to 7 or less than 3 as 1, others as 0?

Input: arr = np.random.randint(1,20,10) Answer:

arr = np.random.randint(1,20,10)
print(arr)
print(np.piecewise(arr, [arr<3, arr>=7], [-1, 1]))

19 Matrix Compression

Question: How to remove single‑dimensional entries from an array shape (squeeze)?

Input: arr = np.random.randint(1,10,[3,1]) Answer:

arr = np.random.randint(1,10,[3,1])
print(arr)
print(np.squeeze(arr))

20 Solving Linear Equations

Question: How to solve the linear system Ax = b with NumPy?

Input:

A = np.array([[1,2,3],[2,-1,1],[3,0,-1]])
b = np.array([9,8,3])

Answer:

A = np.array([[1,2,3],[2,-1,1],[3,0,-1]])
b = np.array([9,8,3])
x = np.linalg.solve(A, b)
print(x)

These twenty classic NumPy questions cover a broad range of array operations and provide a solid foundation for further data‑science work.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

machine learningPythonscientific computingArray Operations
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.