Created
November 17, 2020 17:42
-
-
Save vittorio-nardone/04b590214ed481db35cc1ef4b4135012 to your computer and use it in GitHub Desktop.
A simple Metaflow flow to train a Prophet model using defaults
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from io import StringIO | |
from metaflow import FlowSpec, step, Parameter, IncludeFile | |
from fbprophet import Prophet | |
class ProphetFlow(FlowSpec): | |
""" | |
ProphetFlow use Facebook Prophet to predict future values of a | |
timeseries. | |
""" | |
data_file = IncludeFile('datafile', | |
is_text=True, | |
help='Time series data file - csv file format', | |
default='data/daily-min-temperatures.txt') | |
columns_mapping = Parameter('columns', | |
default={'Date':'ds','Temp':'y'}, | |
help="Rename columns according to Prophet standards") | |
@step | |
def start(self): | |
""" | |
Raw data is loaded and prepared | |
""" | |
# Load csv in pandas dataframe | |
self.df = pd.read_csv(StringIO(self.data_file)) | |
# Rename columns to meet Prophet input dataframe standards | |
self.df.rename(columns=self.columns_mapping, inplace=True) | |
# Convert Date column to datetime64 dtype | |
self.df['ds']= pd.to_datetime(self.df['ds'], infer_datetime_format=True) | |
self.next(self.train) | |
@step | |
def train(self): | |
""" | |
A new Prophet model is fitted. | |
""" | |
# Fit a new model using defaults | |
self.m = Prophet() | |
self.m.fit(self.df) | |
self.next(self.end) | |
@step | |
def end(self): | |
""" | |
Last step, process is finished | |
""" | |
print("ProphetFlow is all done.") | |
if __name__ == '__main__': | |
ProphetFlow() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment