Understand the Python Code
We already built a Linear Regression model. Now we will understand the important Python code line by line and see exactly what each part does.
Every line of the program has a specific job.
We will follow the code from importing the model, creating the data, training the model, and finally making a prediction.
The Complete Code
First, look at the complete program before we break it into individual parts.
from sklearn.linear_model import LinearRegression
# Training data
X = [[1], [2], [3], [4], [5]]
y = [45, 55, 65, 75, 85]
# Create the model
model = LinearRegression()
# Train the model
model.fit(X, y)
# Make a prediction
prediction = model.predict([[6]])
print(prediction)
Import Linear Regression
from sklearn.linear_model import LinearRegression
This line imports the LinearRegression class from scikit-learn.
Scikit-learn is a popular Python library for machine learning. It provides many ready-to-use machine learning algorithms.
Create the Input Data
X = [[1], [2], [3], [4], [5]]
X contains the input feature used by the model.
In our example, the input is:
So the values mean:
Why Is X Written as [[1], [2], [3]]?
This is important for beginners.
Scikit-learn expects X to contain a collection of samples, where each sample contains its features.
X = [
[1],
[2],
[3]
]
This means:
Each inner list represents one sample.
Create the Target Values
y = [45, 55, 65, 75, 85]
y contains the output values that belong to the training examples.
In our example, y represents the exam scores.
Create the Model
model = LinearRegression()
This line creates a Linear Regression model object.
But there is an important point:
But it has not learned from our training data yet.
It has learned parameters from the training data.
Creating a model is not the same thing as training it.
Train the Model
model.fit(X, y)
This is where the model learns from the training data.
Study hours
Exam scores
Learn the relationship
The model looks at the relationship between study hours and exam scores and learns the parameters needed for Linear Regression.
Make a Prediction
prediction = model.predict([[6]])
Now we give the trained model a new input.
The new student studied for 6 hours.
For this simple dataset, the prediction is approximately 95.
Store the Prediction
prediction = model.predict([[6]])
The result returned by predict() is stored in the variable called prediction.
Think of it like:
The variable now contains the model's predicted value.
Print the Prediction
print(prediction)
The print() function displays the value stored in the prediction variable.
You may see output similar to:
[95.]
Scikit-learn commonly returns predictions as a NumPy array, even when there is only one prediction.
Understand the Entire Program
from sklearn.linear_model import LinearRegression
X = [[1], [2], [3], [4], [5]]
y = [45, 55, 65, 75, 85]
model = LinearRegression()
model.fit(X, y)
prediction = model.predict([[6]])
print(prediction)
The Two Most Important Methods
Learns from the training data.
Uses the learned model to make predictions.
Check Your Understanding
X → input, y → target, fit() → learn, predict() → predict.
Once you understand these four ideas, the basic Linear Regression code becomes much easier to read. The model is created first, trained using known data, and then used with new data to make predictions.