Master Linear Regression in Machine Learning using Python 3

Linear Regression in machine learning | Innovate Yourself


Welcome to the world of machine learning! If you’re a Python enthusiast with dreams of becoming a pro in this language, you’ve come to the right place. In this blog post, we’ll embark on an exciting journey through the fundamentals of machine learning, with a specific focus on linear regression in Python 3.

Linear regression is a foundational concept in machine learning, and mastering it will set you on the path to becoming a Python pro. We’ll walk you through every step, from understanding the theory behind linear regression to writing and executing Python code that brings the concept to life.

So, let’s dive right in and unravel the magic of linear regression in Python 3.

What is Linear Regression?

At its core, linear regression is a statistical method used to model the relationship between a dependent variable (often called the target) and one or more independent variables (features). The goal is to find a linear equation that best describes this relationship, allowing us to make predictions based on new data.

Linear Regression in machine learning | Innovate Yourself

When to Use Linear Regression?

Linear regression is particularly useful in scenarios where you want to:

  • Predict numerical values (e.g., predicting house prices based on square footage).
  • Understand the strength and nature of relationships between variables.
  • Identify and quantify the impact of different factors on an outcome.

Gradient Descent

Gradient descent is a fundamental optimization algorithm used in various machine learning and statistical modeling techniques, including linear regression. It’s employed to find the optimal values for the model’s parameters (coefficients) that minimize the error or loss function, making the model a better fit for the data.

Here’s a step-by-step explanation of how gradient descent works in the context of linear regression:

  1. Linear Regression Basics: In linear regression, the goal is to find a linear relationship between the independent variables (features) and the dependent variable (target). This relationship is represented as: y = β0 + β1 * x1 + β2 * x2 + ... + βn * xn Where:
    • y is the predicted value.
    • β0, β1, β2, ... βn are the coefficients (parameters) to be optimized.
    • x1, x2, ... xn are the feature values.
  2. Defining the Error (Cost) Function: To measure how well the model’s predictions match the actual target values, a cost function (also known as a loss function) is used. The most common cost function for linear regression is the Mean Squared Error (MSE). It’s defined as: MSE = (1/n) * Σ(yi - ŷi)^2 Where:
    • n is the number of data points.
    • yi is the actual target value.
    • ŷi is the predicted target value.
    The objective is to minimize this cost function.
  3. Initializing Coefficients: Gradient descent starts with initial values for the coefficients (β0, β1, β2, ... βn), often set to 0 or small random values.
  4. Iterative Optimization: Gradient descent is an iterative optimization process. In each iteration, the algorithm updates the coefficients to minimize the cost function. The update is based on the gradient (slope) of the cost function with respect to each coefficient.
  • Calculate the gradient of the cost function with respect to each coefficient. This gradient tells us how much the cost function would change if we make small adjustments to the coefficients.
  • Update each coefficient by subtracting a fraction of the gradient from its current value. The fraction is determined by the learning rate, a hyperparameter that controls the step size of each update.
  • Repeat this process for a fixed number of iterations (epochs) or until a convergence criterion is met, such as a small change in the cost function.

5. Learning Rate: The learning rate is a critical hyperparameter in gradient descent. It controls the step size for each coefficient update. A high learning rate may cause the algorithm to overshoot the minimum, while a low learning rate may make the convergence too slow. Fine-tuning the learning rate is often necessary for successful optimization.

6. Convergence: Gradient descent continues iterating until the cost function converges to a minimum or reaches a predefined number of iterations. Convergence is typically checked by monitoring the change in the cost function between iterations.

7. Final Coefficients: Once the gradient descent algorithm converges, the final values of the coefficients represent the best-fit linear relationship between the features and the target variable.

8. Using the Model: With the optimized coefficients, you can use the linear regression model to make predictions for new data by plugging in the feature values into the equation.

Gradient descent is a powerful optimization technique that enables linear regression models to find the optimal coefficients that minimize prediction errors and provide accurate results. It’s a fundamental concept not only in linear regression but also in many other machine learning algorithms.

Setting Up Your Python Environment

Before we start coding, let’s ensure you have the necessary tools and libraries installed. Don’t worry; it’s a straightforward process.

Step 1: Installing Python 3

If you haven’t already installed Python 3, visit the official Python website ( to download and install the latest version compatible with your operating system.

Step 2: Installing Python Libraries

Python’s strength in machine learning comes from its libraries. The three essential libraries we’ll use are NumPy, pandas, and scikit-learn. Open your terminal or command prompt and run the following command to install them:

pip install numpy pandas scikit-learn

With your environment set up, we’re ready to explore linear regression through Python.

Understanding Linear Regression

The Basics: Simple Linear Regression

Let’s start with the most fundamental form of linear regression: simple linear regression. In this case, we have one dependent variable (target) and one independent variable (feature).

Imagine we want to predict a student’s final exam score based on the number of hours they’ve studied. Here’s how we can implement simple linear regression in Python:

import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# Sample data
hours_studied = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10])
exam_scores = np.array([50, 55, 60, 65, 70, 75, 80, 85, 90])

# Reshape the data (required for single feature)
X = hours_studied.reshape(-1, 1)

# Create and train the linear regression model
model = LinearRegression(), exam_scores)

# Make predictions
predicted_scores = model.predict(X)

# Visualize the results
plt.scatter(hours_studied, exam_scores, label='Actual Scores')
plt.plot(hours_studied, predicted_scores, color='red', label='Predicted Scores')
plt.xlabel('Hours Studied')
plt.ylabel('Exam Scores')
Linear Regression in machine learning | Innovate Yourself

In this example, we import the necessary libraries, prepare our data, create a linear regression model, make predictions, and visualize the results. It’s a simple yet powerful demonstration of linear regression in action.

Going Beyond: Multiple Linear Regression

While simple linear regression deals with one independent variable, multiple linear regression extends the concept to multiple independent variables. This enables us to consider more complex relationships in our predictions.

Let’s say we want to predict house prices based on not only square footage but also the number of bedrooms and the neighborhood’s crime rate. Here’s how you can implement multiple linear regression:

import numpy as np
from sklearn.linear_model import LinearRegression

# Sample data
square_footage = np.array([1500, 2000, 1200, 1800, 2100])
bedrooms = np.array([3, 4, 2, 3, 4])
crime_rate = np.array([0.05, 0.02, 0.07, 0.03, 0.01])
house_prices = np.array([300000, 400000, 220000, 350000, 420000])

# Create a feature matrix with multiple variables
X = np.column_stack((square_footage, bedrooms, crime_rate))

# Create and train the multiple linear regression model
model = LinearRegression(), house_prices)

# Make a prediction for a new house
new_house = np.array([1900, 3, 0.04]).reshape(1, -1)
predicted_price = model.predict(new_house)

print("Predicted Price:", predicted_price[0])
Predicted Price: 335000.00000000163

In this example, we use three independent variables to predict house prices. The code demonstrates how to create a feature matrix, train a multiple linear regression model, and make predictions for new data.

Evaluating Your Model

Building a model is just the beginning. To ensure your model is accurate and reliable, you need to evaluate its performance. Here are some common evaluation metrics:

Mean Absolute Error (MAE)

MAE measures the average absolute errors between predicted and actual values. Lower MAE values indicate a better model.

from sklearn.metrics import mean_absolute_error

mae = mean_absolute_error(exam_scores, predicted_scores)
print("Mean Absolute Error:", mae)

Mean Squared Error (MSE) and Root Mean Squared Error (RMSE)

MSE measures the average squared errors between predicted and actual values. RMSE is the square root of MSE.

from sklearn.metrics import mean_squared_error
import math

mse = mean_squared_error(exam_scores, predicted_scores)
rmse = math.sqrt(mse)

print("Mean Squared Error:", mse)
print("Root Mean Squared Error:", rmse)

R-squared (R²) Score

R-squared measures how well the model explains the variance in the data. A higher R² score indicates a better model fit.

from sklearn.metrics import r2_score

r2 = r2_score(exam_scores, predicted_scores)
print("R-squared Score:", r2)

By understanding these evaluation metrics, you can assess the performance of your linear regression model effectively.


Congratulations! You’ve embarked on a journey into the world of machine learning by mastering linear regression in Python 3. We’ve covered the basics, from setting up your environment to understanding the theory and writing Python code.

But remember, the world of machine learning is vast, and there’s always more to explore. As you continue your Python journey, don’t hesitate to delve into more advanced topics like logistic regression, decision trees, and deep learning. Each step you take brings you closer to becoming a Python pro and a machine learning expert.

So, keep coding, experimenting, and enjoying the process. Python is a versatile and powerful language, and machine learning is just

one of the countless adventures you can embark on. With dedication and practice, you’ll unlock endless possibilities in the world of Python and machine learning.

Also, check out our other playlist Rasa ChatbotInternet of thingsDockerPython ProgrammingMQTTTech NewsESP-IDF etc.
Become a member of our social family on youtube here.
Stay tuned and Happy Learning. ✌🏻😃
Happy coding, and welcome to the exciting realm of machine learning! ❤️🔥

Leave a Reply