Ejemplo n.º 1
0
def _load_model(path):

    from prophet.serialize import model_from_json

    with open(path, "r") as f:
        model = json.load(f)
    return model_from_json(model)
Ejemplo n.º 2
0
    def _grouped_model_from_dict(cls, raw_model):

        deser_model_payload = {
            literal_eval(master_key): model_from_json(payload)
            for master_key, payload in raw_model.items()
        }
        return deser_model_payload
Ejemplo n.º 3
0
def predict(k_trans_pro, days):
    with open('serialized_model.json', 'r') as fin:
        pro_model_tuned = model_from_json(json.load(fin))  # Load model

    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)

    forecast_pro = pro_model_tuned.predict(pro_test_df)
    predictions = forecast_pro[['ds', 'yhat']].head()
    return predictions
def load_model(file_name, r):
    print(f"called load_model method with parameters: file_name = %s" %
          file_name)
    key = prefix_cache + file_name
    return model_from_json(r.get(key))
Ejemplo n.º 5
0
 def restore(self, checkpoint_file):
     with open(checkpoint_file, 'r') as fin:
         self.model = model_from_json(json.load(fin))
     self.model_init = True
Ejemplo n.º 6
0
from prophet.serialize import model_from_json

with open('/tmp/model.json', 'r') as fin:
    m = model_from_json(fin.read())  # Load model

future = m.make_future_dataframe(periods=365)
forecast = m.predict(future)
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
Ejemplo n.º 7
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)