learns machine studying often begins with linear regression, not simply because it’s easy, however as a result of it introduces us to the important thing ideas that we use in neural networks and deep studying.
We already know that linear regression is used to foretell steady values.
Now we now have this knowledge, we have to construct a easy linear regression mannequin to foretell worth of the home utilizing its dimension.
We typically use python to implement this algorithm.
Code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# 1. Information
X = np.array([1, 2, 3]).reshape(-1, 1)
y = np.array([11, 12, 19])
# 2. Prepare the scikit-learn mannequin
mannequin = LinearRegression()
mannequin.match(X, y)
# 3. Extract the parameters and predictions
intercept = mannequin.intercept_
slope = mannequin.coef_[0]
y_pred = mannequin.predict(X)
errors = y - y_pred
print("--- Scikit-Be taught Outcomes ---")
print(f"Intercept (Beta 0): {intercept:.0f}")
print(f"Slope (Beta 1): {slope:.0f}")
print(f"Predictions: {y_pred}")
print(f"Errors (Residuals): {errors}")
# 4. Create the 2D Scatterplot
plt.determine(figsize=(8, 6))
# Plot the precise knowledge factors
plt.scatter(X, y, shade='blue', s=100, label='Precise Information (y)')
# Plot the scikit-learn line of greatest match
plt.plot(X, y_pred, shade='crimson', linewidth=2, label='scikit-learn Finest Match Line')
# Draw the vertical residual strains (errors)
for i in vary(len(X)):
plt.plot([X[i][0], X[i][0]], [y[i], y_pred[i]], shade='inexperienced', linestyle='--', linewidth=2)
plt.textual content(X[i][0] + 0.05, (y[i] + y_pred[i])/2, f'e={errors[i]:.0f}', shade='inexperienced', fontsize=12)
plt.xlabel('Measurement (x, in 1000 sq ft)')
plt.ylabel('Worth (y, in $100k)')
plt.title('scikit-learn Easy Linear Regression')
plt.legend()
plt.grid(True, linestyle=':', alpha=0.7)
# Show the plot
plt.present()
Then we get these values:

Prepare!
This time we’re going to take a unique route to unravel this drawback.
Since we’re taking a much less explored route, let’s put together ourselves so we don’t get misplaced halfway by way of our journey of understanding.
The route we’re going to take is Vector Projection and for that permit’s recollect our fundamentals on vectors.
On this first half, we’re going to construct our geometric instinct round vectors, dot merchandise, and projections. These are absolutely the fundamentals we have to study to know linear regression clearly.
As soon as we now have that basis down, Half 2 will dive into the precise implementation.
Let’s go.
What’s a Vector?
Let’s return to highschool the place we first received launched to vectors.
One of many first examples we study vectors is Velocity vs. Velocity.
This instance tells us that fifty km/hour is the pace, and it’s a scalar amount as a result of it solely has magnitude, whereas 50 km/hour within the east route is a vector amount as a result of it has each magnitude and route.
Now, let’s draw it on a graph.

If we plot the coordinates (2, 4), we take into account it some extent in 2D house.
But when we join the origin to that time with an arrow, we now take into account it a vector as a result of it has each magnitude and route.
We will consider (2, 4) as a set of directions: it says to take 2 steps to the appropriate alongside the x-axis, after which 4 steps up parallel to the y-axis
The best way it factors provides us the route.
The size of an arrow provides us the magnitude of the vector.

[
text{From the plot we can observe the formation of a right-angled triangle.}
text{From the Pythagorean theorem we know that}
c = sqrt{a^2 + b^2}
text{For a vector } v = (x,y), text{ the magnitude is}
||v|| = sqrt{x^2 + y^2}
text{Substituting the values of the vector } (2,4)
||v|| = sqrt{2^2 + 4^2}
||v|| = sqrt{4 + 16}
||v|| = sqrt{20}
||v|| approx 4.47 text{ units}
]
Now let’s draw one other vector (6,2) in our graph.

By wanting on the vectors, we will see that they’re typically pointing up and to the appropriate.
They aren’t pointing in the very same route, however they’re clearly leaning the identical approach.
The angle between the vectors is small.
As an alternative of simply observing and stating this, we will measure how two vectors really agree with one another. For that, we use the dot product.
From the plot, we now have two vectors:
[
mathbf{A} = (2,4)
]
[
mathbf{B} = (6,2)
]
We already know that we will interpret these numbers as actions alongside the axes.
Vector (A):
[
A = (2,4)
]
means
[
2 text{ units in the } xtext{-direction}
]
[
4 text{ units in the } ytext{-direction}
]
Vector (B):
[
B = (6,2)
]
means
[
6 text{ units in the } xtext{-direction}
]
[
2 text{ units in the } ytext{-direction}
]
To measure how a lot the 2 vectors agree with one another alongside every axis, we multiply their corresponding parts.
Alongside the (x)-axis:
[
2 times 6
]
Alongside the (y)-axis:
[
4 times 2
]
Then we add these contributions collectively:
[
2 times 6 + 4 times 2
]
[
= 12 + 8
]
[
= 20
]
This operation known as the Dot Product.
Typically, for 2 vectors
[
mathbf{A} = (a_1,a_2)
]
[
mathbf{B} = (b_1,b_2)
]
the dot product is outlined as
[
mathbf{A} cdot mathbf{B} = a_1 b_1 + a_2 b_2
]
We received a dot product of 20, however what does that imply?
Since 20 is a optimistic quantity, we will observe that the angle between the vectors is lower than 90o.
We will additionally consider it as a optimistic relationship between the 2 variables represented by these vectors. This concept will change into clearer after we start discussing the Easy Linear Regression drawback.
You could have a doubt about how the dot product is expounded to the angle between two vectors and the way we will say that the angle is lower than 90o.
Earlier than understanding this relationship, we are going to have a look at two extra circumstances of the dot product in order that our understanding turns into clearer about what the dot product is definitely measuring. After that, we are going to transfer on to the angle between vectors.
Now let’s have a look at two extra examples to raised perceive the dot product.

After we have a look at vectors with a dot product equal to 0, we will say that they’re orthogonal, which means they’re perpendicular to one another. On this case, the vectors haven’t any linear relationship, which corresponds to zero correlation.
Now, after we observe vectors with a destructive dot product, we will see that the angle is obtuse, which implies the vectors are pointing in reverse instructions, and this represents a destructive correlation.
Now, as soon as once more, let’s take into account the 2 vectors [2,4] and [6,2].

We received the dot product of 20.
Now, there may be one other approach to discover the dot product, which includes the lengths of the vectors and the angle between them.
This reveals that 20 isn’t a random quantity; it signifies that the vectors are leaning in the identical route.
Let the 2 vectors be
[
A = (2,4), qquad B = (6,2)
]
First compute the lengths (magnitudes) of the vectors.
[
|A| = sqrt{2^2 + 4^2}
]
[
|A| = sqrt{4 + 16} = sqrt{20}
]
[
|B| = sqrt{6^2 + 2^2}
]
[
|B| = sqrt{36 + 4} = sqrt{40}
]
Now utilizing the dot product system
[
A cdot B = |A| |B| cos(theta)
]
From the element system of the dot product
[
A cdot B = 2 times 6 + 4 times 2
]
[
A cdot B = 12 + 8 = 20
]
Substitute into the angle system
[
20 = sqrt{20} times sqrt{40} times cos(theta)
]
[
cos(theta) = frac{20}{sqrt{20}sqrt{40}}
]
Now simplify the denominator
[
sqrt{20} = 2sqrt{5}, qquad sqrt{40} = 2sqrt{10}
]
[
sqrt{20}sqrt{40} = (2sqrt{5})(2sqrt{10})
]
[
= 4sqrt{50}
]
[
= 4(5sqrt{2})
]
[
= 20sqrt{2}
]
So,
[
cos(theta) = frac{20}{20sqrt{2}}
]
[
cos(theta) = frac{1}{sqrt{2}}
]
[
theta = 45^circ
]
We get this system through the use of the Legislation of Cosines. That is the geometric approach of fixing the dot product.
From this equation, we will perceive that if we now have the lengths of the vectors and the angle between them, we will simply discover the dot product of the 2 vectors.
Up up to now, we’ve gotten a fundamental thought of what vectors are, their dot merchandise, and the angles between them.
I do know all the things has been largely mathematical up up to now. Nonetheless, we are actually going to make use of what we’ve realized to debate projections, and issues will change into even clearer after we lastly remedy a easy linear regression drawback.
Vector Projections
Now think about we’re driving alongside a freeway by way of a forest, and on our approach to attain the home someplace deep contained in the forest, far-off from the freeway.
Let’s say our home is at a set level, (2,4). There’s a mud street by way of the forest that leads immediately there, however as a consequence of incessant rains, we can’t take that route.
Now we now have an alternative choice which is freeway by way of the forest, which runs within the route of (6,2).
Now what we now have to do is journey alongside the freeway, park the automotive alongside the freeway, after which take the baggage and stroll to your property.
We’re carrying heavy baggage, and we don’t need to stroll a lot. So, we have to cease your automotive on the level on the freeway the place the strolling distance to our house is the shortest.

The query now could be: How far do we have to journey alongside that freeway (the [6, 2] route) to get as shut as potential to our residence at (2, 4)?

Now, by wanting on the visible above, let’s see what we will observe.
If we cease our automotive at level A, it’s too early; we will see that the crimson line connecting to our house is an extended stroll.
Subsequent, if we cease the automotive at level C, we now have already gone previous our residence, so we have to flip again, and that is additionally an extended stroll.
We will observe that time B is the most effective spot to cease the automotive as a result of our stroll residence kinds an ideal 90o angle with the freeway.
We have to discover the precise level on the freeway to park our automotive.
Let’s begin from the origin and first discover the direct distance to our residence, which is positioned at (2, 4).
In linear algebra, this distance is solely the size of the vector. Right here, the size is , which is 4.47. We will say that our house is 4.47 kilometers from the origin within the route of (2, 4).
However we can’t take that direct route due to the rain; it’s a muddy, unpaved street. We solely have one possibility: drive alongside the freeway within the route of (6, 2).

We’ve a freeway pointing within the route of (6, 2), which we name a vector.
On this freeway, we will solely transfer ahead or backward and that is the one dimension we now have.
Each level we will presumably attain makes up the span of the vector.
It is very important perceive that the freeway is an infinite street. It doesn’t really begin at (0, 0); we’re simply beginning our journey from that particular level in the course of it.
To reduce our stroll by way of the mud, we have to discover the spot on the freeway closest to our residence, which is at all times a perpendicular path.
To know our driving distance alongside the freeway, let’s take into account a milestone signpost on our freeway at (6, 2) which we use as a reference for route on the freeway.
If we calculate the bodily distance from our start line (0, 0) to this signpost, the size is , which is 6.32. So, our reference signpost is precisely 6.32 km down the street.
There are a number of methods to seek out our actual parking level. First, if we have a look at any two recognized factors on the freeway like our begin at (0, 0) and our signpost at (6, 2), we will calculate the slope of the street:
$$
m = frac{y_2 – y_1}{x_2 – x_1}
$$
$$
m = frac{2 – 0}{6 – 0}
$$
$$
m = frac{2}{6} = frac{1}{3}
$$
A slope of 1/3 implies that for each 3 items of improve in x, there may be 1 unit improve in y. As a result of each level on the freeway vector follows this actual rule, we will write the equation for our street as:
$$
y = frac{1}{3}x
$$
This implies each level on the freeway together with the parking level have the coordinates .
We simply want to seek out the proper x that minimizes the stroll between our automotive at () and our residence at (2, 4).
To keep away from coping with sq. roots in our calculus and to make the calculation simpler, let’s reduce the squared distance.
We need to reduce the squared distance, f(x), between our automotive at () and our residence at (2, 4). The squared distance system is:
The squared distance system:
$$
f(x) = (x_2 – x_1)^2 + (y_2 – y_1)^2
$$
$$
f(x) = (x – 2)^2 + left(frac{1}{3}x – 4right)^2
$$
Increasing the binomials:
$$
f(x) = (x^2 – 4x + 4) + left(frac{1}{9}x^2 – frac{8}{3}x + 16right)
$$
Grouping the phrases collectively:
$$
f(x) = left(1 + frac{1}{9}proper)x^2 – left(4 + frac{8}{3}proper)x + (4 + 16)
$$
$$
f(x) = frac{10}{9}x^2 – left(frac{12}{3} + frac{8}{3}proper)x + 20
$$
$$
f(x) = frac{10}{9}x^2 – frac{20}{3}x + 20
$$
If we graph the error operate f(x), it kinds a U-shaped parabola.
To search out the minimal level, we take the spinoff and set it to zero.
$$
f'(x) = frac{d}{dx}left(frac{10}{9}x^2 – frac{20}{3}x + 20right)
$$
$$
f'(x) = frac{20}{9}x – frac{20}{3}
$$
Setting the spinoff equal to zero:
$$
frac{20}{9}x – frac{20}{3} = 0
$$
$$
frac{20}{9}x = frac{20}{3}
$$
$$
x = frac{20}{3} occasions frac{9}{20}
$$
$$
x = 3
$$
Plug x=3 again into the freeway equation:
$$
y = frac{1}{3}x
$$
$$
y = frac{1}{3}(3) = 1
$$
The proper parking spot is (3,1).
Through the use of the calculus technique, we discovered the parking spot at (3, 1). If we evaluate this to our milestone signpost at (6, 2), we will observe that the parking level is precisely half the gap to the signpost.
Which means if we drive midway to the signpost, we attain the precise level the place we will park and take the shortest path to residence.
[
begin{gathered}
text{Our Parking Spot: } mathbf{P} = (3, 1)
text{Our Signpost: } mathbf{V} = (6, 2)
text{Relationship:}
(3, 1) = 0.5 times (6, 2)
text{Therefore, our optimal multiplier } c text{ is } 0.5.
end{gathered}
]
This 0.5 is precisely what we discover in linear regression. We are going to get a good clearer thought of this after we apply these ideas to unravel a real-world regression drawback.
From the plot, we will say that the vector from the origin (0,0) to the parking level (3,1) is the projection of the house vector onto the freeway, whose size is
$$
textual content{Driving Distance} = sqrt{x^2 + y^2}
$$
$$
= sqrt{3^2 + 1^2}
$$
$$
= sqrt{9 + 1}
$$
$$
= sqrt{10}
$$
$$
approx 3.16 textual content{ km}
$$
That is how we calculate the Vector Projections.
Now, we even have a shortcut to seek out the parking level.
Earlier, we calculated the dot product of those two vectors, which is 20.
Now, let’s multiply the size of the projection vector by the size of the freeway vector (from the origin to the signpost).
3.16 occasions 6.32, which additionally equals 20. From this, we will perceive that the dot product provides us the size of the projection multiplied by the size of the freeway.
We’ve a dot product of 20, and the squared size of the freeway is 40. We’re utilizing the squared size as a result of the dot product itself has squared items; after we multiply a1b1 and a2b2 and add them, the items additionally get multiplied.
Now, if we divide the dot product by the squared size (20 / 40), we get 0.5. We name this the scaling issue.
As a result of we need to discover the precise level alongside the freeway, we scale the freeway vector by 0.5, which provides us (3, 1).
In vector vocabulary, we name the freeway the bottom vector and the house vector the goal vector.
And that’s how we get our parking level at (3, 1).
What we mentioned thus far may be expressed utilizing a easy mathematical system known as the projection system.
$$
textual content{proj}_{mathbf{b}}(mathbf{a}) =
frac{mathbf{a}cdotmathbf{b}}{|mathbf{b}|^2}mathbf{b}
$$
Let
$$
A = (2,4), quad B = (6,2)
$$
First compute the dot product.
$$
Acdot B = 2times6 + 4times2
$$
$$
Acdot B = 12 + 8
$$
$$
Acdot B = 20
$$
Now compute the squared size of the freeway vector.
$$
|B|^2 = 6^2 + 2^2
$$
$$
|B|^2 = 36 + 4
$$
$$
|B|^2 = 40
$$
Now divide the dot product by the squared size.
$$
frac{Acdot B}B = frac{20}{40}
$$
$$
= 0.5
$$
This worth is the scaling issue.
Now scale the freeway vector.
$$
textual content{proj}_{B}(A) = 0.5(6,2)
$$
$$
= (3,1)
$$
So the projection level (the parking level) is
$$
(3,1)
$$
Now, what can we are saying about this 3.16 km distance alongside the freeway?
Let’s say, for instance, we take the direct mud route and ignore the freeway. As we transfer alongside our path to residence, we are literally touring in two instructions concurrently: parallel to the freeway and sideways towards our residence.
By the point we lastly attain our residence, we now have successfully traveled 3.16 km within the route of the freeway.
Alternatively, what if we journey alongside the freeway? If we drive precisely 3.16 km alongside the freeway, then we attain our parking level at (3, 1).
This particular level is the place the trail to our house is completely perpendicular to the freeway.
Most significantly, this implies it represents absolutely the shortest strolling path from the freeway to our residence!
I hope you’re strolling away with intuitive understanding of vectors, dot merchandise, and projections!
In Half 2, we are going to take precisely what we realized at present and use it to unravel an actual linear regression drawback.
If something on this submit felt unclear, be happy to remark.
In the meantime, I just lately wrote a deep dive on the Chi-Sq. take a look at. If you’re , you may learn it here.
Thanks a lot for studying!

