Understanding Linear Regression: A Beginner's Guide to Machine Learning
# 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 fi

# 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.
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.
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.
"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.
When I first saw the equation, it looked quite familiar from school mathematics.
The interesting part is that the algorithm learns the values of m and c from the training data.
This allows the model to consider multiple factors while making predictions.
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).
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.
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.
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 Enter fullscreen mode Exit fullscreen mode 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 ] ) Enter fullscreen mode Exit fullscreen mode 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 ) Enter fullscreen mode Exit fullscreen mode Now comes the interesting part: training the model.
model = LinearRegression ( ) model. fit ( X_train, y_train ) Enter fullscreen mode Exit fullscreen mode With just these two lines, Scikit-learn trains the Linear Regression model using our training data.
y_pred = model. predict ( X_test ) print ( "Predictions:", y_pred ) Enter fullscreen mode Exit fullscreen mode 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 ) Enter fullscreen mode Exit fullscreen mode 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 ( ) Enter fullscreen mode Exit fullscreen mode 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.
While exploring this algorithm, I came across several concepts that are important in Machine Learning:
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.