Exercises: NumPy for Data Analysis
starter.py below, then run
python3 starter.py from inside the folder you saved it
in. These exercises use real NumPy — install it first with
pip install numpy if you haven't already.
1 Building a grade array
Write make_grade_array(grades) — turn a plain Python list of numbers into a NumPy array.
2 Summarizing an array
Write array_summary(arr) — return a dict with the array's sum, mean, min, and max.
3 Filtering with a boolean mask
Write passing_scores(scores, passing=60) — return only the passing scores, using boolean masking.
4 Vectorized addition
Write add_bonus_points(scores, bonus) — add bonus points to every score at once.
5 Per-subject average
Write per_subject_average(grades_grid) — average each subject (column) across all students.
6 Per-student average
Write per_student_average(grades_grid) — average each student (row) across all subjects.
7 Reshaping
Write reshape_to_grid(flat_array, rows, cols) — reshape a flat array into a grid.
8 Combining arrays
Write combine_arrays(arr1, arr2) — join two arrays end-to-end.
🐞 Task 9 — Debug the Code
This is supposed to return the average of every subject (one value per column) in a 2D grades grid, but it returns per-student averages instead — the axis is backwards. Find and fix it.
def average_per_subject(grades_grid):
return grades_grid.mean(axis=1)
demo_grid = np.array([[90, 80], [70, 60], [100, 95]])
print(average_per_subject(demo_grid))