Commit c0523bbe authored by Ravindu Nethsara's avatar Ravindu Nethsara

Merge branch 'Demand_Forecast_Model_Implemented' into 'master'

New Files Added For demand forecasting

See merge request !28
parents fe790d07 05d4870e
Month,CALTEX_Delo_Gold_Ultra,Price1 Month,CALTEX_Delo_Gold_Ultra,Price1,Substitude_Product_Price1,Production_cost_per_unit1,VIC_Hino_Fuel_Filter,Price2,Substitude_Product_Price2,Production_cost_per_unit2,CALTEX_Texamatic_1888_Transmision_Oil,Price3,Substitude_Product_Price3,Production_cost_per_unit3
2012-01,380,4100 2012-01,380,4100,4150,3500,520,860,910,410,280,780,850,550
2012-02,240,4100 2012-02,240,4100,4150,3500,460,860,910,410,210,780,850,550
2012-03,324,4100 2012-03,324,4100,4150,3500,472,860,910,410,224,780,850,550
2012-04,381,4300 2012-04,381,4300,4320,3800,580,880,910,410,335,785,850,550
2012-05,246,4300 2012-05,246,4300,4320,3800,430,880,910,410,244,785,850,550
2012-06,336,4350 2012-06,336,4350,4320,3800,438,880,910,410,260,785,850,550
2012-07,282,4350 2012-07,282,4350,4320,3800,460,880,910,410,212,785,850,550
2012-08,212,4350 2012-08,212,4350,4320,3800,497,880,910,410,315,785,850,550
2012-09,222,4350 2012-09,222,4350,4320,3800,430,880,910,410,282,785,850,550
2012-10,301,4350 2012-10,301,4350,4320,3800,432,880,910,410,291,785,850,550
2012-11,264,4350 2012-11,264,4350,4320,3800,430,880,910,410,264,825,910,640
2012-12,388,4350 2012-12,388,4350,4320,3800,555,840,910,410,395,825,910,640
2013-01,354,4350 2013-01,354,4350,4320,3800,510,840,910,410,280,825,910,640
2013-02,247,4450 2013-02,247,4450,4555,3950,445,840,910,410,240,825,910,640
2013-03,221,4450 2013-03,221,4450,4550,3950,480,840,910,410,284,825,910,640
2013-04,363,4450 2013-04,363,4450,4550,3950,610,840,910,410,315,825,910,640
2013-05,240,4450 2013-05,240,4450,4550,3950,410,910,940,540,266,825,910,640
2013-06,312,4450 2013-06,312,4450,4550,3950,450,910,940,540,230,825,910,640
2013-07,233,4450 2013-07,233,4450,4550,3950,465,910,940,540,242,940,1150,700
2013-08,254,4450 2013-08,254,4450,4550,3950,488,910,940,540,310,940,1150,700
2013-09,198,4600 2013-09,198,4600,4670,4120,400,910,940,540,322,940,1150,700
2013-10,246,4600 2013-10,246,4600,4670,4120,450,925,990,540,291,940,1150,700
2013-11,310,4600 2013-11,310,4600,4670,4120,438,925,990,540,264,940,1150,700
2013-12,421,4600 2013-12,421,4600,4670,4120,620,900,990,540,395,940,1150,700
2014-01,313,4600 2014-01,313,4600,4670,4120,560,925,990,540,280,940,1150,700
2014-02,306,4600 2014-02,306,4600,4670,4120,480,925,990,540,240,940,1150,700
2014-03,247,4600 2014-03,247,4600,4670,4120,510,925,990,540,284,940,1150,700
2014-04,353,4650 2014-04,353,4650,4710,4200,645,925,990,540,315,980,1300,715
2014-05,237,4650 2014-05,237,4650,4710,4200,470,925,990,540,266,980,1300,715
2014-06,386,4650 2014-06,386,4650,4710,4200,440,925,990,540,230,980,1300,715
2014-07,260,4650 2014-07,260,4650,4710,4200,460,925,990,540,242,980,1300,715
2014-08,173,4650 2014-08,173,4650,4710,4200,530,960,1100,620,310,980,1300,715
2014-09,328,4650 2014-09,328,4650,4710,4200,440,960,1100,620,322,980,1300,715
2014-10,211,4650 2014-10,211,4650,4710,4200,465,960,1100,620,291,980,1300,715
2014-11,294,4650 2014-11,294,4650,4710,4200,530,960,1100,620,264,980,1300,715
2014-12,444,4400 2014-12,444,4400,4450,4200,660,950,1100,620,395,980,1300,715
2015-01,388,4725 2015-01,388,4725,4810,4350,580,960,1100,620,280,980,1300,715
2015-02,375,4725 2015-02,375,4725,4810,4350,510,960,1100,620,240,980,1300,715
2015-03,218,4725 2015-03,218,4725,4810,4350,515,960,1100,620,284,1250,1550,850
2015-04,414,4630 2015-04,414,4630,4600,4350,630,1100,1110,620,315,1250,1550,850
2015-05,252,4725 2015-05,252,4725,4750,4350,485,1100,1110,620,266,1250,1550,850
2015-06,369,4725 2015-06,369,4725,4750,4350,518,1100,1110,620,230,1250,1550,850
2015-07,313,4725 2015-07,313,4725,4750,4350,490,1100,1110,620,242,1250,1550,850
2015-08,143,4725 2015-08,143,4725,4750,4350,540,1100,1110,620,310,1250,1550,850
2015-09,349,4725 2015-09,349,4725,4750,4350,465,1100,1110,620,322,1250,1550,850
2015-10,328,4725 2015-10,328,4725,4750,4350,490,1100,1110,620,291,1250,1550,850
2015-11,314,4725 2015-11,314,4725,4750,4350,470,1100,1110,620,264,1360,1800,980
2015-12,451,4700 2015-12,451,4700,4710,4200,700,1025,1110,620,395,1360,1800,980
2016-01,422,4700 2016-01,422,4700,4710,4200,580,1100,1150,710,280,1360,1800,980
2016-02,397,4700 2016-02,397,4700,4710,4200,510,1100,1150,710,240,1360,1800,980
2016-03,354,4700 2016-03,354,4700,4710,4200,515,1100,1150,710,284,1360,1800,980
2016-04,421,4700 2016-04,421,4700,4710,4200,630,1360,1380,850,315,1360,1800,980
2016-05,347,4725 2016-05,347,4725,4760,4250,485,1360,1380,850,266,1360,1800,980
2016-06,453,4725 2016-06,453,4725,4760,4250,518,1360,1380,850,230,1360,1800,980
2016-07,365,4725 2016-07,365,4725,4760,4250,490,1360,1380,850,242,1360,1800,980
2016-08,264,4725 2016-08,264,4725,4760,4250,540,1360,1380,850,310,1360,1800,980
2016-09,302,4725 2016-09,302,4725,4760,4250,465,1360,1380,850,322,1360,1800,980
2016-10,324,4725 2016-10,324,4725,4760,4250,490,1360,1380,850,291,1410,1800,1200
2016-11,358,4725 2016-11,358,4725,4760,4250,470,1360,1380,850,264,1410,1870,1200
2016-12,512,4725 2016-12,512,4725,4760,4250,700,1320,1380,850,395,1410,1870,1200
2017-01,416,4725 2017-01,416,4725,4760,4250,580,1360,1380,850,280,1410,1870,1200
2017-02,397,4725 2017-02,397,4725,4760,4250,510,1380,1380,850,240,1410,1870,1200
2017-03,410,4725 2017-03,410,4725,4760,4250,515,1380,1380,850,284,1410,1870,1200
2017-04,476,4725 2017-04,476,4725,4760,4250,630,1380,1380,850,315,1410,1870,1200
2017-05,268,4725 2017-05,268,4725,4760,4250,485,1380,1380,850,266,1410,1870,1200
2017-06,477,4725 2017-06,477,4725,4760,4250,518,1380,1380,850,230,1410,1870,1200
2017-07,125,4725 2017-07,125,4725,4760,4250,490,1380,1380,850,242,1410,1870,1200
2017-08,421,4725 2017-08,421,4725,4760,4250,540,1410,1500,980,310,1410,1870,1200
2017-09,322,4725 2017-09,322,4725,4760,4250,465,1410,1500,980,322,1480,2100,1200
2017-10,373,4725 2017-10,373,4725,4760,4250,490,1410,1500,980,291,1480,2100,1200
2017-11,303,4725 2017-11,303,4725,4760,4250,470,1410,1500,980,264,1480,2100,1200
2017-12,567,4725 2017-12,567,4725,4760,4250,700,1400,1500,980,395,1480,2100,1200
2018-01,239,4725 2018-01,239,4725,4760,4250,580,1410,1500,980,280,1480,2100,1200
2018-02,299,4725 2018-02,299,4725,4760,4250,510,1410,1500,980,240,1480,2100,1200
2018-03,340,4725 2018-03,340,4725,4760,4250,515,1410,1500,980,284,1480,2100,1200
2018-04,370,4725 2018-04,370,4725,4760,4320,630,1550,1500,1010,315,1480,2100,1200
2018-05,227,4725 2018-05,227,4725,4760,4320,485,1550,1500,1010,266,1480,2100,1200
2018-06,386,4725 2018-06,386,4725,4760,4320,518,1550,1500,1010,230,1520,2100,1310
2018-07,317,4725 2018-07,317,4725,4760,4320,490,1550,1500,1010,242,1520,2100,1310
2018-08,438,4725 2018-08,438,4725,4760,4320,540,1550,1610,1010,310,1520,2250,1310
2018-09,321,4725 2018-09,321,4725,4760,4320,465,1550,1610,1010,322,1520,2250,1310
2018-10,394,4725 2018-10,394,4725,4760,4320,490,1550,1610,1010,291,1520,2250,1310
2018-11,281,5050 2018-11,281,5050,5110,4350,470,1550,1610,1010,264,1520,2250,1310
2018-12,436,4900 2018-12,436,4900,5000,4350,700,1500,1610,1010,395,1520,2250,1310
2019-01,334,4900 2019-01,334,4900,5000,4350,580,1550,1610,1010,280,1520,2250,1310
2019-02,262,4900 2019-02,262,4900,5000,4350,510,1610,1610,1110,240,1875,2400,1425
2019-03,286,4900 2019-03,286,4900,5000,4350,515,1610,1610,1110,284,1875,2400,1425
2019-04,476,4900 2019-04,476,4900,5000,4350,630,1610,1610,1110,315,1875,2400,1425
2019-05,410,5130 2019-05,410,5130,5190,4480,485,1610,1610,1110,266,1875,2400,1425
2019-06,474,5130 2019-06,474,5130,5190,4480,518,1610,1610,1110,230,1875,2400,1425
2019-07,343,5130 2019-07,343,5130,5190,4480,490,1610,1610,1110,242,1875,2400,1425
2019-08,359,5130 2019-08,359,5130,5190,4480,540,1610,1610,1110,310,1875,2400,1425
2019-09,251,5130 2019-09,251,5130,5190,4480,465,1610,1610,1110,322,1910,2550,1510
2019-10,281,5130 2019-10,281,5130,5190,4480,490,1610,1610,1110,291,1910,2550,1510
2019-11,357,5130 2019-11,357,5130,5190,4480,470,1610,1610,1110,264,1910,2550,1510
2019-12,570,5130 2019-12,570,5130,5190,4480,700,1600,1610,1110,395,1910,2550,1510
2020-01,348,5130 2020-01,348,5130,5190,4480,580,1610,1610,1110,280,1910,2550,1510
2020-02,364,5130 2020-02,364,5130,5190,4480,510,1700,1850,1250,240,1910,2550,1510
2020-03,477,5130 2020-03,477,5130,5190,4480,515,1700,1850,1250,284,1910,2550,1510
2020-04,488,5130 2020-04,488,5130,5190,4480,630,1700,1850,1250,315,1910,2550,1510
2020-05,418,5130 2020-05,418,5130,5190,4480,485,1700,1850,1250,266,1910,2550,1510
2020-06,512,6100 2020-06,512,6100,6150,5600,518,1700,1850,1250,230,1910,2550,1510
2020-07,429,6100 2020-07,429,6100,6150,5600,490,1700,1850,1250,242,1910,2550,1510
2020-08,413,6100 2020-08,413,6100,6150,5600,540,1700,1850,1250,310,1910,2550,1510
2020-09,377,6100 2020-09,377,6100,6150,5600,465,1700,1850,1250,322,2100,2550,1700
2020-10,421,6100 2020-10,421,6100,6150,5600,490,1850,1850,1300,291,2100,2550,1700
2020-11,302,6100 2020-11,302,6100,6150,5600,470,1850,1850,1300,264,2100,2800,1700
2020-12,488,5090 2020-12,488,5090,4950,5600,700,1850,1850,1300,395,2100,2800,1700
2021-01,424,5090 2021-01,424,5090,4950,5600,580,1850,2100,1300,280,2100,2800,1700
2021-02,312,6250 2021-02,312,6250,6350,5600,510,1850,2100,1300,240,2100,2800,1700
2021-03,383,6250 2021-03,383,6250,6350,5600,515,1850,2100,1300,284,2100,2800,1700
2021-04,521,6250 2021-04,521,6250,6350,5600,630,1850,2100,1300,315,2100,2800,1700
2021-05,288,6800 2021-05,288,6800,6950,5950,485,2100,2100,1540,266,2300,2800,1950
2021-06,341,6800 2021-06,341,6800,6950,5950,518,2100,2100,1540,230,2300,2800,1950
2021-07,324,7400 2021-07,324,7400,7500,6250,490,2100,2100,1540,242,2300,2800,1950
2021-08,291,7400 2021-08,291,7400,7500,6250,540,2100,2100,1540,310,2300,2800,1950
2021-09,330,7400 2021-09,330,7400,7500,6250,465,2100,2100,1540,322,2300,2800,1950
2021-10,382,7400 2021-10,382,7400,7500,6250,490,2100,2100,1540,291,2300,2800,1950
2021-11,421,7400 2021-11,421,7400,7500,6250,470,2100,2100,1540,264,2300,2800,1950
2021-12,516,7200 2021-12,516,7200,7500,6250,700,2170,2100,1540,395,2200,2800,1950
...@@ -15,7 +15,9 @@ MODE_CHOICES = [ ...@@ -15,7 +15,9 @@ MODE_CHOICES = [
MODE_DEMAND = [ MODE_DEMAND = [
('', 'Select Product'), ('', 'Select Product'),
('1', 'CALTEX Delo Gold Ultra') ('1', 'CALTEX_Delo_Gold_Ultra'),
('2', 'VIC_Hino_Fuel_Filter'),
('3', 'CALTEX_Texamatic_1888_Transmision_Oil')
] ]
...@@ -74,4 +76,5 @@ class MyFormServices(forms.Form): ...@@ -74,4 +76,5 @@ class MyFormServices(forms.Form):
Vnumber = forms.CharField(label='Enter Vehicle Number ') Vnumber = forms.CharField(label='Enter Vehicle Number ')
ServiceType = forms.IntegerField(label="Select Service", widget=forms.Select(choices=MODE_SERVICES)) ServiceType = forms.IntegerField(label="Select Service", widget=forms.Select(choices=MODE_SERVICES))
class MyFormNew(forms.Form):
inputdate = forms.DateField(widget=DateInput)
...@@ -15,7 +15,9 @@ urlpatterns = [ ...@@ -15,7 +15,9 @@ urlpatterns = [
path('Employee_Efficiency/', responseapp_views.Employee_Efficiency), path('Employee_Efficiency/', responseapp_views.Employee_Efficiency),
path('PurchaseHistory/', responseapp_views.PurchaseHistory), path('PurchaseHistory/', responseapp_views.PurchaseHistory),
path('Forecasting/', responseapp_views.Forecasting), path('Forecasting/', responseapp_views.Forecasting),
path('HighestDemand/', responseapp_views.HighestDemand),
path('Result/', responseapp_views.Forecasting), path('Result/', responseapp_views.Forecasting),
path('Result2/', responseapp_views.HighestDemand),
path('Customer_Data/', responseapp_views.Customer_Data), path('Customer_Data/', responseapp_views.Customer_Data),
path('Customer_Frequency/', responseapp_views.Customer_Frequency), path('Customer_Frequency/', responseapp_views.Customer_Frequency),
path('MaintainData/', responseapp_views.MaintainData), path('MaintainData/', responseapp_views.MaintainData),
......
...@@ -4,7 +4,7 @@ from django.template import loader ...@@ -4,7 +4,7 @@ from django.template import loader
from django.http import HttpResponse from django.http import HttpResponse
from sklearn.tree import DecisionTreeClassifier from sklearn.tree import DecisionTreeClassifier
from User.forms import MyForm,MyForm2,MyForm3,MyForm4,MyForm5,MyFormServices from User.forms import MyForm,MyForm2,MyForm3,MyForm4,MyForm5,MyFormServices,MyFormNew
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
from sklearn.linear_model import LogisticRegression from sklearn.linear_model import LogisticRegression
from User import models as Umodels from User import models as Umodels
...@@ -166,44 +166,61 @@ def PurchaseHistory(request): ...@@ -166,44 +166,61 @@ def PurchaseHistory(request):
# start Demand Forecasting Machine Learning Model # start Demand Forecasting Machine Learning Model
def Forecasting(request): def Forecasting(request):
if request.method == 'POST': if request.method == 'POST':
myForm = MyForm4(request.POST) myForm = MyForm4(request.POST)
if myForm.is_valid(): if myForm.is_valid():
Productid = myForm.cleaned_data['Productid'] Productid = myForm.cleaned_data['Productid']
inputdate = myForm.cleaned_data['inputdate']
ProductName = "CALTEX Delo Gold Ultra" if (Productid == 1): # load first product budget
if (ProductName == "CALTEX Delo Gold Ultra"): # load first product budget
df = pd.read_csv('Data/Purchasefile.csv') df = pd.read_csv('Data/Purchasefile.csv')
df = df.drop(
['VIC_Hino_Fuel_Filter', 'Price2', 'Substitude_Product_Price2', 'Production_cost_per_unit2',
'CALTEX_Texamatic_1888_Transmision_Oil', 'Price3', 'Substitude_Product_Price3',
'Production_cost_per_unit3'], axis=1)
df.head() df.head()
## Cleaning up the data
df.columns = ["Month", "CALTEX Delo Gold Ultra", "Price1"] df.columns = ["Month", "CALTEX_Delo_Gold_Ultra", "Price1", "Substitude_Product_Price1",
"Production_cost_per_unit1"]
rrr = df.head() rrr = df.head()
## Drop last 2 rows
# df.drop(106, axis=0, inplace=True)
# df.tail()
# df.drop(105, axis=0, inplace=True)
# df.tail()
# Convert Month into Datetime # Convert Month into Datetime
df['Month'] = pd.to_datetime(df['Month']) df['Month'] = pd.to_datetime(df['Month'])
# df.head()
df.set_index('Month', inplace=True) df.set_index('Month', inplace=True)
# df.head()
df.describe() df.describe()
# df.plot()
# plt.show()
### Testing For Stationarity
adfuller(df['CALTEX Delo Gold Ultra']) from statsmodels.tsa.stattools import adfuller
test_result = adfuller(df['CALTEX_Delo_Gold_Ultra'])
test_result2 = adfuller(df['Price1']) test_result2 = adfuller(df['Price1'])
def adfuller_test(Product1): def adfuller_test(CALTEX_Delo_Gold_Ultra):
result = adfuller(Product1) result = adfuller(CALTEX_Delo_Gold_Ultra)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used'] labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result, labels): for value, label in zip(result, labels):
print(label + ' : ' + str(value)) print(label + ' : ' + str(value))
def adfuller_test2(Price1): def adfuller_test2(Price1):
result2 = adfuller(Price1) result2 = adfuller(Price1)
adfuller_test(df['CALTEX Delo Gold Ultra']) labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result2, labels):
print(label + ' : ' + str(value))
adfuller_test(df['CALTEX_Delo_Gold_Ultra'])
adfuller_test2(df['Price1']) adfuller_test2(df['Price1'])
##Differencing ##Differencing
df['Product1 First Difference'] = df['CALTEX Delo Gold Ultra'] - df['CALTEX Delo Gold Ultra'].shift(1) df['Product1 First Difference'] = df['CALTEX_Delo_Gold_Ultra'] - df['CALTEX_Delo_Gold_Ultra'].shift(1)
df['Product1 First Difference2'] = df['Price1'] - df['Price1'].shift(1) df['Product1 First Difference2'] = df['Price1'] - df['Price1'].shift(1)
# df['Sales'].shift(1) # df['Sales'].shift(1)
df['Seasonal First Difference'] = df['CALTEX Delo Gold Ultra'] - df['CALTEX Delo Gold Ultra'].shift(12) df['Seasonal First Difference'] = df['CALTEX_Delo_Gold_Ultra'] - df['CALTEX_Delo_Gold_Ultra'].shift(12)
df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12) df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12)
# df.head(14) # df.head(14)
...@@ -211,34 +228,45 @@ def Forecasting(request): ...@@ -211,34 +228,45 @@ def Forecasting(request):
adfuller_test(df['Seasonal First Difference'].dropna()) adfuller_test(df['Seasonal First Difference'].dropna())
adfuller_test2(df['Seasonal First Difference2'].dropna()) adfuller_test2(df['Seasonal First Difference2'].dropna())
# ARIMA import statsmodels.api as sm
model = ARIMA(df['CALTEX Delo Gold Ultra'], order=(1, 0, 0)) # For non-seasonal data
# p=1, d=1, q=0 or 1
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(df['CALTEX_Delo_Gold_Ultra'], order=(1, 0, 0))
model2 = ARIMA(df['Price1'], order=(1, 0, 0)) model2 = ARIMA(df['Price1'], order=(1, 0, 0))
model_fit = model.fit() model_fit = model.fit()
print(model_fit.summary())
model_fit2 = model2.fit() model_fit2 = model2.fit()
print(model_fit2.summary())
# model_fit.summary() # model_fit.summary()
df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True) df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True) df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True)
# df[['Product1','forecast']].plot(figsize=(12,8)) # df[['Product1','forecast']].plot(figsize=(12,8))
model = sm.tsa.statespace.SARIMAX(df['CALTEX Delo Gold Ultra'], order=(1, 1, 1), model = sm.tsa.statespace.SARIMAX(df['CALTEX_Delo_Gold_Ultra'], order=(1, 1, 1),
seasonal_order=(1, 1, 1, 12)) seasonal_order=(1, 1, 1, 12))
model2 = sm.tsa.statespace.SARIMAX(df['Price1'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12)) model2 = sm.tsa.statespace.SARIMAX(df['Price1'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
results = model.fit() results = model.fit()
results2 = model2.fit() results2 = model2.fit()
# df['forecast']=results.predict(start=100,end=100,dynamic=True)
# df[['Product1','forecast']].plot(figsize=(12,8))
from pandas.tseries.offsets import DateOffset
future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)] future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns) future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
# future_datest_df.tail() # future_datest_df.tail()
future_df = pd.concat([df, future_datest_df]) future_df = pd.concat([df, future_datest_df])
# work with date
inputdatex = inputdate # work with date
inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")
# 10/01/2025 # find Date Space
inputdatex = myForm.cleaned_data['inputdate']
inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")
lastDate = '10/01/2021' lastDate = '07/01/2020'
inputdate = inputdate inputdate = inputdate
date_format = "%m/%d/%Y" date_format = "%m/%d/%Y"
...@@ -248,32 +276,365 @@ def Forecasting(request): ...@@ -248,32 +276,365 @@ def Forecasting(request):
delta2 = delta.days delta2 = delta.days
# print(delta2) # print(delta2)
forcasting_date = int(delta2 / 30) forcasting_date = int(delta2 / 30)
newDate = forcasting_date + 121 # ending number newDate = forcasting_date + 102 # ending number
# print(forcasting_date) # print(forcasting_date)
forecast = future_df['forecast'] = results.predict(start=119, end=newDate, dynamic=True) forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
# future_df[['Sales', 'forecast']].plot(figsize=(12, 8)) # future_df[['Sales', 'forecast']].plot(figsize=(12, 8))
forecastPrice = future_df['forecastPrice'] = results2.predict(start=119, end=newDate, dynamic=True) forecastPrice = future_df['forecastPrice'] = results2.predict(start=104, end=newDate, dynamic=True)
# future_df[['Price1', 'forecastPrice']].plot(figsize=(12, 8)) # future_df[['Price1', 'forecastPrice']].plot(figsize=(12, 8))
df3 = forecast[-1] df3 = forecast[-1]
df4 = forecastPrice[-1] df4 = forecastPrice[-1]
print(int(df3))
print(int(df4))
arz = int(df3)
arz2 = int(df4)
arz3 = arz * arz2
context = {'arz': arz, 'arz2': arz2, 'arz3': arz3, 'datagot': inputdate}
template = loader.get_template('Admin/Result.html')
# returing the template
return HttpResponse(template.render(context, request))
if (Productid == 2): # load 2nd product budget
df = pd.read_csv('Data/Purchasefile.csv')
df = df.drop(
['CALTEX_Delo_Gold_Ultra', 'Price1', 'Substitude_Product_Price1', 'Production_cost_per_unit1',
'CALTEX_Texamatic_1888_Transmision_Oil', 'Price3', 'Substitude_Product_Price3',
'Production_cost_per_unit3'], axis=1)
df.head()
df.columns = ["Month", "VIC_Hino_Fuel_Filter", "Price2", "Substitude_Product_Price2",
"Production_cost_per_unit2"]
df['Month'] = pd.to_datetime(df['Month'])
df.set_index('Month', inplace=True)
from statsmodels.tsa.stattools import adfuller
test_result = adfuller(df['VIC_Hino_Fuel_Filter'])
test_result2 = adfuller(df['Price2'])
def adfuller_test(VIC_Hino_Fuel_Filter):
result = adfuller(VIC_Hino_Fuel_Filter)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result, labels):
print(label + ' : ' + str(value))
def adfuller_test2(Price2):
result2 = adfuller(Price2)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result2, labels):
print(label + ' : ' + str(value))
adfuller_test(df['VIC_Hino_Fuel_Filter'])
adfuller_test2(df['Price2'])
PredictedDemand = int(df3) df['Product1 First Difference'] = df['VIC_Hino_Fuel_Filter'] - df['VIC_Hino_Fuel_Filter'].shift(1)
PredictedPrice = int(df4) df['Product1 First Difference2'] = df['Price2'] - df['Price2'].shift(1)
PredictedBudget = PredictedDemand * PredictedPrice
df['Seasonal First Difference'] = df['VIC_Hino_Fuel_Filter'] - df['VIC_Hino_Fuel_Filter'].shift(12)
df['Seasonal First Difference2'] = df['Price2'] - df['Price2'].shift(12)
context = { 'arz': PredictedDemand , 'arz2': PredictedPrice , 'arz3': PredictedBudget , 'datagot': inputdate } adfuller_test(df['Seasonal First Difference'].dropna())
adfuller_test2(df['Seasonal First Difference2'].dropna())
import statsmodels.api as sm
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(df['VIC_Hino_Fuel_Filter'], order=(1, 0, 0))
model2 = ARIMA(df['Price2'], order=(1, 0, 0))
model_fit = model.fit()
model_fit2 = model2.fit()
df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True)
model = sm.tsa.statespace.SARIMAX(df['VIC_Hino_Fuel_Filter'], order=(1, 1, 1),
seasonal_order=(1, 1, 1, 12))
model2 = sm.tsa.statespace.SARIMAX(df['Price2'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
results = model.fit()
results2 = model2.fit()
from pandas.tseries.offsets import DateOffset
future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
# future_datest_df.tail()
future_df = pd.concat([df, future_datest_df])
inputdatex = myForm.cleaned_data['inputdate']
inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")
lastDate = '07/01/2020'
inputdate = inputdate
date_format = "%m/%d/%Y"
a = datetime.strptime(lastDate, date_format) # user input date
b = datetime.strptime(inputdate, date_format)
delta = b - a
delta2 = delta.days
# print(delta2)
forcasting_date = int(delta2 / 30)
newDate = forcasting_date + 102 # ending number
forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
forecastPrice = future_df['forecastPrice'] = results2.predict(start=104, end=newDate, dynamic=True)
df3 = forecast[-1]
df4 = forecastPrice[-1]
print(int(df3))
print(int(df4))
arz = int(df3)
arz2 = int(df4)
arz3 = arz * arz2
context = {'arz': arz, 'arz2': arz2, 'arz3': arz3, 'datagot': inputdate}
template = loader.get_template('Admin/Result.html') template = loader.get_template('Admin/Result.html')
return HttpResponse(template.render(context, request)) return HttpResponse(template.render(context, request))
else:
form = MyForm4() if (Productid == 3): # load 3rd product budget
#returning form df = pd.read_csv('Data/Purchasefile.csv')
return render(request, 'Admin/Forecasting.html', {'form':form}); df = df.drop(
['CALTEX_Delo_Gold_Ultra', 'Price1', 'Substitude_Product_Price1', 'Production_cost_per_unit1',
'VIC_Hino_Fuel_Filter', 'Price2', 'Substitude_Product_Price2', 'Production_cost_per_unit2'],
axis=1)
df.head()
df.columns = ["Month", "CALTEX_Texamatic_1888_Transmision_Oil", "Price3", "Substitude_Product_Price3",
"Production_cost_per_unit3"]
df['Month'] = pd.to_datetime(df['Month'])
df.set_index('Month', inplace=True)
from statsmodels.tsa.stattools import adfuller
test_result = adfuller(df['CALTEX_Texamatic_1888_Transmision_Oil'])
test_result2 = adfuller(df['Price3'])
def adfuller_test(CALTEX_Texamatic_1888_Transmision_Oil):
result = adfuller(CALTEX_Texamatic_1888_Transmision_Oil)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result, labels):
print(label + ' : ' + str(value))
def adfuller_test2(Price3):
result2 = adfuller(Price3)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result2, labels):
print(label + ' : ' + str(value))
adfuller_test(df['CALTEX_Texamatic_1888_Transmision_Oil'])
adfuller_test2(df['Price3'])
df['Product1 First Difference'] = df['CALTEX_Texamatic_1888_Transmision_Oil'] - df[
'CALTEX_Texamatic_1888_Transmision_Oil'].shift(1)
df['Product1 First Difference2'] = df['Price3'] - df['Price3'].shift(1)
df['Seasonal First Difference'] = df['CALTEX_Texamatic_1888_Transmision_Oil'] - df[
'CALTEX_Texamatic_1888_Transmision_Oil'].shift(12)
df['Seasonal First Difference2'] = df['Price3'] - df['Price3'].shift(12)
adfuller_test(df['Seasonal First Difference'].dropna())
adfuller_test2(df['Seasonal First Difference2'].dropna())
import statsmodels.api as sm
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 0, 0))
model2 = ARIMA(df['Price3'], order=(1, 0, 0))
model_fit = model.fit()
model_fit2 = model2.fit()
df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True)
model = sm.tsa.statespace.SARIMAX(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 1, 1),
seasonal_order=(1, 1, 1, 12))
model2 = sm.tsa.statespace.SARIMAX(df['Price3'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
results = model.fit()
results2 = model2.fit()
from pandas.tseries.offsets import DateOffset
future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
# future_datest_df.tail()
future_df = pd.concat([df, future_datest_df])
inputdatex = myForm.cleaned_data['inputdate']
inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")
lastDate = '07/01/2020'
inputdate = inputdate
date_format = "%m/%d/%Y"
a = datetime.strptime(lastDate, date_format) # user input date
b = datetime.strptime(inputdate, date_format)
delta = b - a
delta2 = delta.days
# print(delta2)
forcasting_date = int(delta2 / 30)
newDate = forcasting_date + 102 # ending number
forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
forecastPrice = future_df['forecastPrice'] = results2.predict(start=104, end=newDate, dynamic=True)
df3 = forecast[-1]
df4 = forecastPrice[-1]
print(int(df3))
print(int(df4))
arz = int(df3)
arz2 = int(df4)
arz3 = arz * arz2
context = {'arz': arz, 'arz2': arz2, 'arz3': arz3, 'datagot': inputdate}
template = loader.get_template('Admin/Result.html')
return HttpResponse(template.render(context, request))
else:
form = MyForm4()
# returning form
return render(request, 'Admin/Forecasting.html', {'form': form});
# end Admin ML
# End Demand Forecasting Machine Learning Model # End Demand Forecasting Machine Learning Model
# start Admin ML
def HighestDemand(request):
if request.method == 'POST':
myForm = MyFormNew(request.POST)
if myForm.is_valid():
df = pd.read_csv('Data/Purchasefile.csv')
df = df.drop(['Price1','Price2','Price3'], axis=1)
df.head()
## Cleaning up the data
df.columns = ["Month", "CALTEX_Delo_Gold_Ultra", "Substitude_Product_Price1",
"Production_cost_per_unit1", "VIC_Hino_Fuel_Filter",
"Substitude_Product_Price2", "Production_cost_per_unit2",
"CALTEX_Texamatic_1888_Transmision_Oil", "Substitude_Product_Price3",
"Production_cost_per_unit3"]
rrr = df.head()
# Convert Month into Datetime
df['Month'] = pd.to_datetime(df['Month'])
df.set_index('Month', inplace=True)
df.describe()
from statsmodels.tsa.stattools import adfuller
test_result = adfuller(df['CALTEX_Delo_Gold_Ultra'])
test_result2 = adfuller(df['VIC_Hino_Fuel_Filter'])
test_result2 = adfuller(df['CALTEX_Texamatic_1888_Transmision_Oil'])
def adfuller_test(CALTEX_Delo_Gold_Ultra):
result = adfuller(CALTEX_Delo_Gold_Ultra)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result, labels):
print(label + ' : ' + str(value))
def adfuller_test2(VIC_Hino_Fuel_Filter):
result2 = adfuller(VIC_Hino_Fuel_Filter)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result2, labels):
print(label + ' : ' + str(value))
def adfuller_test3(CALTEX_Texamatic_1888_Transmision_Oil):
result3 = adfuller(CALTEX_Texamatic_1888_Transmision_Oil)
labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
for value, label in zip(result3, labels):
print(label + ' : ' + str(value))
adfuller_test(df['CALTEX_Delo_Gold_Ultra'])
adfuller_test2(df['VIC_Hino_Fuel_Filter'])
adfuller_test3(df['CALTEX_Texamatic_1888_Transmision_Oil'])
df['Seasonal First Difference'] = df['CALTEX_Delo_Gold_Ultra'] - df['CALTEX_Delo_Gold_Ultra'].shift(1)
# df['Product1 First Difference2'] = df['Price1'] - df['Price1'].shift(1)
df['Seasonal First Difference2'] = df['VIC_Hino_Fuel_Filter'] - df['VIC_Hino_Fuel_Filter'].shift(12)
# df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12)
df['Seasonal First Difference3'] = df['CALTEX_Texamatic_1888_Transmision_Oil'] - df['CALTEX_Texamatic_1888_Transmision_Oil'].shift(12)
# df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12)
adfuller_test(df['Seasonal First Difference'].dropna())
adfuller_test2(df['Seasonal First Difference2'].dropna())
adfuller_test3(df['Seasonal First Difference3'].dropna())
import statsmodels.api as sm
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(df['CALTEX_Delo_Gold_Ultra'], order=(1, 0, 0))
model2 = ARIMA(df['VIC_Hino_Fuel_Filter'], order=(1, 0, 0))
model3 = ARIMA(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 0, 0))
model_fit = model.fit()
model_fit2 = model2.fit()
model_fit3 = model3.fit()
df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
df['forecastP2'] = model_fit2.predict(start=104, end=120, dynamic=True)
df['forecast3'] = model_fit3.predict(start=104, end=120, dynamic=True)
# df[['Product1','forecast']].plot(figsize=(12,8))
model = sm.tsa.statespace.SARIMAX(df['CALTEX_Delo_Gold_Ultra'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
model2 = sm.tsa.statespace.SARIMAX(df['VIC_Hino_Fuel_Filter'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
model3 = sm.tsa.statespace.SARIMAX(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 1, 1),
seasonal_order=(1, 1, 1, 12))
results = model.fit()
results2 = model2.fit()
results3 = model3.fit()
from pandas.tseries.offsets import DateOffset
future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
future_df = pd.concat([df, future_datest_df])
inputdatex = myForm.cleaned_data['inputdate']
inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")
lastDate = '07/01/2020'
inputdate = inputdate
date_format = "%m/%d/%Y"
a = datetime.strptime(lastDate, date_format) # user input date
b = datetime.strptime(inputdate, date_format)
delta = b - a
delta2 = delta.days
# print(delta2)
forcasting_date = int(delta2 / 30)
newDate = forcasting_date + 102 # ending number
# print(forcasting_date)
forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
forecastP2 = future_df['forecastP2'] = results2.predict(start=104, end=newDate, dynamic=True)
forecastP3 = future_df['forecastP3'] = results3.predict(start=104, end=newDate, dynamic=True)
df3 = forecast[-1] # 1st product
df4 = forecastP2[-1] # 2nd product
df5 = forecastP3[-1] # 3rd product
print (int(df3))
print(int(df4))
print(int(df5))
maxvalue = max(df3, df4, df5)
# arz = int(maxvalue)
if (maxvalue == df3 ):
arz = "CALTEX_Delo_Gold_Ultra"
if (maxvalue == df4 ):
arz = "VIC_Hino_Fuel_Filter"
if (maxvalue == df5):
arz = "CALTEX_Texamatic_1888_Transmision_Oil"
context = { 'arz': arz , 'datagot': inputdate }
template = loader.get_template('Admin/Result2.html')
#returing the template
return HttpResponse(template.render(context, request))
else:
form = MyFormNew()
#returning form
return render(request, 'Admin/HighestDemand.html', {'form':form});
# end Admin ML
# Customer Data # Customer Data
def Customer_Data(request): def Customer_Data(request):
......
No preview for this file type
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
{{form.as_table}} {{form.as_table}}
</table> </table>
<input type="submit" value="FORECAST" class="btn btn--radius-2 btn" style="background:#02111f"/> <input type="submit" value="FORECAST" class="btn btn--radius-2 btn" style="background: #747578; border: none; color: white;"/>
<div role="alert"> <div role="alert">
......
...@@ -184,7 +184,7 @@ body{ ...@@ -184,7 +184,7 @@ body{
<li><a style="text-decoration:none;" href="/Admin/EmployeeStatus_Update/"><i class="fas fa-upload"></i> Assign Employee </a> <li><a style="text-decoration:none;" href="/Admin/EmployeeStatus_Update/"><i class="fas fa-upload"></i> Assign Employee </a>
</li> </li>
<li><a style="text-decoration:none;" href="/Admin/Forecasting/"><i class="fas fa-bullseye "></i> Demand Forecasting </a> <li><a style="text-decoration:none;" href="/Admin/Forecasting/"><i class="fas fa-bullseye "></i> Demand Forecasting </a>
<li><a style="text-decoration:none;" href="#"><i class="fas fa-eye"></i> Highest Demand </a> <li><a style="text-decoration:none;" href="/Admin/HighestDemand/"><i class="fas fa-eye"></i> Highest Demand </a>
<li><a style="text-decoration:none;" href="/Admin/PurchaseHistory/"><i class="fas fa-cubes"></i> Purchase History </a> <li><a style="text-decoration:none;" href="/Admin/PurchaseHistory/"><i class="fas fa-cubes"></i> Purchase History </a>
<li><a style="text-decoration:none;" href="/Admin/Customer_Data/"><i class="fas fa-user"></i> Customer Data </a> <li><a style="text-decoration:none;" href="/Admin/Customer_Data/"><i class="fas fa-user"></i> Customer Data </a>
<li><a style="text-decoration:none;" href="/Admin/Customer_Frequency/"><i class="fas fa-chart-bar"></i> Customer Frequency </a> <li><a style="text-decoration:none;" href="/Admin/Customer_Frequency/"><i class="fas fa-chart-bar"></i> Customer Frequency </a>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment