MACHINE LEARNING • LESSON 7

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.

THE SIMPLEST DEFINITION

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.

01

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)
Now we will understand this code one piece at a time.
02

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.

sklearn The scikit-learn library.
linear_model The part of scikit-learn containing linear models.
LinearRegression The Linear Regression model we want to use.
Think of import as: "Bring this machine learning tool into my Python program."
03

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:

Number of hours the student studied.

So the values mean:

X Value Meaning
[1] Student studied 1 hour
[2] Student studied 2 hours
[3] Student studied 3 hours
[4] Student studied 4 hours
[5] Student studied 5 hours
04

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:

SAMPLE 1 [1]
SAMPLE 2 [2]
SAMPLE 3 [3]

Each inner list represents one sample.

[[6]] means one sample with one feature.
05

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.

Study Hours Exam Score
1 45
2 55
3 65
4 75
5 85
X = input/features, y = target/output.
06

Create the Model

model = LinearRegression()

This line creates a Linear Regression model object.

But there is an important point:

AFTER THIS LINE Model exists

But it has not learned from our training data yet.

AFTER fit() Model has learned

It has learned parameters from the training data.

Creating a model is not the same thing as training it.

07

Train the Model

model.fit(X, y)

This is where the model learns from the training data.

INPUT X

Study hours

+
TARGET y

Exam scores

FIT model.fit(X, y)

Learn the relationship

The model looks at the relationship between study hours and exam scores and learns the parameters needed for Linear Regression.

08

Make a Prediction

prediction = model.predict([[6]])

Now we give the trained model a new input.

The new student studied for 6 hours.

NEW INPUT [[6]]
TRAINED MODEL predict()
OUTPUT Prediction

For this simple dataset, the prediction is approximately 95.

09

Store the Prediction

prediction = model.predict([[6]])

The result returned by predict() is stored in the variable called prediction.

Think of it like:

MODEL model.predict([[6]])
STORED RESULT prediction

The variable now contains the model's predicted value.

10

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.

[95.] means the predicted value is 95.
11

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)
Import model
Create X
Create y
Create model
Train with fit()
Predict with predict()
Print result
12

The Two Most Important Methods

fit() model.fit(X, y)

Learns from the training data.

predict() model.predict(new_data)

Uses the learned model to make predictions.

Remember: fit() = learn, predict() = predict.
QUICK CHECK

Check Your Understanding

What is X? The input feature used by the model.
What is y? The target/output values used during training.
What does fit() do? It trains the model using X and y.
What does predict() do? It uses the trained model to produce predictions.
REMEMBER THIS

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.

NEXT TOPIC

Evaluate a Regression Model

We can now build and use a Linear Regression model. The next question is more important: how do we know whether its predictions are good or bad?