Creating a simple machine learning model
Last Updated :
15 Apr, 2025
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:
Y= w_1X_1 + w_2X_2 + w_nX_n + b
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:
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:
Similar Reads
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.It can
5 min read
Prerequisites for Machine Learning
Python for Machine Learning Welcome to "Python for Machine Learning," a comprehensive guide to mastering one of the most powerful tools in the data science toolkit. Python is widely recognized for its simplicity, versatility, and extensive ecosystem of libraries, making it the go-to programming language for machine learning. I
6 min read
SQL for Machine Learning Integrating SQL with machine learning can provide a powerful framework for managing and analyzing data, especially in scenarios where large datasets are involved. By combining the structured querying capabilities of SQL with the analytical and predictive capabilities of machine learning algorithms,
6 min read
Getting Started with Machine Learning
Advantages and Disadvantages of Machine Learning Machine learning (ML) has revolutionized industries, reshaped decision-making processes, and transformed how we interact with technology. As a subset of artificial intelligence ML enables systems to learn from data, identify patterns, and make decisions with minimal human intervention. While its pot
3 min read
Why ML is Important ? Machine learning (ML) has become a cornerstone of modern technology, revolutionizing industries and reshaping the way we interact with the world. As a subset of artificial intelligence (AI), ML enables systems to learn and improve from experience without being explicitly programmed. Its importance s
4 min read
Real- Life Examples of Machine Learning Machine learning plays an important role in real life, as it provides us with countless possibilities and solutions to problems. It is used in various fields, such as health care, financial services, regulation, and more. Importance of Machine Learning in Real-Life ScenariosThe importance of machine
13 min read
What is the Role of Machine Learning in Data Science In today's world, the collaboration between machine learning and data science plays an important role in maximizing the potential of large datasets. Despite the complexity, these concepts are integral in unraveling insights from vast data pools. Let's delve into the role of machine learning in data
9 min read
Top Machine Learning Careers/Jobs Machine Learning (ML) is one of the fastest-growing fields in technology, driving innovations across healthcare, finance, e-commerce, and more. As companies increasingly adopt AI-based solutions, the demand for skilled ML professionals is Soaring. Machine Learning JobsThis article delves into the Ty
10 min read