ML Project – Sales Forecasting for a Retail Store using Multiple Variable for Linear Regression
Machine Learning courses with 100+ Real-time projects Start Now!!
Program 1
# Project Sales Forecasting for a Retail Store
# To predict future sales for a retail store based on historical sales data
# using a Linear Regression model.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import seaborn as sns
# Load dataset
df = pd.read_csv("retail_sales.csv")
# Display basic info
#print(df.head())
#print("\nCorrelation Matrix:\n", df.corr())
# Visualize
# sns.pairplot(df)
# plt.show()
# Independed and Depended variables
X = df[['Advertising_Budget', 'Customers_Visited']] # Independed variable
y = df['Sales'] # Depended variables
# x_train---> Training data set for Independed variable
# y_train---> Training data set for depended variable
# x_test---> Testing data set for Independed variable
# y_test---> Testing data set for depended variable
# Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,)
# print(len(X_train))
# print(len(y_train))
# Linear Regression Model
model = LinearRegression()
model.fit(X_train, y_train)
#print(model)
# Predictions
y_pred = model.predict(X_test)
#print(y_pred)
# Evaluation
#print(model.score(X_test,y_test))
print("\nModel Coefficients:", model.coef_)
print("Model Intercept:", model.intercept_)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R² Score:", r2_score(y_test, y_pred))
#Plot Actual vs Predicted
plt.scatter(y_test, y_pred, color='blue')
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red', linewidth=2)
plt.xlabel('Actual Sales')
plt.ylabel('Predicted Sales')
plt.title('Actual vs Predicted Sales')
plt.grid(True)
plt.show()
Did you like this article? If Yes, please give DataFlair 5 Stars on Google

