20 Essential NumPy Challenges to Master Matrix Operations
This article presents a curated set of twenty NumPy exercises covering array creation, searching, calculations, processing, conversion, and storage, each accompanied by concise Python code solutions to help readers deepen their understanding of matrix manipulation in Python.
Welcome to the sixth installment of the NumPy advanced practice series, offering a collection of twenty focused problems that emphasize matrix operations and data handling techniques.
Problem 41 – Data Creation
Task: Generate a 6×6 array with random integers from 1 to 100.
data = np.random.randint(1,100, [6,6])Problem 42 – Data Search
Task: Find the maximum value of each column.
np.amax(data, axis=0)Problem 43 – Data Search
Task: Find the minimum value of each row.
np.amin(data, axis=1)Problem 44 – Data Calculation
Task: Count the occurrences of each element in the array.
np.unique(data, return_counts=True)Problem 45 – Data Calculation
Task: Obtain the ranking of elements in each row.
data.argsort()Problem 46 – Data Processing
Task: Repeat each row once.
np.repeat(data, 2, axis=0)Problem 47 – Data Processing
Task: Remove duplicate rows.
np.unique(data, axis=0)Problem 48 – Data Sampling
Task: Sample three elements without replacement from the first row.
np.random.choice(data[0:1][0], 3, replace=False)Problem 49 – Data Calculation
Task: Identify elements in the second row that are not present in the third row.
a = data[1:2]
b = data[2:3]
index = np.isin(a, b)
array = a[~index]
arrayProblem 50 – Data Calculation
Task: Check whether the array contains any empty rows.
(~data.any(axis=1)).any()Problem 51 – Data Sorting
Task: Sort each row in ascending order.
data.sort(axis=1)Problem 52 – Data Conversion
Task: Convert the array to float type.
data1 = data.astype(float)Problem 53 – Data Modification
Task: Replace elements smaller than 5 with NaN.
data1[data1 < 5] = np.nanProblem 54 – Data Processing
Task: Delete rows that contain NaN values.
data1 = data1[~np.isnan(data1).any(axis=1), :]Problem 55 – Data Calculation
Task: Find the most frequent value in the first row.
vals, counts = np.unique(data1[0,:], return_counts=True)
print(vals[np.argmax(counts)])Problem 56 – Data Calculation
Task: Locate the element closest to 100.
a = 100
data1.flat[np.abs(data1 - a).argmin()]Problem 57 – Data Calculation
Task: Subtract the mean of each row from its elements.
data1 - data1.mean(axis=1, keepdims=True)Problem 58 – Data Calculation
Task: Normalize the array to the range [0, 1].
a = np.max(data1) - np.min(data1)
(data1 - np.min(data1)) / aProblem 59 – Data Calculation
Task: Standardize the array (zero mean, unit variance).
mu = np.mean(data1, axis=0)
sigma = np.std(data1, axis=0)
(data1 - mu) / sigmaProblem 60 – Data Storage
Task: Save the array to a local text file.
np.savetxt('test.txt', data1)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
