The first step into the world of machine learning and statistics usually begins with Linear Models. This approach, which has been the most fundamental building block of data science for decades, allows us to express seemingly complex relationships in data with a simple and linear equation. At its core, what it does is very clear: It tries to predict the output value given the input vector :
is the bias (intercept) value. If we write this formula with as a vector:
Understanding Dot Product
Let’s break down the formula step by step to better understand it:
Here, each is a single feature value and is a scalar:
Similarly, each coefficient is also a scalar:
Therefore, the product of two real numbers yields a single real number:
For example:
then:
is obtained.
This 12 is now a scalar; it is not a vector or matrix, but just a single number.
The same applies to all terms:
each produces scalar values individually.
As a result, their sum is also a single number:
Therefore, the output of a linear model is a single scalar prediction. Each product is also a scalar, and this is called the dot product (inner product).
Including the Bias
Instead of writing the bias separately, we can extend the feature vector:
The reason we start with 1 is to be able to include the value in the formula as well.
Why is Transpose Needed?
We cannot directly multiply two column vectors.
In dot product multiplication, the inner dimensions must be the same. Therefore, we need to write one of them as a row.
So now:
Example Solution
Let there be two features:
Then:
Result:
Hyperplane
The geometric meaning of a linear model is a hyperplane. Let’s think with two features () and one output (). The predictions produced by our model () form a flat surface, i.e., a plane (or a hyperplane in higher dimensions), extending along the and axes.
In the visualization below; the blue plane represents the hyperplane created by our linear model in space, the points represent actual data points, and the red dashed lines represent the distances of the actual data to our model (plane):
Each point is orthogonally projected onto the hyperplane. The difference between the actual value () and the model’s prediction on the plane () is called the residual: .
Note: How this plane is placed in space and how these residual errors are minimized is the subject of optimization methods. The linear model is simply the mathematical definition of this plane.
Coefficients and the Gradient Vector
When we think of the linear model’s equation as a function, , what determines the model’s slope in space is the coefficients. Mathematically, the gradient of a function indicates the direction of steepest ascent at that point.
For a linear model, this gradient is directly equal to the coefficient vector:
So the vector indicates the direction in which the model’s output () increases fastest in the input space.
In the visualization above, the green arrow represents the (i.e., ) vector, and the orange dot represents the current position . Since the slope of the plane is constant in a linear model, this direction is the same everywhere. The algorithms we use when training models (e.g., Gradient Descent) use these vectorial properties to bring the model to the most accurate position.