Matrix-Vector Products for Model Predictions

Resumen

Applying a trained model to a full dataset means turning learned weights into predictions, and the matrix-vector product is the operation that makes it possible. You will learn how to align dimensions, use transpose in NumPy, and run predictions over multiple examples in parallel.

What is a matrix-vector product and why does it matter for predictions?

When a model finishes training, it stores its knowledge in a vector of weights. To use that knowledge on new data, you need an operation called inference, and the matrix-vector product is its mathematical engine.

Think of it like this: each row of your data matrix is one example, and the weight vector holds what the model considers important. Multiplying them with a dot product gives you a single prediction per row. Stack all those results and you get a new vector of predictions.

What is inference in machine learning? It is the step where you take the weights a model learned during training and use them to compute outputs for new data it has never seen.

This operation sits at the core of linear models and neural networks. Every time a model predicts at scale, it is essentially running many dot products in parallel.

How do you fix incompatible shapes with transpose in NumPy?

For the multiplication to work, dimensions must match. The number of columns in the matrix has to equal the number of rows in the vector. And here is where most beginners hit a wall.

Sometimes your weight vector loads as a row vector with shape (1, 2) when you actually need a column vector with shape (2, 1). The math refuses to cooperate, and NumPy throws an error.

The fix is the transpose operation, written as .T in NumPy. Its only job is to swap rows and columns:

  • A matrix of shape (3, 2) becomes (2, 3).
  • A row vector of shape (1, 5) becomes a column vector of shape (5, 1).
  • A column vector of shape (2, 1) becomes a row vector of shape (1, 2).

What does .T do in NumPy? It transposes the array, swapping its rows and its columns so the shapes align for matrix operations.

With transpose you rotate your arrays until the rule "columns of the first equal rows of the second" is satisfied.

How to multiply a matrix by a vector in NumPy step by step

Start by importing NumPy and creating a small dataset. Imagine three students described by two features: study hours and sleep hours.

python import numpy as np

X = np.array([ [10, 8], [5, 7], [12, 4] ])

w_row = np.array([[0.6, 0.4]])

print(X.shape) # (3, 2) print(w_row.shape) # (1, 2)

The weight 0.6 tells the model that study hours matter more than sleep hours. But the shapes (3, 2) and (1, 2) are not compatible for multiplication.

If you try X @ w_row directly, NumPy raises a shape error. The columns of X (2) do not match the rows of w_row (1).

Transposing the weight vector

Apply .T to turn the row vector into a column vector:

python w_col = w_row.T print(w_col) print(w_col.shape) # (2, 1)

Now the values 0.6 and 0.4 are stacked vertically, and the shape (2, 1) is compatible with (3, 2).

Running the matrix-vector product

Use the @ operator to perform the multiplication:

python predictions = X @ w_col print(predictions) print(predictions.shape) # (3, 1)

The result is a column vector with three predictions: 9.2, 5.8, and 8.8. Each value is the predicted performance score for one student, on a scale roughly from 1 to 10.

The output shape (3, 1) makes sense: rows of the matrix times columns of the vector.

What happens if you change the weights in the model?

Try swapping the weights to give more importance to sleep hours. Change w_row from [0.6, 0.4] to [0.3, 0.7] and run everything again.

The predictions shift because the model now values rest over study time. The student with the most sleep hours climbs in the ranking, while the one who only studied a lot may drop.

This simple change shows how weights shape behavior. In real models, training is exactly this: finding the combination of numbers that produces the best predictions across thousands of examples.

Which student got the highest score in your version? Share your results in the comments and tell me which weight combination you tried.