# My First Step into Machine Learning: Understanding Linear Regression
I've recently started exploring Machine Learning, and honestly, there are so many algorithms and concepts that I want to understand.
Instead of jumping directly into advanced algorithms, I decided to start with the basics.
My first topic is Linear Regression.
I had heard this name several times while studying Machine Learning, but I wanted to understand what actually happens behind the algorithm rather than just memorizing its definition.
So, here's what I've understood so far.
What exactly is Linear Regression?
Let's understand it with a simple example.
Imagine you want to buy a house. You visit different properties and notice something interesting: houses with larger areas generally cost more than smaller ones.
Now, imagine someone asks you:
"If a house has an area of 1,500 square feet, what would its approximate price be?"
You might look at the prices of other houses and try to estimate the answer.
This is basically the kind of problem Linear Regression helps us solve.
In simple words, Linear Regression is a supervised Machine Learning algorithm that predicts continuous numerical values by learning the relationship between input features and an output.
Some other examples include:
- Predicting house prices.
- Estimating monthly sales.
- Predicting employee salaries.
- Forecasting product demand.
Let's understand the mathematics behind it
When I first saw the equation, it looked quite familiar from school mathematics.
The basic equation is:
y = mx + c
Here:
-
yrepresents the predicted value. -
xrepresents the input. -
mrepresents the slope. -
crepresents the intercept.
The interesting part is that the algorithm learns the values of m and c from the training data.
For multiple input features, we use:
y = b0 + b1x1 + b2x2 + ... + bnxn
This allows the model to consider multiple factors while making predictions.
But how does the algorithm actually learn?
This was the part I wanted to understand properly.
Think of a graph containing several data points.
The algorithm tries to find a straight line that fits the data as closely as possible.
Initially, the predictions may not be accurate. The model calculates the errors between actual values and predicted values and tries to reduce them.
One commonly used error measurement is Mean Squared Error (MSE).
Its formula is:
MSE = (1/n) × Σ(y - ŷ)²
It calculates the average squared difference between actual and predicted values.
A smaller MSE generally means the predictions are closer to the actual values on the evaluated dataset.
This is also where concepts like Gradient Descent become important. I'll explore that in more detail in another article.
Time to try it with Python!
After understanding the basic idea, I wanted to see how Linear Regression works in practice.
For this example, I'm using a small sample dataset containing house areas and their prices.
First, let's import the libraries.
import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score from sklearn.model_selection import train_test_split Now, let's create our dataset.
X = np.array([ [500], [800], [1000], [1200], [1500], [1800], [2000], [2200] ]) y = np.array([ 25, 40, 50, 60, 75, 90, 100, 110 ]) Here, X contains the house areas, and y contains their prices in lakhs.
Next, we'll divide the data into training and testing sets.
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.25, random_state=42 ) Now comes the interesting part: training the model.
model = LinearRegression() model.fit(X_train, y_train) With just these two lines, Scikit-learn trains the Linear Regression model using our training data.
Let's make some predictions.
y_pred = model.predict(X_test) print("Predictions:", y_pred) Finally, let's evaluate the model.
mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print("Mean Squared Error:", mse) print("R2 Score:", r2) We can also visualize the regression line.
plt.scatter(X, y, label="Actual Data") plt.plot( X, model.predict(X), color="red", label="Regression Line" ) plt.xlabel("House Area (sq. ft.)") plt.ylabel("House Price (Lakhs)") plt.title("My First Linear Regression Model") plt.legend() plt.show() Seeing the data points and the regression line together makes the concept much easier to understand.
Note: This is a small, artificially created dataset for learning purposes, not a real-world house price dataset.
A few things I learned
While exploring this algorithm, I came across several concepts that are important in Machine Learning:
- Supervised Learning uses labelled data for training.
- Linear Regression predicts continuous numerical values.
- The model learns coefficients from the training data.
- MSE helps measure prediction errors.
- Training and testing data help us evaluate model performance.
- Visualization makes it easier to understand how a model behaves.
What's next?
This is just the beginning of my Machine Learning journey.
I want to understand each algorithm step by step, implement it using Python, experiment with datasets and gradually work on real-world projects.
My next topic will be Logistic Regression, where I'll explore how Machine Learning can be used for classification problems.
I'm sharing these notes not as an expert, but as someone who is learning, experimenting and improving every day.
If you're also learning Machine Learning, I'd love to know what you're currently exploring.
Let's learn and grow together! 🚀

