Week 3 • NumPy Special

NumPy from ground 0 to expert point 100

This week is not only about learning a library. It is about changing the way students think about data. By the end of this lesson, they should stop seeing values as isolated numbers and start seeing them as structured arrays that can be sliced, reshaped, transformed, summarized, and prepared for machine learning.

The journey begins with the simple question, why not just use Python lists? It ends with matrix-style thinking, vectorization, broadcasting, aggregation, data preparation, and mini project work that feels close to real AI-ML practice.

Level 0 → 100 Array thinking Vectorization Broadcasting Matrix intuition ML preparation

By the end of Week 3, learners should be able to

1DUnderstand vectors as ordered numeric data
2DRead matrices and table-like structures
No loopsPrefer vectorized operations where possible
ReadyPrepare numeric data for later ML weeks

Learning ladder: 0 to 100

Every stage builds on the previous one. Students should feel the jump from beginner coding into data-science style thinking.

0–10Why NumPy exists and why Python lists are not enough.
10–25Create arrays, inspect types, compare lists and arrays.
25–45Master indexing, slicing, shape, reshape, and dimensions.
45–75Use vectorization, aggregation, and broadcasting with confidence.
75–100Think in matrices, prepare data, and solve real numeric tasks.

Part 1 — Ground zero: why NumPy matters

A student should first feel the problem before being shown the solution. Python lists are flexible, but NumPy arrays are built for numerical computing.

Plain-English idea

  • Python lists can store mixed values and are very flexible.
  • NumPy arrays are designed for numbers and large numeric work.
  • Operations on arrays are cleaner, faster, and more natural for data science.
  • Machine learning libraries expect structured numeric arrays.

Lists versus arrays

import numpy as np

a = [1, 2, 3]
b = [4, 5, 6]

print(a + b)          # list concatenation

x = np.array(a)
y = np.array(b)

print(x + y)          # element-wise addition
print(x * 2)          # vectorized multiplication
print(x ** 2)         # square each element

Key message

NumPy is not just another syntax chapter. It is the first serious step into numerical thinking. This is the week where students begin to think in data structures rather than only in statements and loops.

Part 2 — Creating arrays and reading them correctly

Students should learn that arrays can be one-dimensional, two-dimensional, or more. This helps them later with tables, images, and model inputs.

1D array

A 1D array is like a row of values. It can represent marks, temperatures, monthly sales, or sensor readings.

2D array

A 2D array is like a matrix or table. It can represent a mark sheet, image pixels, or rows and columns of structured values.

3D and beyond

Higher dimensions appear in image batches, video, and advanced ML workflows. Students do not need mastery now, but they should know the idea.

Common creation methods

import numpy as np

a = np.array([10, 20, 30, 40])
b = np.array([[1, 2, 3], [4, 5, 6]])

zeros = np.zeros((2, 3))
ones = np.ones((3, 2))
range_arr = np.arange(0, 10, 2)
line = np.linspace(0, 1, 5)

print(a)
print(b)
print(zeros)
print(ones)
print(range_arr)
print(line)

Important words

  • dtype: the type of values stored in the array
  • shape: the dimensions of the array
  • ndim: the number of dimensions
  • size: the total number of elements

A strong beginner should not memorize blindly. They should be able to say what these words mean in simple language.

Part 3 — Indexing and slicing mastery

This is where students begin to control data. If they cannot select the right rows, columns, and ranges, they cannot become effective in later weeks.

1D indexing and slicing

import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60])

print(arr[0])      # first item
print(arr[-1])     # last item
print(arr[1:4])    # 20, 30, 40
print(arr[:3])     # first three values
print(arr[::2])    # every second value

2D indexing and slicing

import numpy as np

mat = np.array([
    [11, 12, 13],
    [21, 22, 23],
    [31, 32, 33]
])

print(mat[0, 1])      # 12
print(mat[2, 2])      # 33
print(mat[0:2, 1:3])  # submatrix
print(mat[:, 0])      # first column
print(mat[1, :])      # second row

Teacher emphasis

Students often know the syntax but cannot describe what a slice returns. Make them say it in words before running the code.

Part 4 — Shape, reshape, transpose, and dimension awareness

Now the learner must stop seeing arrays as flat content and start seeing structure. This is a major turning point for AI-ML readiness.

Shape and reshape

import numpy as np

arr = np.arange(1, 13)
print(arr.shape)            # (12,)

mat = arr.reshape(3, 4)
print(mat)
print(mat.shape)            # (3, 4)

again = mat.reshape(2, 6)
print(again)

Transpose

import numpy as np

mat = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(mat.shape)   # (2, 3)
print(mat.T)
print(mat.T.shape) # (3, 2)
Concept Plain meaning Why it matters
shape rows and columns or dimensions Needed for matrix work and ML input design
reshape rearrange the same values into a new structure Common in data preparation
transpose turn rows into columns and columns into rows Important in matrix logic and feature arrangement
size total number of elements Helps validate whether reshape is possible

Part 5 — Vectorization: the great leap forward

This is one of the most important ideas of the week. Instead of processing items one by one with explicit loops, NumPy can operate on whole arrays at once.

Loop style

numbers = [1, 2, 3, 4, 5]
doubled = []

for n in numbers:
    doubled.append(n * 2)

print(doubled)

Vectorized style

import numpy as np

numbers = np.array([1, 2, 3, 4, 5])
doubled = numbers * 2

print(doubled)

Cleaner

The code is shorter and easier to read.

Faster

Under the hood, NumPy performs operations efficiently.

Scalable

The advantage becomes clearer with large datasets.

Expert habit

Whenever students write a loop over pure numbers, ask: Can this be done with array operations instead?

Part 6 — Broadcasting

Broadcasting feels magical at first. It lets NumPy stretch smaller values or arrays across larger structures when shapes are compatible.

Scalar broadcasting

import numpy as np

arr = np.array([5, 10, 15, 20])

print(arr + 3)
print(arr * 10)

Row-wise broadcasting

import numpy as np

mat = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

row_add = np.array([10, 20, 30])

print(mat + row_add)

Why broadcasting matters

  • Normalize values by subtracting means.
  • Add constants to entire datasets.
  • Adjust rows or columns in one step.
  • Write concise and professional numeric code.

Part 7 — Aggregation and summary thinking

A data student must not only store values. They must summarize them, compare them, and understand their spread.

Core summary functions

import numpy as np

marks = np.array([56, 78, 89, 91, 67, 74, 83])

print(np.sum(marks))
print(np.mean(marks))
print(np.min(marks))
print(np.max(marks))
print(np.std(marks))
print(np.median(marks))

Interpretation habit

  • sum tells total quantity.
  • mean gives central tendency.
  • min and max show range edges.
  • median helps when outliers distort the mean.
  • std gives spread intuition.

Axis-based summaries

import numpy as np

data = np.array([
    [80, 70, 90],
    [60, 75, 85],
    [88, 92, 79]
])

print(np.sum(data, axis=0))   # column sums
print(np.sum(data, axis=1))   # row sums
print(np.mean(data, axis=0))  # column means
print(np.max(data, axis=1))   # row max values

Part 8 — Matrix thinking for AI-ML

At the expert end of this week, students should begin to see matrices as the language of machine learning. Features, records, and model inputs often appear in array form.

Matrix multiplication intuition

import numpy as np

a = np.array([
    [1, 2],
    [3, 4]
])

b = np.array([
    [5, 6],
    [7, 8]
])

print(np.dot(a, b))
print(a @ b)

What students should understand

  • A row can represent one record.
  • A column can represent one feature.
  • Image pixels can be stored in 2D arrays.
  • Many ML models expect a matrix-shaped input.

This is not yet a full linear algebra week, but students should feel that arrays are the gateway to later ML mathematics.

Part 9 — Real-work style examples

Now we combine skills in realistic numeric mini-scenarios.

Example A — Student marks analysis

import numpy as np

marks = np.array([45, 67, 72, 89, 91, 54, 76, 88, 60, 79])

print("Average:", np.mean(marks))
print("Top score:", np.max(marks))
print("Below average count:", np.sum(marks < np.mean(marks)))
print("Sorted:", np.sort(marks))

Example B — Small image grid

import numpy as np

img = np.array([
    [0, 50, 100],
    [150, 200, 255]
])

normalized = img / 255
print(normalized)

Example C — Sales reshaping

A flat list of 12 monthly sales values can be reshaped into a 3 × 4 structure to represent three products across four months or four quarters across three regions.

Example D — Data cleaning step

Broadcasting can help subtract a baseline, scaling can normalize values, and boolean masking can isolate suspicious entries for review.

Part 10 — Common mistakes and how to beat them

Many NumPy mistakes are not advanced mathematical failures. They are usually shape confusion, indexing confusion, or list-versus-array confusion.

Mistake 1

Using Python lists and expecting NumPy-style arithmetic.

Mistake 2

Trying to reshape into incompatible dimensions.

Mistake 3

Not knowing whether axis 0 means rows or columns in context.

Mistake 4

Forgetting that slicing gives a range and not just one value.

Mistake 5

Avoiding 2D arrays because they look “too mathematical.”

Mistake 6

Writing loops for everything without checking for vectorization.

Part 11 — Practice path from beginner to expert

These are good checkpoints for lab discussion and oral questioning.

Level Student should be able to do Confidence sign
Beginner Create arrays, inspect shape, use simple slicing Can explain difference between list and array
Developing Reshape arrays, use sum/mean/min/max, perform vectorized arithmetic Can solve classroom tasks without much help
Strong Use broadcasting, axis summaries, and 2D indexing confidently Can debug shape problems
Expert-ready Apply arrays to marks, sales, image grids, and data preparation Can connect NumPy to later ML workflow

Week 3 completion checklist

  • I can explain why NumPy is used instead of plain lists for numerical work.
  • I can create 1D and 2D arrays correctly.
  • I can use indexing and slicing without guessing.
  • I can reshape arrays and understand shape.
  • I can use vectorized operations instead of loops in many cases.
  • I can use broadcasting in simple examples.
  • I can summarize data with mean, sum, min, max, and median.
  • I can explain how NumPy prepares me for ML.

Mini capstone for this week

Create a “Student Dataset Analyzer” using NumPy. Store marks for 20 students, compute class average, highest score, lowest score, above-average count, and a reshaped 4 × 5 display. Then write a short paragraph explaining what the results suggest.