Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import pandas as pd

import matplotlib.pyplot as plt


from sklearn.linear_model import LinearRegression

# Sales data
sales_data = {'Month': list(range(1, 37)),
'Sales': [242, 235, 232, 178, 184, 140, 145, 152, 110, 130, 152,
206, 263, 238, 247, 193, 193, 149, 157, 161, 122, 130,
167, 230, 282, 255, 265, 205, 210, 160, 166, 174, 126,
148, 173, 235]}

sales_df = pd.DataFrame(sales_data)

# Create dummy variables for months


sales_df = pd.get_dummies(sales_df, columns=['Month'])

# Add a trend variable


#sales_df['Trend'] = range(1, len(sales_df) + 1)

# Prepare the independent variables (X) and the dependent variable (y)
X = sales_df.drop('Sales', axis=1)
y = sales_df['Sales']
print(sales_df)
# Fit the regression model
model = LinearRegression().fit(X, y)

# Forecasting for the next 12 months (4th year)


future_months = pd.DataFrame({'Month_2': [0] * 12, 'Month_3': [0] * 12,
'Month_4': [0] * 12, 'Month_5': [0] * 12,
'Month_6': [0] * 12, 'Month_7': [0] * 12,
'Month_8': [0] * 12, 'Month_9': [0] * 12,
'Month_10': [0] * 12, 'Month_11': [0] * 12,
'Month_12': [0] * 12})

future_forecast = model.predict(future_months)

# Display the forecast for the next 12 months


forecast_df = pd.DataFrame({'Month': range(37, 49), 'Forecasted_Sales':
future_forecast})
print(forecast_df)

# Plotting the original and forecasted time series


plt.figure(figsize=(10, 6))
plt.plot(sales_df['Month_1'], sales_df['Sales'], marker='o', linestyle='-',
label='Actual Sales')
plt.plot(forecast_df['Month'], forecast_df['Forecasted_Sales'], marker='o',
linestyle='-', label='Forecasted Sales')
plt.title('Sales Time Series with Forecast')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.show()

You might also like