-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprophet.py
67 lines (41 loc) · 1.48 KB
/
prophet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pandas as pd # Importing PANDAS
df = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/monthly-car-sales.csv')
df
type(df)
df.info()
df.isnull().sum() # Searching for Missing Values
df.rename(columns = {'Sales':'y','Month':'ds'},inplace = True)
# Renaming the Month and Sales columns as 'ds' and 'y'
df.info()
# As the columns are not in the required datatype formats
# We have to convert 'ds' to TIMESTAMP FORMAT or DATETIME LIBRARY
# And convert 'y' to INTEGER TYPE (int or float)
df['ds'] = pd.to_datetime(df['ds'])
df['y'] = df['y'].astype(int)
df.info()
df
import matplotlib.pyplot as plt
plt.figure(figsize=(20,10))
plt.scatter(df['ds'],df['y'])
import numpy as np
#Creating An Outliner
df['y'] = np.where(df['y']>25000, np.nan, df['y'])
df['y'] = np.where(df['y']<5600, np.nan, df['y'])
df.isnull().sum()
import matplotlib.pyplot as plt
plt.figure(figsize=(20,10))
plt.scatter(df['ds'],df['y'])
# Implementing a forecasting model using "FBProphet" for the given dataset
import prophet
model = prophet.Prophet(changepoint_prior_scale=1)
model.fit(df)
# Predicting according to "Months" (Keeping freq = 'M')
# Forecasting for 500 Days
forecast = model.make_future_dataframe(periods = 17, freq='M',include_history=True)
forecast = model.predict(forecast)
model.plot(forecast,xlabel = 'Date',ylabel = 'y', figsize=(24,10),)
plt.show()
**Saving the model using joblib**
import joblib
joblib.dump(model,'model_jlib')
joblib.load('model_jlib')