PYTHON FOR AI • LESSON 2

NumPy Project

In this project, we will use NumPy to analyze a small student score dataset. You will create arrays, inspect their shape, access values, calculate statistics, and perform mathematical operations.

PROJECT GOAL

Use NumPy to analyze real numerical data.

Instead of learning NumPy functions separately, we will combine them into one small project. This is how NumPy is actually used in AI and Machine Learning: data is stored in arrays and mathematical operations are performed on those arrays.

01

Project Scenario

Imagine that we have the scores of five students in three subjects:

Math    English    Science
80      75         90
90      85         88
70      80         75
85      95         92
60      70         65

We want NumPy to help us answer questions such as:

  • What is the average score?
  • What is the highest score?
  • What is the lowest score?
  • What is the average for each subject?
  • Which student's scores should we inspect?
02

Import NumPy

First, import NumPy using its standard alias np.

import numpy as np

The alias np makes NumPy functions shorter and easier to write.

03

Create the NumPy Array

Now we convert our student scores into a NumPy array.

scores = np.array([
    [80, 75, 90],
    [90, 85, 88],
    [70, 80, 75],
    [85, 95, 92],
    [60, 70, 65]
])

print(scores)
[[80 75 90]
 [90 85 88]
 [70 80 75]
 [85 95 92]
 [60 70 65]]

Each row represents one student, while each column represents one subject.

04

Check the Shape

The shape property tells us how many rows and columns the array contains.

print(scores.shape)
(5, 3)

This means:

  • 5 rows = 5 students
  • 3 columns = 3 subjects
05

Access a Student's Scores

NumPy uses zero-based indexing, just like Python lists.

To get the first student's scores:

print(scores[0])
[80 75 90]

To get the second student's scores:

print(scores[1])
[90 85 88]
06

Access a Single Score

We can access a specific row and column.

print(scores[0, 0])
80

This means:

scores[row, column]

scores[0, 0]

So we are accessing the first student's Math score.

07

Get Multiple Students

Slicing allows us to select part of the array.

first_three = scores[:3]

print(first_three)
[[80 75 90]
 [90 85 88]
 [70 80 75]]

This gives us the first three students.

08

Select One Subject

We can select an entire column.

math_scores = scores[:, 0]

print(math_scores)
[80 90 70 85 60]

The : means "all rows", while 0 selects the first column.

09

Calculate the Average

Now we can calculate the average of all scores.

average = np.mean(scores)

print(average)
81.0

NumPy looks at all 15 values and calculates their average.

10

Calculate the Average for Each Subject

We can calculate the average for each column using axis=0.

subject_averages = np.mean(scores, axis=0)

print(subject_averages)
[77. 81. 82.]

This means:

  • Math average = 77
  • English average = 81
  • Science average = 82

This is one of the most useful concepts in NumPy: performing calculations across an entire dimension.

11

Calculate the Average for Each Student

Now we want the average score for each student instead.

We use axis=1.

student_averages = np.mean(scores, axis=1)

print(student_averages)
[81.         87.66666667 75.         90.66666667 65.        ]

Each number represents one student's average.

12

Find Minimum and Maximum Scores

highest = np.max(scores)
lowest = np.min(scores)

print("Highest:", highest)
print("Lowest:", lowest)
Highest: 95
Lowest: 60

We can also find the highest score for each subject.

highest_by_subject = np.max(scores, axis=0)

print(highest_by_subject)
[90 95 92]
13

Add Bonus Points

Suppose every student receives 5 bonus points.

We can add 5 directly to the entire array.

updated_scores = scores + 5

print(updated_scores)
[[85 80 95]
 [95 90 93]
 [75 85 80]
 [90 100 97]
 [65 75 70]]

We did not need a loop. NumPy automatically added 5 to every value.

14

Normalize the Scores

In Machine Learning, data often needs to be transformed into a suitable numerical range.

For this simple example, we can divide the scores by 100 to convert them to values between 0 and 1.

normalized_scores = scores / 100

print(normalized_scores)
[[0.80 0.75 0.90]
 [0.90 0.85 0.88]
 [0.70 0.80 0.75]
 [0.85 0.95 0.92]
 [0.60 0.70 0.65]]

This is a simple example of scaling numerical data. Real Machine Learning preprocessing can use more sophisticated techniques, but the mathematical idea starts here.

15

Complete Project Code

Now let's put everything together into one small NumPy project.

Student Score Analyzer

This program creates the dataset and calculates useful statistics from it.

import numpy as np


# Student scores
scores = np.array([
    [80, 75, 90],
    [90, 85, 88],
    [70, 80, 75],
    [85, 95, 92],
    [60, 70, 65]
])


# Dataset information
print("Shape:", scores.shape)


# Overall statistics
print("Total:", np.sum(scores))
print("Average:", np.mean(scores))
print("Highest:", np.max(scores))
print("Lowest:", np.min(scores))


# Subject averages
subject_averages = np.mean(scores, axis=0)

print("\nSubject Averages:")
print(subject_averages)


# Student averages
student_averages = np.mean(scores, axis=1)

print("\nStudent Averages:")
print(student_averages)


# Add 5 bonus points
updated_scores = scores + 5

print("\nUpdated Scores:")
print(updated_scores)


# Normalize scores
normalized_scores = scores / 100

print("\nNormalized Scores:")
print(normalized_scores)
16

Understand What We Built

This project used almost everything we learned in the NumPy lessons.

  • Arrays — stored the student data.
  • Shape — checked rows and columns.
  • Indexing — accessed individual values.
  • Slicing — selected parts of the data.
  • Mathematical operations — calculated statistics.
  • Axis — calculated results by row or column.
  • Broadcasting — added bonus points to every score.
  • Normalization — transformed scores into a 0–1 range.

The important lesson is not memorizing every NumPy function. The important lesson is understanding how numerical data can be represented and manipulated as arrays.

KEY TAKEAWAY

NumPy is the foundation for working with numerical data in Python.

In this project, we used NumPy arrays to store data, inspect its shape, select values, calculate averages, find minimum and maximum values, perform mathematical operations, and transform the data. These same ideas appear throughout Data Science, Machine Learning, and AI. The next step is learning Pandas, which builds on these numerical concepts and makes working with structured datasets much easier.