def model_json(model):
    json_file = model_to_json(model)
    json_file = json.dumps(json_file)
    b64 = base64.b64encode(json_file.encode()).decode()
    href = f'<a href="data:file/json;base64,{b64}" download="loadForecast_model.json" ' \
           f'target="_blank">Download model</a>'
    return href
Ejemplo n.º 2
0
def get_prophet_forecast(df, country="ALL", save_model=True):

    df_ts = df.copy() if country == "ALL" else df[df['country'] ==
                                                  country].copy()
    if len(df_ts) <= 180:
        return None

    df_month = df_ts.groupby('inv_month').sum().reset_index()
    df_ts['inv_date'] = pd.to_datetime(df_ts['inv_date'])

    df_ts.rename(columns={'inv_date': 'ds', 'value': 'y'}, inplace=True)
    m = Prophet(yearly_seasonality=20)
    m.fit(df_ts)

    if save_model:
        with open(fr'models\{country}_model.json', 'w') as fout:
            json.dump(model_to_json(m), fout)  # Save model

    future = m.make_future_dataframe(periods=180, freq='D')
    forecast = m.predict(future)
    forecast['inv_month'] = forecast['ds'].apply(
        lambda v: date(v.year, v.month, 1).isoformat())
    monthly_forecast = forecast[['inv_month', 'yhat'
                                 ]].groupby('inv_month').sum().reset_index()

    df_forecast = pd.merge(monthly_forecast,
                           df_month,
                           on='inv_month',
                           how='left')
    return df_forecast
Ejemplo n.º 3
0
def _save_model(model, path):

    from prophet.serialize import model_to_json

    model_ser = model_to_json(model)
    with open(path, "w") as f:
        json.dump(model_ser, f)
Ejemplo n.º 4
0
def prophet_model(Train, Days):

    # make sure to get the input here, then uncomment the rest
    ###
    # HERE, convert the  json that looks like this
    # {"income":[{income fields},{},{}], "expense":spend_dict}
    # to a dataframe
    # the you can just do this: jsonify({"predictions": yourlist})
    ###
    k_trans_pro = Train
    n = len(k_trans_pro)
    d = Days
    pro_train_df = k_trans_pro[0:n - d]
    pro_test_df_y = k_trans_pro[n - d:]
    pro_test_df = k_trans_pro[n - d:].drop(['y'], axis=1)

    param_grid = {
        'changepoint_prior_scale': [0.001, 0.01, 0.1, 0.5],
        'seasonality_prior_scale': [0.01, 0.1, 1.0, 10.0],
    }

    # Generate all combinations of parameters
    all_params = [
        dict(zip(param_grid.keys(), v))
        for v in itertools.product(*param_grid.values())
    ]
    rmses = []  # Store the RMSEs for each params here

    # Use cross validation to evaluate all parameters
    for params in all_params:
        m = Prophet(**params).fit(pro_train_df)  # Fit model with given params
        df_cv = cross_validation(m,
                                 period='30 days',
                                 horizon='30 days',
                                 parallel="processes")
        df_p = performance_metrics(df_cv, rolling_window=1)
        rmses.append(df_p['rmse'].values[0])

    # Find the best parameters
    tuning_results = pd.DataFrame(all_params)
    tuning_results['rmse'] = rmses
    best_params = all_params[np.argmin(rmses)]
    pro_model_tuned = Prophet(**best_params).fit(pro_train_df)

    with open('serialized_model.json', 'w') as fout:
        json.dump(model_to_json(pro_model_tuned), fout)  # Save model
    ## Here you find the predictions

    ###
    # HERE, convert the precitions to a json doc that looks like this
    # { "predictions": [ {"ds": value, "yhat":value}, {"ds": value, "yhat":value}, {"ds": value, "yhat":value}, ]}
    # just get a list from your dataframe.
    # the you can just do this: jsonify({"predictions": yourlist})

    return None
Ejemplo n.º 5
0
    def _grouped_model_to_dict(self):

        model_dict = {
            attr: getattr(self, attr)
            for attr in GROUPED_MODEL_BASE_ATTRIBUTES
        }
        model_dict["model"] = {
            str(master_key): model_to_json(model)
            for master_key, model in self.model.items()
        }
        return model_dict
Ejemplo n.º 6
0
 def calc(self, arg):
     print("impl start", flush=True)
     try:
         ds_array = []
         y_array = []
         for row in arg:
             ds_array.append(row[0])
             y_array.append(row[1])
         list_of_tuples = list(zip(ds_array, y_array))
         df = pd.DataFrame(list_of_tuples, columns=['ds', 'y'])
         with suppress_stdout_stderr():
             m = Prophet(daily_seasonality=True,
                         weekly_seasonality=True,
                         yearly_seasonality=True)
             m.fit(df)
         print("impl train end", flush=True)
         self._collector.collectRow(model_to_json(m))
     except BaseException as ex:
         print("{}".format(ex), flush=True)
     else:
         print("impl finished", flush=True)
def save_model(file_name, model, r):
    print(f"called save_model method with parameters: file_name = %s" %
          file_name)
    file = prefix_cache + file_name
    r.mset({file: model_to_json(model)})
Ejemplo n.º 8
0
 def save(self, checkpoint_file):
     if self.model is None:
         raise Exception(
             "Needs to call fit_eval or restore first before calling save")
     with open(checkpoint_file, 'w') as fout:
         json.dump(model_to_json(self.model), fout)
Ejemplo n.º 9
0
    def calc(self, conf, argv1, argv2):
        print("Entering Python calc", flush=True)
        try:
            growth = conf['growth']
            if growth is None:
                growth = 'linear'

            predict_num = conf['periods']
            if predict_num is None:
                predict_num = 2
            else:
                predict_num = int(predict_num)

            freq = conf['freq']
            if freq is None:
                freq = 'D'

            uncertainty_samples = conf['uncertainty_samples']
            if uncertainty_samples is None:
                uncertainty_samples = 1000
            else:
                uncertainty_samples = int(uncertainty_samples)

            stan_i = conf['init_model']
            if stan_i is not None:
                stan_i = self.stan_init2(json.loads(stan_i))
                dimDelta = len(stan_i['delta'])

            #data
            ds_array = []
            y_array = []
            for row in argv1:
                ds_array.append(row[0])
                y_array.append(row[1])
            list_of_tuples = list(zip(ds_array, y_array))
            df = pd.DataFrame(list_of_tuples, columns=['ds', 'y'])

            dataLen = len(list_of_tuples)

            # init model
            with suppress_stdout_stderr():
                m = Prophet(growth=growth,
                            uncertainty_samples=uncertainty_samples)
                if stan_i is not None and dataLen == dimDelta + 2:
                    m.fit(df, init=stan_i)
                elif argv2 is None or argv2[0][0] is None:
                    m.fit(df)
                else:
                    init_model_str = argv2[0][0]
                    init_model = model_from_json(init_model_str)

                    # fit and pred
                    m.fit(df, init=stan_init(init_model))

            future = m.make_future_dataframe(periods=predict_num,
                                             freq=freq,
                                             include_history=False)
            pout = m.predict(future)

            self._collector.collectRow(model_to_json(m), pout.to_json(),
                                       json.dumps(pout.yhat.values.tolist()))
        except BaseException as ex:
            print({}.format(ex), flush=True)
            raise ex
        finally:
            print("Leaving Python calc", flush=True)
Ejemplo n.º 10
0
import pandas as pd
from prophet import Prophet
from prophet.serialize import model_to_json

# float_precision='high' required for pd.read_csv to match precision of Rover.read_csv
df = pd.read_csv('examples/example_wp_log_peyton_manning.csv',
                 float_precision='high')

m = Prophet()
m.fit(df, seed=123)

with open('/tmp/model.json', 'w') as fout:
    fout.write(model_to_json(m))