Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

Student Marks Analysis Project Using NumPy

In this practical project, we will build a student marks analysis system using NumPy. We will store student marks in arrays, calculate totals and averages, identify the highest and lowest marks, assign grades, check pass or fail status, rank students, and save the results to files. This project demonstrates how NumPy simplifies numerical analysis in real-world applications.

1. Project Objectives

After completing this project, you will be able to:

  • Create NumPy arrays for student names, subjects, and marks.
  • Calculate total marks and average marks for each student.
  • Find subject-wise averages and highest and lowest scores.
  • Apply conditions to identify passing and failing students.
  • Assign grades based on average marks.
  • Sort students according to their performance.
  • Export analysis results to a CSV file.
  • Save and reload numerical data using the NPZ format.

2. Project Requirements

Our institute, CIIT TRANING INSTITUTE, wants to analyze the marks of three students across three subjects: Python, NumPy, and Statistics.

Student Python NumPy Statistics
Samadhan 85 90 78
Pradnya 72 88 91
Samiksha 34 65 55
Project rule: Each subject is scored out of 100. A student must score at least 35 in every subject to pass. The grades in this project are illustrative and are based on the student's average marks.

3. Import NumPy and Create the Dataset

First, import NumPy. Store the student names and subject names separately from the numerical marks. The marks will be represented by a two-dimensional array.

import numpy as np students = np.array(["Samadhan", "Pradnya", "Samiksha"]) subjects = np.array(["Python", "NumPy", "Statistics"]) marks = np.array([ [85, 90, 78], [72, 88, 91], [34, 65, 55] ]) print("Students:") print(students) print("\nSubjects:") print(subjects) print("\nMarks:") print(marks)

Expected Output

Students: ['Samadhan' 'Pradnya' 'Samiksha'] Subjects: ['Python' 'NumPy' 'Statistics'] Marks: [[85 90 78] [72 88 91] [34 65 55]]

Each row of marks represents one student, and each column represents one subject. Therefore, the first row contains Samadhan's marks and the first column contains every student's Python marks.

4. Understand the Array Structure

Marks Matrix Python NumPy Statistics Samadhan Pradnya Samiksha 85 90 78 72 88 91 34 65 55
Rows represent students; columns represent subjects.
print("Array shape:", marks.shape) print("Number of dimensions:", marks.ndim) print("Total number of marks:", marks.size)

Expected Output

Array shape: (3, 3) Number of dimensions: 2 Total number of marks: 9

The shape (3, 3) means the array contains three rows and three columns. There are nine individual marks in total.

5. Calculate Total and Average Marks

Use np.sum() to calculate totals and np.mean() to calculate averages. The parameter axis=1 performs calculations across each row, producing one result per student.

total_marks = np.sum(marks, axis=1) average_marks = np.mean(marks, axis=1) print("Total marks:") print(total_marks) print("\nAverage marks:") print(np.round(average_marks, 2))

Expected Output

Total marks: [253 251 154] Average marks: [84.33 83.67 51.33]
Student Total Marks Average
Samadhan 253 / 300 84.33
Pradnya 251 / 300 83.67
Samiksha 154 / 300 51.33
Remember: axis=1 calculates across columns within each row. axis=0 calculates down the rows, giving one result per subject.

6. Find Highest and Lowest Marks for Each Student

The functions np.max() and np.min() can find the highest and lowest score in each student's row.

highest_per_student = np.max(marks, axis=1) lowest_per_student = np.min(marks, axis=1) print("Highest mark per student:") print(highest_per_student) print("\nLowest mark per student:") print(lowest_per_student)

Expected Output

Highest mark per student: [90 91 65] Lowest mark per student: [78 72 34]

Samadhan's highest mark is 90, Pradnya's is 91, and Samiksha's is 65. Samiksha's lowest mark is 34, which is below the passing requirement of 35.

7. Perform Subject-Wise Analysis

To calculate results for each subject, use axis=0. NumPy then calculates the mean, maximum, and minimum across all students for each subject.

subject_averages = np.mean(marks, axis=0) subject_highest = np.max(marks, axis=0) subject_lowest = np.min(marks, axis=0) print("Subject averages:") print(np.round(subject_averages, 2)) print("\nSubject highest marks:") print(subject_highest) print("\nSubject lowest marks:") print(subject_lowest)

Expected Output

Subject averages: [63.67 81. 74.67] Subject highest marks: [85 90 91] Subject lowest marks: [34 65 55]
Subject Average Highest Lowest
Python 63.67 85 34
NumPy 81.00 90 65
Statistics 74.67 91 55

8. Check Pass and Fail Status

A student passes only when every subject mark is at least 35. The expression marks >= 35 creates a Boolean array. Use np.all() with axis=1 to check every subject for each student.

passing_students = np.all(marks >= 35, axis=1) for i in range(len(students)): if passing_students[i]: status = "Pass" else: status = "Fail" print(students[i], "-", status)

Expected Output

Samadhan - Pass Pradnya - Pass Samiksha - Fail
Important: Checking only the average is not enough for this project's pass rule. Samiksha's average is above 35, but her Python score is 34. Since every subject must meet the minimum score, she fails.

9. Assign Grades Using Conditions

We will assign grades according to the following illustrative rules. These are project-specific rules and may differ from your institute's official grading policy.

Average Marks Grade
90 and aboveA+
80 to less than 90A
70 to less than 80B
60 to less than 70C
50 to less than 60D
Below 50F
grades = np.select( [ average_marks >= 90, average_marks >= 80, average_marks >= 70, average_marks >= 60, average_marks >= 50 ], ["A+", "A", "B", "C", "D"], default="F" ) for i in range(len(students)): print( students[i], "- Average:", round(average_marks[i], 2), "- Grade:", grades[i] )

Expected Output

Samadhan - Average: 84.33 - Grade: A Pradnya - Average: 83.67 - Grade: A Samiksha - Average: 51.33 - Grade: D

Notice that grade and pass/fail status are different calculations. Under the illustrative grading rules, Samiksha receives grade D but still fails the project because she scored below 35 in one subject.

10. Rank Students by Average Marks

Use np.argsort() to obtain the indices that sort average marks in ascending order. Reverse the result to display the highest average first.

ranking = np.argsort(average_marks)[::-1] print("Student ranking:") for position, index in enumerate(ranking, start=1): print( position, students[index], "- Average:", round(average_marks[index], 2) )

Expected Output

Student ranking: 1 Samadhan - Average: 84.33 2 Pradnya - Average: 83.67 3 Samiksha - Average: 51.33
How it works: np.argsort() returns positions, not the sorted student names. Use those positions to retrieve the corresponding names and averages.

11. Find the Top Student and Class Average

NumPy can identify the student with the highest average and calculate the overall average across all individual marks.

top_index = np.argmax(average_marks) class_average = np.mean(marks) print("Top student:", students[top_index]) print("Top average:", round(average_marks[top_index], 2)) print("Overall class average:", round(class_average, 2))

Expected Output

Top student: Samadhan Top average: 84.33 Overall class average: 73.11

The overall class average is the mean of all nine subject marks. Since every student has the same number of subjects, it is also equal to the mean of the three student averages.

12. Export Results to a CSV File

NumPy is useful for the calculations, while Python's built-in csv module is convenient for exporting a mixture of text and numerical values.

import csv with open("student_marks_report.csv", "w", newline="", encoding="utf-8") as file: writer = csv.writer(file) writer.writerow([ "Student", "Python", "NumPy", "Statistics", "Total", "Average", "Grade", "Status" ]) for i in range(len(students)): status = "Pass" if passing_students[i] else "Fail" writer.writerow([ students[i], marks[i, 0], marks[i, 1], marks[i, 2], total_marks[i], round(average_marks[i], 2), grades[i], status ]) print("Report saved to student_marks_report.csv")

Expected Output

Report saved to student_marks_report.csv

The file student_marks_report.csv is created in the program's current working directory. You can open it using a text editor or spreadsheet application.

File location: If you cannot find the CSV file, check the current working directory of your Python environment. The file is not automatically saved into your ASP.NET project's folder unless you specify that location.

13. Save and Load Data Using NPZ

The NPZ format stores multiple NumPy arrays in one compressed archive. It is useful when you want to save the original dataset and calculated results for later analysis.

np.savez_compressed( "student_marks_data.npz", students=students, subjects=subjects, marks=marks, total_marks=total_marks, average_marks=average_marks, grades=grades ) print("Data saved successfully.") with np.load("student_marks_data.npz") as data: loaded_students = data["students"] loaded_marks = data["marks"] loaded_averages = data["average_marks"] print("Loaded students:", loaded_students) print("Loaded marks:") print(loaded_marks) print("Loaded averages:", np.round(loaded_averages, 2))

Expected Output

Data saved successfully. Loaded students: ['Samadhan' 'Pradnya' 'Samiksha'] Loaded marks: [[85 90 78] [72 88 91] [34 65 55]] Loaded averages: [84.33 83.67 51.33]
Tip: Use np.save() for a single array, np.savez() for multiple arrays in an archive, and np.savez_compressed() when you also want compression.

14. Complete Project Code

The following script combines the key calculations, grading, ranking, CSV export, and NPZ persistence into one runnable program.

import numpy as np import csv # 1. Create dataset students = np.array(["Samadhan", "Pradnya", "Samiksha"]) subjects = np.array(["Python", "NumPy", "Statistics"]) marks = np.array([ [85, 90, 78], [72, 88, 91], [34, 65, 55] ]) # 2. Student-wise calculations total_marks = np.sum(marks, axis=1) average_marks = np.mean(marks, axis=1) highest_marks = np.max(marks, axis=1) lowest_marks = np.min(marks, axis=1) # 3. Subject-wise calculations subject_averages = np.mean(marks, axis=0) # 4. Pass/fail status passing_students = np.all(marks >= 35, axis=1) # 5. Assign grades grades = np.select( [ average_marks >= 90, average_marks >= 80, average_marks >= 70, average_marks >= 60, average_marks >= 50 ], ["A+", "A", "B", "C", "D"], default="F" ) # 6. Display report print("STUDENT MARKS REPORT") print("-" * 60) for i in range(len(students)): status = "Pass" if passing_students[i] else "Fail" print( f"{students[i]} | " f"Total: {total_marks[i]} | " f"Average: {average_marks[i]:.2f} | " f"Highest: {highest_marks[i]} | " f"Lowest: {lowest_marks[i]} | " f"Grade: {grades[i]} | " f"Status: {status}" ) # 7. Subject summary print("\nSUBJECT-WISE AVERAGES") for i in range(len(subjects)): print(f"{subjects[i]}: {subject_averages[i]:.2f}") # 8. Rank students ranking = np.argsort(average_marks)[::-1] print("\nRANKING") for position, index in enumerate(ranking, start=1): print( f"{position}. {students[index]} - " f"{average_marks[index]:.2f}" ) # 9. Export CSV report with open("student_marks_report.csv", "w", newline="", encoding="utf-8") as file: writer = csv.writer(file) writer.writerow([ "Student", "Python", "NumPy", "Statistics", "Total", "Average", "Grade", "Status" ]) for i in range(len(students)): status = "Pass" if passing_students[i] else "Fail" writer.writerow([ students[i], *marks[i], total_marks[i], round(average_marks[i], 2), grades[i], status ]) # 10. Save NumPy arrays np.savez_compressed( "student_marks_data.npz", students=students, subjects=subjects, marks=marks, total_marks=total_marks, average_marks=average_marks, grades=grades ) print("\nCSV report and NPZ data saved successfully.")

Run the complete script in your Python environment. It prints the report and creates student_marks_report.csv and student_marks_data.npz in the current working directory.

15. Common Mistakes

Mistake 1: Using the wrong axis

Using axis=0 for student totals calculates subject-wise totals instead. Use axis=1 when you need one result per student.

Mistake 2: Checking only the average for passing

A student may have a passing average but still fail one subject. Use np.all(marks >= 35, axis=1) to enforce the rule that every subject must meet the minimum.

Mistake 3: Mixing up names and sorted indices

np.argsort() returns indices. Use those indices to access the corresponding names and averages rather than treating the indices as marks.

Mistake 4: Trying to export mixed data as a purely numeric array

Student names and grades are text, while marks and averages are numerical. A CSV writer handles these different data types naturally.

Mistake 5: Forgetting to close a saved archive

Use with np.load(...) when reading NPZ files so the archive is closed automatically after the data has been read.

16. Practice Exercises

  1. Add a fourth student and update the marks array.
  2. Calculate the percentage for every student.
  3. Find the subject with the highest class average.
  4. Count how many students passed and how many failed.
  5. Display only students whose average is greater than 80.
  6. Find the difference between the highest and lowest mark in each subject.
  7. Change the passing threshold to 40 and recalculate the results.
  8. Load the NPZ archive and display the saved subject names.

Summary

In this project, you used NumPy arrays to organize student marks and calculate totals, averages, highest and lowest marks, subject-wise statistics, grades, pass/fail status, and student rankings. You also used Boolean conditions and axis-based operations to analyze the dataset efficiently.

Finally, you exported the report to CSV and saved numerical data in compressed NPZ format. These techniques provide a foundation for larger data-analysis projects involving student results, sales records, and other numerical datasets.