Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Time Forecasting using Python/4.2 Predict Profit using Linear Regression .ipynb
3074 views
Kernel: Python 3 (ipykernel)
import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression Y = mx+ C M= slope C= intercept m=9 c=.9 x=800

Profit=C1(Date)+C2(Sales)+C3(Qty)+Intercep+Error

# Display the generated data profit_data = pd.read_csv("profit_data.csv") profit_data.head()
profit_data.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 60 entries, 0 to 59 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Date 60 non-null object 1 Profit 60 non-null float64 dtypes: float64(1), object(1) memory usage: 1.1+ KB
profit_data['Date']
0 2015-01-31 1 2015-02-28 2 2015-03-31 3 2015-04-30 4 2015-05-31 5 2015-06-30 6 2015-07-31 7 2015-08-31 8 2015-09-30 9 2015-10-31 10 2015-11-30 11 2015-12-31 12 2016-01-31 13 2016-02-29 14 2016-03-31 15 2016-04-30 16 2016-05-31 17 2016-06-30 18 2016-07-31 19 2016-08-31 20 2016-09-30 21 2016-10-31 22 2016-11-30 23 2016-12-31 24 2017-01-31 25 2017-02-28 26 2017-03-31 27 2017-04-30 28 2017-05-31 29 2017-06-30 30 2017-07-31 31 2017-08-31 32 2017-09-30 33 2017-10-31 34 2017-11-30 35 2017-12-31 36 2018-01-31 37 2018-02-28 38 2018-03-31 39 2018-04-30 40 2018-05-31 41 2018-06-30 42 2018-07-31 43 2018-08-31 44 2018-09-30 45 2018-10-31 46 2018-11-30 47 2018-12-31 48 2019-01-31 49 2019-02-28 50 2019-03-31 51 2019-04-30 52 2019-05-31 53 2019-06-30 54 2019-07-31 55 2019-08-31 56 2019-09-30 57 2019-10-31 58 2019-11-30 59 2019-12-31 Name: Date, dtype: object
profit_data['Date']=pd.to_datetime(profit_data['Date'])
# Convert dates to ordinal integers profit_data['Date_Ordinal'] = profit_data['Date'].apply(lambda x: x.toordinal())
# Split the data into features (X) and target variable (y) X = profit_data['Date_Ordinal'].values.reshape(-1, 1) # Convert date to ordinal integer for linear regression y = profit_data['Profit']
# Train the linear regression model Forecast_Profit = LinearRegression() Forecast_Profit.fit(X, y)
# Generate dates for the next 5 years (2023-2027) future_dates = pd.date_range(start='2020-01-09', end='2023-12-31', freq='Y') future_dates_ordinal = future_dates.to_series().apply(lambda x: x.toordinal()).values.reshape(-1, 1) # Predict profit for the next 5 years future_profit = Forecast_Profit.predict(future_dates_ordinal) # Create a DataFrame for future profit predictions future_data = pd.DataFrame({'Date': future_dates, 'Predicted Profit': future_profit}) # Display the future profit predictions print(future_data)
Date Predicted Profit 0 2020-12-31 8340.280127 1 2021-12-31 7816.856193 2 2022-12-31 7293.432260 3 2023-12-31 6770.008326
import matplotlib.pyplot as plt # Plot the predicted profits plt.figure(figsize=(11, 14)) plt.plot(profit_data['Date'], profit_data['Profit'], label='Historical Profit') plt.plot(future_data['Date'], future_data['Predicted Profit'], linestyle='--', color='red', label='Predicted Profit') plt.title('Predicted Profit for the Next 5 Years') plt.xlabel('Year') plt.ylabel('Profit') plt.legend() plt.grid(True) plt.show()
Image in a Jupyter notebook
Forecast_Profit.intercept_
1066359.30492475
Forecast_Profit.coef_
array([-1.43403817])
Y=MX+C+eRROR Y=MX1+MX2+ERROR+C

fORECAST_pRPFIT = M(DATES)+ INTERCEPT+e1(MSE/rmse)

Forecasted Profit = -DATE(.32)+25K