Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Matrix Multiplication and Dimension Checking

Recommended resources

Welcome back. In the previous lesson, a dot product combined one workspace’s feature vector with one vector of weights to produce a single score. Matrix multiplication is the same operation scaled up: it applies compatible collections of weighted sums in an organized way.

This lesson focuses on two habits that prevent most linear-algebra mistakes:

  1. Check dimensions before calculating.
  2. Compute each output entry as a row–column dot product.

By the end, you will be able to multiply small matrices manually and recognize the matrix operation behind producing predictions for many trial workspaces at once. Plan for roughly 40 minutes, including the short video and reading.


Matrix dimensions are part of the meaning

A matrix is a rectangular array. Its shape is written:

For example, a matrix with 2 rows and 3 columns has shape :

The dimensions are not merely bookkeeping. In a data-science setting, they usually represent distinct kinds of things:

  • rows of a feature matrix: observations, such as workspaces;
  • columns of a feature matrix: features, such as active days or teammates invited;
  • rows of a coefficient matrix: input features;
  • columns of a coefficient matrix: outputs, scores, or latent units.

The general matrix-multiplication pattern is:

The two inner dimensions, and , must match. If they do, the product exists; the outer dimensions determine the output shape.

A compact memory aid is:

Inside dimensions match; outside dimensions remain.

The diagram shows the dimension rule: an \(m \times n\) matrix \(A\) can multiply an \(n \times p\) matrix \(B\), producing an \(m \times p\) matrix \(C\). The shared inner dimension \(n\) disappears from the result.

For instance:

This is valid because the inner dimensions are both . In contrast,

is undefined: the first matrix has columns, but the second has rows.

The order matters. Matrix multiplication is generally not commutative: is not assumed to equal . Indeed, one order may be valid while the reverse order is invalid; and even if both products exist, their shapes and values may differ.

Why Matrix Multiplication Uses Rows & Columns (The Logic Explained)

Watch “Why Matrix Multiplication Uses Rows & Columns (The Logic Explained)” by Krista King. It gives a useful operational intuition: matrix multiplication chains two systems by connecting a shared intermediate category.

Watch the bakery example, focusing on how products connect to ingredients and ingredients connect to vendors. Then watch the dimension rule for the inside-match, outside-remain check. Finish with the worked calculations, noticing that every output cell comes from one row of the left matrix and one column of the right matrix.

The bakery example illustrates why the shared dimension must match. If the columns of the first matrix describe ingredients, then the rows of the second must describe prices for those same ingredients in the same order. Compatible dimensions allow the arithmetic, but semantic compatibility makes the result meaningful.

That distinction matters in production data work. Two arrays can have compatible shapes while representing mismatched feature orders. The code may run successfully and still calculate the wrong predictions.



What a product entry means: row by column

Suppose

Before doing arithmetic, inspect shapes:

The inner dimensions match, so is defined. The answer must have the outer dimensions:

Write an empty result matrix with that shape:

Each entry of is a dot product:

  • take a row from ;
  • take a column from ;
  • multiply corresponding entries;
  • add the products.

The top-left entry uses row 1 of and column 1 of :

The top-right entry uses the same first row of , but column 2 of :

Now use the second row of :

Therefore,

In general, if has shape and has shape , then the entry in row , column of is:

The index runs across the shared dimension. This formula formalizes “row of dotted with column of .”

5.4 - A Matrix Formulation of the Multiple Regression Model | STAT 462

Read the “Matrix multiplication” portion of this Penn State STAT 462 page. It connects the dimension rule and row-by-column calculation directly to the regression expression X\beta, which will be central throughout this course.

In the “Matrix multiplication” section, begin with the statement that multiplication is restricted by matrix shapes. Read the dimension rule. Then continue through the numerical example immediately below, including the calculations of c_{11}, c_{12}, and c_{23}. Track two things: predict the shape before calculating, and identify precisely which row and column produce each displayed entry.



From one prediction to many predictions

The dot product from the previous lesson is already a matrix multiplication:

A dataset simply stacks many feature vectors together as rows.

Suppose there are trial workspaces and features. The design matrix has shape :

For a single linear model, collect the feature weights in a column vector:

Its shape is . Thus:

The output has one row per workspace:

Each row of is dotted with the same coefficient vector . So matrix multiplication means: apply the same weighted-sum model to every workspace at once.

Consider three workspaces and the three features from the previous lesson:

Dimension check:

The predictions without an intercept are:

If the model has intercept , add it to each score:

The matrix multiplication does not itself turn these scores into probabilities. Later, logistic regression will apply a nonlinear transformation to scores of this form. But the dimensional logic of remains the same.



Implementing the shape contract in NumPy

For model code, let dimensions make the intended calculation obvious:

import numpy as np

X = np.array([
    [4.0, 3.0, 2.0],
    [2.0, 0.0, 1.0],
    [6.0, 5.0, 3.0],
])  # shape: (3, 3)

beta = np.array([
    [2.0],
    [0.8],
    [1.5],
])  # shape: (3, 1)

b = 5.0

linear_scores = X @ beta
predictions = linear_scores + b

print(X.shape)             # (3, 3)
print(beta.shape)          # (3, 1)
print(linear_scores.shape) # (3, 1)
print(predictions)

The expression X @ beta means matrix multiplication. It is not the same as X * beta, which is elementwise multiplication under NumPy’s broadcasting rules.

A useful explicit validation is:

assert X.ndim == 2
assert beta.ndim == 2
assert X.shape[1] == beta.shape[0]

The final assertion tests the formal multiplication rule: number of columns in equals number of rows in beta.

In typical scikit-learn usage, coefficients are often stored as a one-dimensional array of shape (d,), rather than a column matrix. Then this is also valid:

beta_1d = np.array([2.0, 0.8, 1.5])  # shape: (3,)

scores = X @ beta_1d
print(scores.shape)  # (3,)

Numerically, this represents the same three scores. The distinction is mainly one of representation:

  • (d, 1) makes the column-vector mathematics explicit and yields (n, 1);
  • (d,) is convenient in NumPy and yields a one-dimensional array (n,).

In a real feature pipeline, shape checks are necessary but not sufficient. You also need a feature-order contract. If beta[0] belongs to active_days, then the first column of X must also be active_days.

A safer pattern is to define the order once and enforce it:

feature_names = [
    "active_days",
    "teammates_invited",
    "key_actions",
]

X = df.loc[:, feature_names].to_numpy()

This protects against a particularly dangerous failure mode: a valid matrix multiplied by a valid coefficient vector whose columns and coefficients refer to different features. Dimensions would pass; the model would not be meaningful.



Key takeaways

Matrix multiplication has a precise shape rule:

The inner dimensions must match. The outer dimensions give the shape of the result.

To calculate a product entry, take a row from the left matrix and a column from the right matrix, then compute their dot product:

For predictive modeling, the central application is:

This applies one weighted-sum model to observations simultaneously. Before trusting such a calculation, check both the numerical shapes and the semantic alignment of feature columns with coefficient entries.

Next, you will use matrices to solve a small system of linear equations and see why singular systems correspond to redundant information in a feature matrix.