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.
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.
Our Example
We will build a model that predicts an exam score based on the number of hours a student studies.
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.
We are telling Python that we want to use the LinearRegression model provided by scikit-learn.
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:
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.
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.
This distinction is important. Creating the model and training the model are two different steps.
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.
Study hours
Exam scores
The model learns the coefficients needed to represent the relationship in the training data.
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.
For this simple dataset, the model learns a relationship equivalent to:
Therefore, 6 hours gives a prediction of approximately 95.
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)
Understand the Code as a Workflow
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.
For Linear Regression, these learned parameters include the coefficients and intercept used to make predictions.
What Does predict() Do?
model.predict([[6]])
The predict() method takes new input and uses the trained model to calculate the estimated output.
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:
Think of it as:
[
[6]
]
The outer list represents the collection of samples. The inner list contains the features for one sample.
Understand the Four Important Lines
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.