MACHINE LEARNING • LESSON 7

Build Linear Regression With Python

In this lesson, we will build a simple Linear Regression model using Python and see how training and prediction work in real code.

THE SIMPLEST DEFINITION

Give Python training data → train the model → make a prediction.

We will use scikit-learn because it provides a ready-to-use Linear Regression implementation.

01

Our Example

We will build a model that predicts an exam score based on the number of hours a student studies.

Study Hours Exam Score
1 45
2 55
3 65
4 75
5 85
INPUT Study Hours
MODEL Linear Regression
OUTPUT Exam Score
02

Import Linear Regression

First, we import the Linear Regression model from scikit-learn.

from sklearn.linear_model import LinearRegression

We can now create a Linear Regression model in Python.

What does this line mean?

We are telling Python that we want to use the LinearRegression model provided by scikit-learn.

03

Create the Training Data

Next, we create our input data and the corresponding output values.

X = [[1], [2], [3], [4], [5]]

y = [45, 55, 65, 75, 85]

Here:

X Input feature — study hours
y Target — exam score
X = input features, y = target/output.

Notice that X is written as a list of lists:

[[1], [2], [3], [4], [5]]

Each inner list represents one training example and its feature.

04

Create the Model

Now we create a Linear Regression model object.

model = LinearRegression()

At this point, the model has been created, but it has not learned anything yet.

CREATED model
Not trained yet

This distinction is important. Creating the model and training the model are two different steps.

05

Train the Model

Now we give the training data to the model.

model.fit(X, y)

The fit() method is where the model learns the relationship between X and y.

INPUT X

Study hours

+
TARGET y

Exam scores

TRAINING model.fit(X, y)

The model learns the coefficients needed to represent the relationship in the training data.

06

Make a Prediction

Our model is now trained. Suppose a new student studied for 6 hours.

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

print(prediction)

We give 6 to the trained model.

NEW DATA [[6]]
TRAINED MODEL model.predict()
PREDICTION 95

For this simple dataset, the model learns a relationship equivalent to:

Score ≈ 35 + 10 × Study Hours

Therefore, 6 hours gives a prediction of approximately 95.

07

The Complete Python Code

Now put all the steps together:

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)
This is the basic Linear Regression workflow in Python.
08

Understand the Code as a Workflow

Import LinearRegression
Create X and y
Create the model
model.fit(X, y)
model.predict(new_data)
Predicted value
09

What Does fit() Actually Do?

This is one of the most important things to understand.

model.fit(X, y)

The model looks at the training examples and learns the relationship between the input and target.

BEFORE FIT Model has not learned from this data
AFTER FIT Model has learned parameters from the data

For Linear Regression, these learned parameters include the coefficients and intercept used to make predictions.

10

What Does predict() Do?

model.predict([[6]])

The predict() method takes new input and uses the trained model to calculate the estimated output.

NEW INPUT 6 study hours
predict() Uses learned parameters
OUTPUT Predicted score
11

Why Is X Written as [[6]]?

Beginners often get confused by this:

model.predict([[6]])

The reason is that scikit-learn expects input features in a two-dimensional structure:

ONE SAMPLE [6]
DATASET WITH ONE SAMPLE [[6]]

Think of it as:

[
    [6]
]

The outer list represents the collection of samples. The inner list contains the features for one sample.

[[6]] = one sample with one feature.
QUICK CHECK

Understand the Four Important Lines

LinearRegression() Creates the model.
model.fit(X, y) Trains the model using the data.
model.predict(...) Makes predictions using the trained model.
print(prediction) Displays the prediction.
REMEMBER THIS

fit() learns. predict() predicts.

In scikit-learn, you create the Linear Regression model, train it with fit(X, y), and then use predict() with new input to get a numerical prediction.

NEXT TOPIC

Understand the Python Code

Next, we will go through the Python code line by line so you understand exactly what each part is doing.