Rana Maninder Singh
Lesson 1 of 4Math for ML (bridge)

Vectors & matrices

Lesson opener

In one line: A vector is one customer's numbers in a fixed order — C0001 is [26, 35.17, 2] for tenure, monthly spend, tickets — and a matrix is many of those rows stacked.

Why it matters: Models do not read "Silver, two tickets." They read a list of numbers. If you swap spend and tickets, the model treats ₹2 as tenure. The order is the meaning.

Before this lesson: NumPy basics helps. Variables if you still need named fields. Same data/customers.csv.

Practitioner focus: Every array printed here is cut from the real 300-row file. Feature order is always tenure_months, monthly_spend, support_tickets. Pin numpy==2.1.3 and pandas==2.3.3.

What you will learn

By the end of this lesson you will be able to:

  1. Build one customer as a length-3 NumPy vector.
  2. State its shape (3,) and why order matters.
  3. Stack five customers into a matrix of shape (5, 3).
  4. Transpose that matrix and read the new shape (3, 5).
  5. Pull C0003's vector and see it is not C0001.
  6. Explain vector = one row, matrix = a batch.

Worked example: C0001 is a vector

Step 1 — Three numbers, one customer.

import pandas as pd
import numpy as np

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
x = df.iloc[0][cols].to_numpy(dtype=float)
print(x)
print(x.shape)

Expected output:

[26.   35.17  2.  ]
(3,)

26 months, ₹35.17 spend, 2 tickets. Shape (3,) means one axis, three slots. That is a vector.

Step 2 — Five customers is a matrix. Rows are people. Columns are the same three features.

import pandas as pd
import numpy as np

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
X = df.head(5)[cols].to_numpy(dtype=float)
print(X.shape)
print(X)

Expected output:

(5, 3)
[[ 26.    35.17   2.  ]
 [ 21.   121.3    0.  ]
 [ 28.    71.01   1.  ]
 [ 26.    30.12   6.  ]
 [ 33.     6.85   1.  ]]

Row 0 is C0001. Row 1 is C0002 (Platinum, ₹121.30 spend, 0 tickets). Shape (5, 3) = 5 rows, 3 columns.

Step 3 — Transpose swaps rows and columns. Now each column is a customer.

import pandas as pd
import numpy as np

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
X = df.head(5)[cols].to_numpy(dtype=float)
print(X.T.shape)

Expected output:

(3, 5)

You will see \(W^\top\) in later ML lessons. T means "swap the two axes."

Step 4 — C0003 is a different vector.

import pandas as pd
import numpy as np

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
x = df.loc[df["customer_id"] == "C0003", cols].iloc[0].to_numpy(dtype=float)
print(x)

Expected output:

[28.   71.01  1.  ]

Same three slots, different person. That is the whole idea of a feature vector.

Pause and think

Q1. What breaks if you put tickets before spend?

Every later weight multiplies the wrong field. The model is not "smart about columns" — it is a list of numbers.

Q2. Is (3,) the same as (1, 3)?

No. (3,) is one axis. (1, 3) is a one-row matrix. X @ w cares which one you pass.

Q3. What is the matrix of all 300 customers?

Shape (300, 3) with the same column order.

Guided practice: shape of all 300

import pandas as pd
import numpy as np

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
X = df[cols].to_numpy(dtype=float)
print(X.shape)

Expected output:

(300, 3)

Independent practice: C0004's tickets slot

Deliverable: print only the tickets value (index 2) from C0004's vector. C0004 has 6 tickets.

import pandas as pd
import numpy as np

df = pd.read_csv("data/customers.csv")
cols = ["tenure_months", "monthly_spend", "support_tickets"]
x = df.loc[df["customer_id"] == "C0004", cols].iloc[0].to_numpy(dtype=float)
print(x[2])

Expected output:

6.0

Common mistake

The deliberate mistake

Symptom: two teammates get different model scores on the "same" three features.

Diagnosis: one used [spend, tenure, tickets], the other used the order in this lesson.

Correction: write the column list once and reuse it.

Prevention: print x and x.shape before you multiply anything.

Where this connects

What you can now do:

  • See one customer as a vector and a batch as a matrix.
  • Read shape (n, 3) without flinching.

Next lesson: Dot product. A weight vector times a customer vector becomes one score.