0% found this document useful (0 votes)
1 views2 pages

EXP4DV

The document outlines a process for predicting sales trends using linear regression in Python. It includes data acquisition, analysis through visualization, model training, and evaluation, followed by future sales predictions from 2022 to 2030. The results are displayed in a DataFrame and visualized with a plot showing actual and predicted sales trends.

Uploaded by

Amaan Amaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

EXP4DV

The document outlines a process for predicting sales trends using linear regression in Python. It includes data acquisition, analysis through visualization, model training, and evaluation, followed by future sales predictions from 2022 to 2030. The results are displayed in a DataFrame and visualized with a plot showing actual and predicted sales trends.

Uploaded by

Amaan Amaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error

# Step 1: Acquire Data (Simulated or real-world data can be used)


data = {
"Year": np.arange(2000, 2021),
"Sales": [100, 120, 130, 145, 160, 175, 190, 210, 230, 250, 270, 290, 310, 330,
350, 375, 400, 420, 440, 460, 480]
}
df = pd.DataFrame(data)

# Step 2: Analyze Patterns


plt.scatter(df["Year"], df["Sales"], color='blue', label='Actual Data')
plt.xlabel("Year")
plt.ylabel("Sales")
plt.title("Sales Trend Over Years")
plt.legend()
plt.show()

# Step 3: Predict Future Trends


X = df[["Year"]] # Feature
y = df["Sales"] # Target

# Splitting Data 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)

# Train Model
model = LinearRegression()
model.fit(X_train, y_train)

# Predict Sales
predictions = model.predict(X_test)

# Evaluate Model
mae = mean_absolute_error(y_test, predictions)
mse = mean_squared_error(y_test, predictions)
print(f"Mean Absolute Error: {mae}")
print(f"Mean Squared Error: {mse}")

# Predict Future Trends (e.g., for 2022-2030)


future_years = np.arange(2022, 2031).reshape(-1, 1)
future_sales = model.predict(future_years)

# Visualizing Predictions
plt.scatter(df["Year"], df["Sales"], color='blue', label='Actual Data')
plt.plot(future_years, future_sales, color='red', linestyle='dashed',
label='Predicted Trend')
plt.xlabel("Year")
plt.ylabel("Sales")
plt.title("Future Sales Prediction")
plt.legend()
plt.show()

# Display Future Predictions


df_future = pd.DataFrame({"Year": future_years.flatten(), "Predicted Sales":
future_sales})
print(df_future)

You might also like