NumPy Advanced Challenges
Welcome to the advanced NumPy challenges at CIIT TRANING INSTITUTE. This lesson focuses on solving complex numerical problems using multidimensional arrays, advanced indexing, broadcasting, aggregation, vectorization, linear algebra, and random simulations. Each challenge includes a problem statement, a working solution, and its expected output.
1. Advanced Multidimensional Indexing
NumPy allows you to select elements from multidimensional arrays using row and column indices, slices, and index arrays. These techniques are useful when working with large datasets and selecting specific observations.
Challenge 1: Extract Specific Elements
Given a 3 × 4 matrix, extract the elements at positions (row 0, column 1), (row 1, column 2), and (row 2, column 3).
Expected output:
When index arrays are used together, NumPy selects corresponding row-column pairs rather than every possible combination.
Challenge 2: Select Every Other Column
Expected output:
The slice ::2 selects every second column, starting from
column index zero.
2. Advanced Boolean Masking
Boolean masking can combine several conditions to identify records
that meet multiple requirements. Use parentheses around each condition
when combining comparisons with & or |.
Challenge 3: Filter Values Within a Range
Select values greater than or equal to 20 and less than 60.
Expected output:
Challenge 4: Find Students Who Meet Two Requirements
Select students whose average mark is at least 80 and whose attendance is at least 75%.
Expected output:
Only Pradnya meets both conditions. Samiksha's average is high enough, but her attendance does not meet the threshold.
3. Broadcasting with Higher-Dimensional Arrays
Broadcasting is especially useful when applying different adjustments to rows or columns. Before performing an operation, compare the shapes of the arrays to make sure they are compatible.
Challenge 5: Normalize Values by Column
Subtract the mean of each column from every value in that column.
Expected output:
The shape of column_means is (3,).
Broadcasting aligns it with the three columns of the original matrix.
Challenge 6: Apply Row-wise Scaling
Expected output:
factors[:, np.newaxis] converts the one-dimensional
array into a column with shape (2, 1), allowing each
row to be multiplied by its own scaling factor.
4. Advanced Aggregation and Axis Control
Aggregation functions can operate along one or more axes. They help summarize datasets, identify unusual values, and compare rows or columns.
Challenge 7: Find the Maximum Value in Each Row
Expected output:
Challenge 8: Calculate Weighted Scores
Calculate each student's weighted score using weights of 20%, 30%, and 50% for three assessments.
Expected output:
Matrix multiplication combines each assessment score with its corresponding weight. The weights sum to 1, so the result is a weighted average.
5. Sorting, Ranking, and Top-N Selection
Sorting functions can order values or produce the indices needed to rank records.
Use np.argsort() when you need to preserve the relationship
between scores and their associated names.
Challenge 9: Find the Top Two Scores
Expected output:
np.argsort(scores) returns the indices that would sort
the scores in ascending order. Slicing selects the last two indices,
and [::-1] reverses them into descending order.
Challenge 10: Find the Index of the Largest Element
Expected output:
6. Vectorization and Conditional Transformation
Vectorized expressions can transform an entire dataset in one operation. They reduce repetitive code and often run more efficiently than explicit Python loops for numerical calculations.
Challenge 11: Apply Different Transformations
Increase values below 50 by 10 and reduce values of 50 or more by 5.
Expected output:
Challenge 12: Calculate Element-wise Differences
Expected output:
This pattern is useful for comparing actual measurements with predictions or expected values.
7. Advanced Reshaping and Splitting
Reshaping, stacking, and splitting help reorganize data before analysis. These operations are useful when combining datasets or separating training and testing records.
Challenge 13: Split an Array into Three Parts
Expected output:
np.split(data, 3) divides the array into three equal sections.
The number of sections must divide the array length evenly.
Challenge 14: Combine Arrays Vertically
Expected output:
Vertical stacking adds rows. The arrays must have compatible column dimensions.
8. Random Sampling and Reproducibility
NumPy's random generator can produce repeatable results when initialized with the same seed. This is important for testing, experiments, and simulations.
Challenge 15: Generate Repeatable Random Data
The generator produces five integers in the range 1 through 10,
because the upper bound of integers() is excluded.
Using the same seed and the same sequence of generator calls
makes the result reproducible in the same NumPy environment.
np.random.default_rng(seed) for new code instead of
relying on the legacy global random state.
9. Linear Algebra Challenge
NumPy provides matrix multiplication, determinants, and solutions
to systems of linear equations through numpy.linalg.
Challenge 16: Solve a System of Equations
Solve the following system:
2x + y = 5
x + 3y = 6
Expected output:
The returned array contains the solution values for x and y.
Use np.linalg.solve() when solving a square linear
system with a unique solution, rather than calculating the inverse
explicitly.
10. Final Advanced Project: Sales Data Analysis
In this project, analyze sales for three products across four months. Calculate total sales per product, monthly totals, identify the best-selling product, and find the month with the highest overall sales.
Complete Project Solution
Expected output:
The monthly index is zero-based, so index 3 refers to the fourth month. The project combines aggregation, indexing, and maximum-value selection into a practical data analysis workflow.
11. Advanced Practice Questions
Question 1: Replace Values Above a Threshold
Replace every value greater than 100 with 100.
Question 2: Calculate Row-wise Standard Deviations
NumPy calculates the population standard deviation by default,
using ddof=0.
Question 3: Find All Values Above the Mean
Question 4: Calculate a Matrix Product
Question 5: Count Occurrences of Each Value
12. Performance and Best Practices
- Prefer vectorized NumPy operations over unnecessary Python loops.
- Check array shapes before using broadcasting or matrix multiplication.
- Choose suitable data types to balance precision and memory usage.
- Use Boolean masks to filter records efficiently.
- Use
np.random.default_rng()for modern random-number generation. - Use
np.linalg.solve()for suitable linear systems instead of explicitly calculating an inverse. - Remember that basic slices usually return views, while advanced indexing generally returns copies.
- Validate input dimensions and handle empty arrays when building reusable functions.
Summary
In this lesson, you practiced advanced indexing, Boolean masking, broadcasting, axis-based aggregation, weighted calculations, sorting, vectorized transformations, array reshaping, random sampling, and linear algebra using NumPy.
Apply these techniques to practical datasets such as student marks, sales reports, scientific measurements, and numerical simulations. Complete the challenges independently and test your solutions with different input shapes and values before attempting the final assessment.