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

Dot product & matrix multiply

Lesson opener

In one line: A dot product multiplies matching slots and adds — C0001 · [0.02, 0.01, 0.5] is 1.8717 — and X @ w does that for every row at once.

Why it matters: Logistic regression and a first neural layer are this number. If you use * (element-wise) instead of @, you get three numbers back, not a score, and the next step silently goes wrong.

Before this lesson: Vectors & matrices. Same file, same three columns, same pins: numpy==2.1.3, pandas==2.3.3.

Practitioner focus: Weights [0.02, 0.01, 0.5] are a teaching choice (tickets count more than one month of tenure). They are not a fitted churn model. The printed 1.8717 is from C0001's real features.

What you will learn

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

  1. Compute a hand-checkable dot product on C0001.
  2. Repeat it with x @ w and get the same 1.8717.
  3. Score five customers with X @ w.
  4. Contrast * (element-wise) with @ (matrix multiply).
  5. Add a bias term b + x @ w.
  6. Explain why lengths must match.

Worked example: one score, then a batch

Step 1 — Multiply pairs, then add.

C0001 is [26, 35.17, 2]. Weights w = [0.02, 0.01, 0.5].

26×0.02 = 0.52

35.17×0.01 = 0.3517

2×0.50 = 1.00

Sum = 1.8717

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)
w = np.array([0.02, 0.01, 0.5])
print(round(float(x[0] * w[0] + x[1] * w[1] + x[2] * w[2]), 4))
print(round(float(x @ w), 4))

Expected output:

1.8717
1.8717

Same number twice — the @ is the pair-and-add.

Step 2 — Five scores in one multiply.

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)
w = np.array([0.02, 0.01, 0.5])
print(np.round(X @ w, 4))

Expected output:

[1.8717 1.633  1.7701 3.8212 1.2285]

C0004's 6 tickets × 0.5 is why that score jumps to 3.8212. The weight on tickets is doing real work.

*Step 3 — is not @.**

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)
w = np.array([0.02, 0.01, 0.5])
print(np.round(x * w, 4))
print((x * w).shape)

Expected output:

[0.52   0.3517 1.    ]
(3,)

Three products, no add. That is element-wise. A score is one number.

Step 4 — Bias is just a number you add.

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)
w = np.array([0.02, 0.01, 0.5])
b = -1.0
print(round(float(b + x @ w), 4))

Expected output:

0.8717

z = b + w · x is the linear score you will squash in math4.

Pause and think

Q1. Why must w have length 3?

Each slot of x needs a weight. (3,) @ (2,) is a shape error.

Q2. Who has the largest of the five scores and why?

C0004. Six tickets times 0.5 dominates.

Q3. What does X @ w need for X shape (300, 3)?

w shape (3,) or (3, 1). Result length 300.

Guided practice: C0003's score

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)
w = np.array([0.02, 0.01, 0.5])
print(round(float(x @ w), 4))

Expected output:

1.7701

Independent practice: mean score 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)
w = np.array([0.02, 0.01, 0.5])
print(round(float((X @ w).mean()), 4))

Expected output:

1.7264

Common mistake

The deliberate mistake

Symptom: you print three numbers and pass them to a sigmoid that expected one.

Diagnosis: you used * or np.multiply instead of @ / np.dot.

Correction: float(x @ w) for one row, X @ w for a batch.

Prevention: print .shape. A score is () or (n,), not (3,).

Where this connects

What you can now do:

  • Score one customer and a batch with the same weights.
  • Tell * and @ apart.

Next lesson: Loss & gradient descent. A score is a guess. Loss says how wrong, and a step updates w.