Linear Regression for Single Prediction
Last Updated :
23 Jul, 2025
Linear regression is a statistical method and machine learning foundation used to model relationship between a dependent variable and one or more independent variables. The primary goal is to predict the value of the dependent variable based on the values of the independent variables.
Predicting a Single Value Using Linear Regression
Once the model has been trained and evaluated we can use it to make predictions. When we talk about producing a single prediction value it means using a specific set of independent variable(s) to generate one dependent variable using linear regression model.
Example 1: Single Prediction Using Simple Linear Regression
Let’s assume you have a dataset that tracks hours studied (independent variable) and test scores (dependent variable). After training the model you want to predict the test score for a student who studied for 5 hours.
Using the equation of the regression line y = \beta_0 + \beta_1x, where: x is the number of hours studied.
We can substitute x=5x into the equation and compute y, which is the predicted test score.
Example 2: Single Prediction Using Multiple Linear Regression
In the case of multiple linear regression where more than one independent variable influences the dependent variable predicting a single value involves inputting multiple independent variable values in the model.
For example in predicting house prices factors such as area, number of bedrooms and location all be considered. Once you input the values for these independent variables the model will output predicted house price.
Building a Linear Regression Model
1. Loading and Preparing Data
First import necessary libraries and load the dataset. For this example we will generate a synthetic dataset with multiple features using numpy
. Assume we are working with a dataset where we want to predict a target based on two random features.
Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# Set random seed for reproducibility
np.random.seed(42)
# Generate random independent variables
n_samples = 1000
X1 = np.random.uniform(1, 100, n_samples) # Random numbers for feature 1
X2 = np.random.uniform(1, 50, n_samples) # Random numbers for feature 2
# Create a relationship between features and the target variable with some noise
noise = np.random.normal(0, 10, n_samples)
y = 2.5 * X1 + 3.8 * X2 + noise # Linear relationship with noise
# Create a DataFrame for easy manipulation
data = pd.DataFrame({'Feature1': X1, 'Feature2': X2, 'Target': y})
data.head()
Output:
Feature1 Feature2 Target
0 38.079472 10.071514 124.690605
1 95.120716 27.553146 334.234944
2 73.467400 43.774346 347.746226
3 60.267190 36.879019 294.481904
4 16.445845 40.521496 204.232146
This synthetic dataset has two features (Feature1
and Feature2
) and one target variable (Target
) with a linear relationship with some noise.
2. Exploratory Data Analysis (EDA)
Before moving to modeling let's analyze the data visually to ensure there is a linear relationship between the features and the target variable.
Python
# Pairplot to visualize relationships between variables
sns.pairplot(data)
plt.show()
# Check the correlation between features and target
corr_matrix = data.corr()
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.show()
Output:
Exploratory Data Analysis (EDA)3. Data Preprocessing
Linear regression is sensitive to feature scaling. To improve model performance, it’s important to scale the features. We'll use StandardScaler
from sklearn
.
Python
# Split data into independent variables (X) and dependent variable (y)
X = data[['Feature1', 'Feature2']]
y = data['Target']
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Scale the features using StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
4. Train the Linear Regression Model
Now that the data is scaled, we can train the linear regression model. This step provides the model's coefficients and the intercept.
Python
# Instantiate the linear regression model
model = LinearRegression()
# Train the model on the training data
model.fit(X_train_scaled, y_train)
# Check the model's coefficients and intercept
print("Coefficients: ", model.coef_)
print("Intercept: ", model.intercept_)
Output:
Coefficients: [72.55199542 54.26405783]
Intercept: 224.59535042865838
5. Make Single Predictions
We can now make predictions on the test data and evaluate the model. Below, new_data
represents a new data point with two feature values. The model will predict the target value based on this new input.
Python
# Predict the target values for the test set
y_pred = model.predict(X_test_scaled)
# Example of predicting a single value using a new data point
new_data = np.array([[45, 30]]) # Example values for Feature1 and Feature2
new_data_scaled = scaler.transform(new_data) # Scale the new data
single_prediction = model.predict(new_data_scaled)
print(f"Predicted value for the new data point {new_data[0]}: {single_prediction[0]}")
Output:
Predicted value for the new data point [45 30]: 226.37747796176552
6. Model Evaluation
To evaluate the model's performance we’ll compute the Mean Squared Error (MSE) and R-squared value for the test data.
Python
# Calculate Mean Squared Error (MSE) and R-squared (R^2)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r2}")
Output:
Mean Squared Error: 100.35222719050975
R-squared: 0.9876309086737035
Limitations of Linear Regression for Single Predictions
- Sensitivity to Outliers: Outliers have negative impact on linear regression models when making predictions. It can skew the best-fit line and result in inaccurate predictions.
- Overfitting: If the model is overly complex (for example, too many independent variable) it may fit the training data too closely leading to overfitting and perform poorly on new unseen data.
- Assumes a Linear Relationship: Linear regression assumes a linear relationship between the independent and dependent variables. However in real-world data is non-linear relationship.
Linear regression is a simple model that can be used to predict single values based on historical data. The process of building a linear regression model involves preparing data, training the model and making predictions based on specific inputs.
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.Do you
5 min read
Introduction to Machine Learning
Python for Machine Learning
Machine Learning with Python TutorialPython language is widely used in Machine Learning because it provides libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and Keras. These libraries offer tools and functions essential for data manipulation, analysis, and building machine learning models. It is well-known for its readability an
5 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Feature Engineering
Supervised Learning
Unsupervised Learning
Model Evaluation and Tuning
Advance Machine Learning Technique
Machine Learning Practice