Open In App

Creating a simple machine learning model

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Machine Learning models are the core of smart applications. To get a better insight into how machine learning models work, let us discuss one of the most basic algorithms: Linear Regression. This is employed in predicting a dependent variable based on one or multiple independent variables by utilizing a linear relationship.

In this article, we will go through step by step procedure to create and test a Linear Regression model in Python with synthetically generated data.

What is Linear Regression?

In machine learning, linear regression is a supervised learning algorithm for predicting a continuous outcome variable based on one or more predictor variables. To understand the working of linear regression mathematically, you might take a look at Linear Regression.

The equation of a linear model is:

[Tex]Y= w_1X_1 + w_2X_2 + w_nX_n + b[/Tex]

Where:

  • w1, w2 are the coefficients (weights),
  • b is the bias or intercept term.

we’ll generate data such that the output is given by:

Y = a+ 2b+ 3c

Step 1: Generating the Training Dataset

We will generate 100 random data samples, each with 3 features a, b, and c.

Python
# python library to generate random numbers
from random import randint

# limit under which the samples are to be generated
TRAIN_SET_LIMIT = 1000

# count of samples
TRAIN_SET_COUNT = 100

# Lists to store input and corresponding outputs
TRAIN_INPUT = []
TRAIN_OUTPUT = []

# loop to create 100 data  items with three columns each
for _ in range(TRAIN_SET_COUNT):
    a = randint(0, TRAIN_SET_LIMIT)
    b = randint(0, TRAIN_SET_LIMIT)
    c = randint(0, TRAIN_SET_LIMIT)

    # Output based on the formula: a + 2b + 3c
    output = a + (2 * b) + (3 * c)

    TRAIN_INPUT.append([a, b, c])
    
    # adding each output to output list
    TRAIN_OUTPUT.append(output)

Step 2: Training the Linear Regression Model

We’ll use the LinearRegression class from scikit-learn to train our model on the generated dataset.

Python
from sklearn.linear_model import LinearRegression

# Initialize the Linear Regression model
model = LinearRegression(n_jobs=-1)

# Train the model
model.fit(X=TRAIN_INPUT, y=TRAIN_OUTPUT)

Output:

linear_model

Step 3: Testing the Model

We will test the trained model with a known input and compare the predictions with the expected output.

Python
X_TEST = [[10, 20, 30]] #Test data

predicted_output = model.predict(X=X_TEST)

# Getting the coefficients
coefficients = model.coef_

print(f"Outcome : {predicted_output}")
print(f"Coefficients : {coefficients}")

Output:

Outcome : [140.]
Coefficients : [1. 2. 3.]

Now to check if the output is correct

Y= 10 + (2∗20) + (3∗30) = 10 +40 +90 = 140

Hence, the model perfectly learned the underlying relationship in the training dataset with the help of linear regression.

Related Article:


Next Article

Similar Reads