DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Using A/B Testing To Make Data-Driven Product Decisions
  • Black Box Tester in Python
  • Exploring Mobile Device Lab: Pros and Cons
  • How Artificial Intelligence (AI) Is Transforming the Mortgage Industry

Trending

  • Essential Monitoring Tools, Troubleshooting Techniques, and Best Practices for Atlassian Tools Administrators
  • Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality
  • Explore the Complete Guide to Various Internet of Things (IoT) Protocols
  • Empowering Citizen Developers With Low- and No-Code Tools: Changing Developer Workflows and Empowering Non-Technical Employees to Build Apps
  1. DZone
  2. Coding
  3. Languages
  4. A Guide to Regression Analysis Forecasting in Python

A Guide to Regression Analysis Forecasting in Python

Python's statsmodels and sklearn libraries are widely used to develop the forecasting models based on Regression Analysis.

By 
Kapil Kumar Sharma user avatar
Kapil Kumar Sharma
·
May. 29, 24 · Tutorial
Like (3)
Save
Tweet
Share
365 Views

Join the DZone community and get the full member experience.

Join For Free

Regression analysis is a technic of estimating the value of a dependent variable based on the values of independent values. These models largely explore the possibilities of studying the impact of independent variables on dependent variables. 

In this article, we will focus on estimating the value of revenue (dependent variable) based on the historical demand (independent values) coming from various demand channels like Call, Chat, and Web inquiries. I will use Python libraries like statsmodels and sklearn to develop an algorithm to forecast revenue.

Once we can predict the revenue, this empowers the businesses to strategies the investment, and prioritize the customers and demand channels with an aim to grow the revenue.

Data Ingestion and Exploration

In this section, I will detail the test data, data ingestion using pandas, and data exploration to get familiar with the data. I am using a summarized demand gen dataset, containing the following data attributes.

data attributes

The following code block would ingest the input file “DemandGenRevenue.csv” and list the sample records.

Python
 
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
import statsmodels.formula.api as sm
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
import warnings

warnings.simplefilter(action='ignore', category=FutureWarning)

df = pd.read_csv("DemandGenRevenue.csv")
df.head()


revenue

Python
 
df.columns
df.info()
df.describe().T



class
table

The following code can be used to design Scatter plots to explore the linearity assumption between the independent variables — Call, Chat, Web Inquiry, and the dependent variable — Revenue.

Python
 
sns.pairplot(df, x_vars=["call", "chat", "Web_Inquiry"], y_vars="Revenue", kind="reg")


scatter plot

Let's explore the normality assumption of the dependent variable — Revenue using Histograms.

Python
 
df.hist(bins=20)


Let's explore the normality assumption of the dependent variable — Revenue using Histograms.

Before we start working on the model, let's explore the relationship between each independent variable and the dependent variable using Linear regression plots.

Python
 
sns.lmplot(x='call', y='Revenue', data=df)
sns.lmplot(x='chat', y='Revenue', data=df)
sns.lmplot(x='Web_Inquiry', y='Revenue', data=df)



Linear regression plots

chat and revenue

web inquiry

Forecasting Model

In this section, I will delve into the model preparation using statsmodels and sklearn libraries. We will build a linear regression model based on the demand coming from calls, chats, and web inquiries. 

Python
 
X = df.drop('Revenue', axis=1)
y = df[["Revenue"]]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=46)


The following code will build a Linear regression model to forecast the revenue.

Python
 
lin_model = sm.ols(formula="Revenue ~ call + chat + Web_Inquiry", data=df).fit()
print(lin_model.params, "\n")



Linear Regressional Model

Use the below code to explore the coefficients of the linear model.

Python
 
print(lin_model.summary())



regression results

The code below can be used to define various models and loop through the models to forecast, for simplicity's sake, we will only focus on the Linear Regression Model.

Python
 
results = []
names = []
models = [('LinearRegression', LinearRegression())]

for name, model in models:
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    result = np.sqrt(mean_squared_error(y_test, y_pred))
    results.append(result)
    names.append(name)
    msg = "%s: %f" % (name, result)
    print(msg)


Now that we have the model ready, let's try and forecast the revenue based on the input. If we were to get 1000 calls, 650 chats, and 725 Web Inquiries, based on the historical data we can expect $64.5M of revenue.

Python
 
new_data = pd.DataFrame({'call': [1000], 'chat': [650], 'Web_Inquiry': [725]})
Forecasted_Revenue = lin_model.predict(new_data)
print("Forecasted Revenue:", int(Forecasted_Revenue))



forecasted revenue

The code below provides another set of inputs to test the model, if the demand center receives 2000 calls, 1200 chats, and 250 web inquiries, the model forecasts the revenue at $111.5M of revenue.

Python
 
new_data = pd.DataFrame({'call': [2000], 'chat': [1200], 'Web_Inquiry': [250]})
Forecasted_Revenue = lin_model.predict(new_data)
print("Forecasted Revenue:", int(Forecasted_Revenue))



new data

Conclusion

In the end, Python offers multiple libraries to implement the forecasting, statsmodels and sklearn lays a solid foundation for implementing a linear regression model to predict the outcomes based on historical data. I would suggest continued Python exploration for working on enterprise-wide sales and marketing data to analyze historical trends and execute models to predict future sales and revenue. Darts is another Python library I would recommend implementing time series-based anomaly detection and user-friendly forecasting based on models like ARIMA to deep neural networks.

Linear regression Test data Python (language) Testing

Opinions expressed by DZone contributors are their own.

Related

  • Using A/B Testing To Make Data-Driven Product Decisions
  • Black Box Tester in Python
  • Exploring Mobile Device Lab: Pros and Cons
  • How Artificial Intelligence (AI) Is Transforming the Mortgage Industry

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: