Example #1
0
def core_PROPHET(df_master, dimension, region, changepoint_prior_scale,
                 periods):
    df_master.rename(columns={
        df_master.columns[0]: 'ds',
        df_master.columns[1]: 'y'
    },
                     inplace=True)

    m = Prophet(changepoint_prior_scale=changepoint_prior_scale)
    m.fit(df_master)
    future = m.make_future_dataframe(periods=periods)
    forecast = m.predict(future)

    if region == 'all':
        region = 'Italy'

    fig = m.plot(forecast, xlabel='Date', ylabel=dimension + ' for ' + region)
    add_changepoints_to_plot(fig.gca(), m, forecast)
    m.plot_components(forecast)
    plt.pause(10)

    forecast.rename(columns={
        'ds': 'data',
        'yhat': 'predizione',
        'yhat_lower': 'intervallo_inferiore',
        'yhat_upper': 'intervallo_superiore'
    },
                    inplace=True)
    print(forecast[[
        'data', 'predizione', 'intervallo_inferiore', 'intervallo_superiore'
    ]].tail(periods))
Example #2
0
def get_figures(filepath):
    df = pd.read_csv(filepath)
    df.columns = ['ds', 'y']
    m = Prophet().fit(df)
    future = m.make_future_dataframe(periods=90)
    forecast = m.predict(future)
    forecast_fig = m.plot(forecast)
    add_changepoints_to_plot(forecast_fig.gca(), m, forecast)
    components_fig = m.plot_components(forecast)
    return forecast_fig, components_fig
Example #3
0
def process_job(conn, job):
    assert job['type'].lower() in ['cases', 'deaths', 'tests']

    print(f"{time.strftime('%H:%M:%S')} Starting job={job}")

    data = query_data(conn, job)

    df = prepare_data(job, data)
    m = create_model(job)

    m.fit(df)

    # predict a third into the future of what we looked back
    future_days = round(job['days_to_look_back'] / 3)
    future = m.make_future_dataframe(periods=future_days)

    future['cap'] = df['cap'][0]
    forecast = m.predict(future)

    # region debug
    if os.getenv('DOCKER_ACTIVE') is None:
        fig = m.plot(forecast)
        add_changepoints_to_plot(fig.gca(), m, forecast)
        fig.savefig(f"../job/prediction-{job['id']}.png")
    # endregion

    change_points = m.changepoints.dt.date.tolist()
    store_prediction(conn, job, forecast, change_points)

    # cross validate and create score
    if job['with_score']:
        # compute period to have 5-6 simulated forecasts
        horizon = pd.Timedelta("14 days")
        initial = horizon * 3
        period = (df.iloc[-1]['ds'] - df.iloc[0]['ds'] - horizon - initial) / 5

        df_cv = cross_validation(m,
                                 initial=initial,
                                 horizon=horizon,
                                 period=period,
                                 parallel='processes')
        df_p = performance_metrics(df_cv)

        # region debug
        if os.getenv('DOCKER_ACTIVE') is None:
            fig = plot_cross_validation_metric(df_cv, metric='mape')
            fig.savefig(f"../job/score-{job['id']}.png")
        # endregion

        score = df_p.iloc[-1]['mape']
        store_score(conn, job, score)
Example #4
0
def prophet(data):
        model = Prophet()
        model.fit(data)

        future = model.make_future_dataframe(periods=365)
        forecast = model.predict(future)
        forecast.tail()

        fig1 = model.plot(forecast)
        fig2 = model.plot_components(forecast)

        add_changepoints_to_plot(fig1.gca(), model, forecast, threshold=0)
        
        plt.show()
def test3():
    """
        手动指定潜在突变点的位置,而非利用自动的突变点检测,可以使用changepoints 参数
        m = Prophet(changepoints=['2014-01-01'])
    """
    df = pd.read_csv('data/example_wp_log_peyton_manning.csv')
    m = Prophet(changepoints=['2014-01-01'])
    m.fit(df)
    future = m.make_future_dataframe(periods=365)
    forecast = m.predict(future)
    fig = m.plot(forecast)
    add_changepoints_to_plot(fig.gca(), m, forecast)
    m.plot(forecast)
    plt.show()
Example #6
0
def get_data():
    from dateutil.parser import parse
    data_path = u'/Users/longguangbin/Work/Documents/SAAS/安踏线下/季节性/sample/duanku_sales.xls'
    data = pd.read_excel(data_path)

    data['week_sale'] = data['sku_num_sum'].rolling(window=7).sum()
    data.loc[:5, ['week_sale']] = data[:6]['sku_num_sum'].cumsum()
    data['week_day'] = data['commit_date'].apply(lambda x: parse(x).weekday())
    data_week = data[data['week_day'] == 6]

    data['commit_month'] = data['commit_date'].apply(lambda x: parse(x).strftime('%Y-%m'))
    data_month = data.groupby(['commit_month']).agg({'sku_num_sum': 'sum'}).reset_index()

    df = data.loc[:, ['commit_date', 'sku_num_sum']]
    df.columns = ['ds', 'y']
    df = data_month.loc[:, ['commit_month', 'sku_num_sum']]
    df.columns = ['ds', 'y']
    df = data_week.loc[:, ['commit_date', 'sku_num_sum']]
    df.columns = ['ds', 'y']
    # df = data_month['sku_num_sum'].values
    # df = data_week['sku_num_sum'].values

    m = Prophet(growth='logistic', changepoint_prior_scale=0.005, changepoint_range=0.9)
    df['cap'] = np.max(df['y']) * 1.2
    df['floor'] = 0
    m.fit(df)
    future = m.make_future_dataframe(periods=360)
    future['cap'] = np.max(df['y']) * 1.2
    future['floor'] = 0
    forecast = m.predict(future)
    fig = m.plot(forecast)
    a = add_changepoints_to_plot(fig.gca(), m, forecast)
    m.plot_components(forecast)
Example #7
0
def daily_cases_fb_forecast(df, d, imgdir, attribution, figsize=(14, 9)):
    # fit model
    log.info("# Fitting daily cases using Prophet")
    cases = df.groupby(df.date)['cases'].sum().diff()
    df = cases.to_frame().reset_index().fillna(0)
    df.columns = ['ds', 'y']
    m = Prophet(interval_width=0.95)
    with SuppressStdout():
        m.fit(df)
    future = m.make_future_dataframe(periods=d)
    pred = m.predict(future)

    # plot result
    plt.figure(figsize=figsize)
    fig = m.plot(pred)
    _ = add_changepoints_to_plot(fig.gca(), m, pred)
    plt.xlabel('Date [YYYY-MM-DD]')
    plt.ylabel('Total Cases')
    plt.title(
        'Knoxville Metro COVID19 Forecasted Daily New Cases -- Updated: {}'.
        format(time_now()))
    plt.annotate(attribution['text'], (0, 0), (0, -60),
                 xycoords='axes fraction',
                 textcoords='offset points',
                 fontsize=attribution['fsize'],
                 color=attribution['color'],
                 alpha=attribution['alpha'])
    plt.tight_layout()
    plt.savefig(os.path.join(imgdir, 'metro-cases-all-daily-forecasted.png'))
Example #8
0
def forecasting(cps,period,dataframe):
    from fbprophet.plot import add_changepoints_to_plot
    changed=Prophet(changepoint_prior_scale=cps)
    changed.fit(dataframe)
    future=changed.make_future_dataframe(periods=period)
    forecast=changed.predict(future)
    fig=changed.plot(forecast)
    p=add_changepoints_to_plot(fig.gca(),changed,forecast)
    return changed
def test2():
    """
        如果趋势的变化被过度拟合(即过于灵活)或者拟合不足(即灵活性不够),可以利用输入参数 changepoint_prior_scale
        来调整稀疏先验的程度。默认下,这个参数被指定为 0.05 。
        增加这个值,使趋势变化更加灵活。减少这个值会使灵活度降低
    """
    df = pd.read_csv('data/example_wp_log_peyton_manning.csv')

    # 拟合模型
    m = Prophet(changepoint_prior_scale=0.5)
    m.fit(df)

    future = m.make_future_dataframe(periods=365)

    forecast = m.predict(future)
    fig = m.plot(forecast)
    add_changepoints_to_plot(fig.gca(), m, forecast)
    plt.show()
Example #10
0
def evaluateModel(model, data, endDate=None, title=None):
    model.fit(data["training"])
    holdout_period = model.make_future_dataframe(periods=len(data["holdout"]),
                                                 include_history=False)
    holdout_forecast = model.predict(holdout_period)
    if endDate is None:
        periods = len(data["holdout"])
    else:
        periods = (endDate - data["training"].ds.max()).days
    all_period = model.make_future_dataframe(periods=periods,
                                             include_history=True)
    all_forecast = model.predict(all_period)
    text = '{}Holdout MAPE: {:,.2f}%'.format(
        "{} - ".format(title) if title is not None else "",
        calcMAPE(data["holdout"].y, holdout_forecast.yhat))
    plotHTML = plot(
        {
            "data": [
                go.Scatter(x=data["all"]['ds'], y=data["all"]['y'], name='y'),
                go.Scatter(
                    x=all_forecast['ds'], y=all_forecast['yhat'], name='yhat'),
                go.Scatter(x=all_forecast['ds'],
                           y=all_forecast['yhat_upper'],
                           fill='tonexty',
                           mode='none',
                           name='upper'),
                go.Scatter(x=all_forecast['ds'],
                           y=all_forecast['yhat_lower'],
                           fill='tonexty',
                           mode='none',
                           name='lower'),
            ],
            "layout":
            go.Layout(title=text)
        },
        output_type='div')
    plotProphet = model.plot(all_forecast)
    add_changepoints_to_plot(plotProphet.gca(), model, all_forecast)
    plotComponents = model.plot_components(all_forecast)
    return {
        "plot": plotHTML,
        "prophetplot": plotProphet,
        "plotcomponents": plotComponents
    }
def show_trend(ncov_df,
               name=None,
               variable="Confirmed",
               n_changepoints=2,
               future_perd=0,
               plot_n=False,
               **kwargs):
    """
    Show trend of log10(@variable) using fbprophet package.
    @ncov_df <pd.DataFrame>: the clean data
    @variable <str>: variable name to analyse
        - if Confirmed, use Infected + Recovered + Deaths
    @n_changepoints <int>: max number of change points
    @kwargs: keword arguments of select_area()
    """
    # Data arrangement
    df = select_area(ncov_df, **kwargs)
    try:
        df = df.loc[:, ["Date", variable]]
        df.columns = ["ds", "y"]
        # Log10(x)
        warnings.resetwarnings()
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            df["y"] = np.log10(df["y"]).replace([np.inf, -np.inf], 0)
        # fbprophet
        model = Prophet(growth="linear",
                        daily_seasonality=False,
                        n_changepoints=n_changepoints)
        model.fit(df)
        future = model.make_future_dataframe(periods=future_perd)
        forecast = model.predict(future)
        #     future1 = model.make_future_dataframe(periods=)
        #     forecast1 = model.predict(future1)
        # Create figure
        if (plot_n):
            fig = model.plot(forecast)
            _ = add_changepoints_to_plot(fig.gca(), model, forecast)
            if name is None:
                try:
                    name = f"{kwargs['places'][0][0]}: "
                except Exception:
                    name = str()
            else:
                name = f"{name}: "
            plt.title(f"{name}log10({variable}) over time and chainge points")
            plt.ylabel(f"log10(the number of cases)")
            plt.xlabel("")
        return ({
            'change_point': model.changepoints,
            'predict': forecast[['ds', 'yhat']]
        })
    except Exception:
        pass
        return (None)
Example #12
0
def plot_prophet_forecast(prophet_model, prophet_pred, save_dir=None):
    '''
    Plot Prophet model's forecast using the Prophet API, including changepoints
    :param prophet_model: Fitted Prophet model
    :param prophet_pred: A forecast from a Prophet model (result of a prophet.predict() call)
    '''

    fig = prophet_model.plot(prophet_pred)
    ax = fig.gca()
    add_changepoints_to_plot(ax, prophet_model, prophet_pred)
    ax = fig.gca()
    ax.set_xlabel('Date')
    ax.set_ylabel('Consumption [m^3]')
    fig.suptitle('Prophet Model Forecast', fontsize=15)
    fig.tight_layout(pad=2, rect=(0, 0, 1, 0.95))
    save_dir = cfg['PATHS'][
        'FORECAST_VISUALIZATIONS'] if save_dir is None else save_dir
    plt.savefig(save_dir + 'Prophet_API_forecast' +
                datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + '.png')
    return
def fbp_plot(forecast, model, changepoint=False):

    fig = model.plot(forecast,
                     xlabel='date',
                     ylabel='percent increase',
                     figsize=(8, 6))
    if changepoint:
        a = add_changepoints_to_plot(fig.gca(), model, forecast)
    plt.show()
    model.plot_components(forecast, figsize=(8, 6))
    plt.show()
def test1():
    """
    自动检测突变点
    """
    df = pd.read_csv('data/example_wp_log_peyton_manning.csv')
    m = Prophet(n_changepoints=5)
    m.fit(df)

    # 预测
    future = m.make_future_dataframe(periods=365)
    forecast = m.predict(future)

    # 获取显著突变点的位置
    # 默认情况下,只有在时间序列的前80%才会推断出突变点,以便有足够的长度来预测未来的趋势,并避免在时间序列的末尾出现过度拟合的波动。
    # 这个默认值可以在很多情况下工作,但不是所有情况下都可以,可以使用changepoint_range参数进行更改。
    # 例如,Python中的m = Prophet(changepoint_range=0.9)。这意味着将在时间序列的前90%处寻找潜在的变化点
    fig = m.plot(forecast)
    add_changepoints_to_plot(fig.gca(), m, forecast)

    plt.show()
Example #15
0
def plot_forecast(market, period, frequency):
    model = fit_model_kwargs(market)

    future = model.make_future_dataframe(periods=period, freq=frequency)
    forecast = model.predict(future)
    forecast[['yhat', 'yhat_lower']] = forecast[['yhat',
                                                 'yhat_lower']].clip(0, )

    fig = model.plot_components(forecast)
    fig = model.plot(forecast)
    a = add_changepoints_to_plot(fig.gca(), model, forecast)
def saveFig(name, yr):
    fig = m.plot(forecast)
    plt.xlabel("year")
    plt.ylabel("CAD")
    plt.title(name + " price forecast, weekly prices from " + str(yr) +
              " to 2023")
    plt.subplots_adjust(top=0.9)
    plt.savefig(resultPath + name + str(term) + "1.png", dpi=800)
    a = add_changepoints_to_plot(fig.gca(), m, forecast)
    fig = m.plot_components(forecast)
    plt.savefig(resultPath + name + str(term) + "2.png")
Example #17
0
 def prediction(self, period):
     self.model.fit(self.prophet_df)
     future = self.model.make_future_dataframe(periods=period)
     # Eliminate weekend from future dataframe
     future['day'] = future['ds'].dt.weekday
     future = future[future['day'] <= 4]
     forecast = self.model.predict(future)
     fig = plot_plotly(self.model, forecast)
     fig2 = self.model.plot_components(forecast)
     fig3 = self.model.plot(forecast)
     a = add_changepoints_to_plot(fig3.gca(), self.model, forecast)
     return (fig, fig2, fig3)
Example #18
0
    def plot(self, changepoints=True):
        """
        Plots actuals and forecasted values.

        :param changepoints: Bool, whether or not to add changepoints and trendline to plot.

        :return: Matplotlib Figure
        """
        fig = self.m.plot(self.f.reset_index())
        if changepoints:
            add_changepoints_to_plot(fig.gca(), self.m, self.f.reset_index())

        if self.acts is not None:
            self.acts.plot(fig=fig,
                           ls='',
                           marker='D',
                           mec='black',
                           mfc='lightgray',
                           markersize=3)

        return fig
Example #19
0
def model_prophet1(data):
    model = fbprophet.Prophet(changepoint_prior_scale=0.05,
                              daily_seasonality=True)
    # 定义模型
    model.fit(data)  # 训练模型
    forecast_df = model.make_future_dataframe(periods=365, freq='D')  # 生成需预测序列
    forecast = model.predict(forecast_df)  # 模型预测
    model.plot(forecast, xlabel='Date', ylabel='Close Price ¥')  # 绘制预测图
    plt.title('Close Price of 000001.SZ')
    plt.show()

    fig = model.plot(forecast)  # 绘制预测图
    a = add_changepoints_to_plot(fig.gca(), model, forecast)  # 增加变化点
Example #20
0
def corona_prdict():
    df_korea = pd.read_csv('corona.csv')
    df_prophet = df_korea[['date', 'patient']]
    # df_prophet = df_korea.rename(columns={
    #     'date': 'ds',
    #     'confirmed': 'y'
    # })
    df_prophet.columns = ['ds', 'y']

    # a = np.arange(0.8, 1.01, 0.1)
    # b = np.arange(0.1, 0.3, 0.1)
    #
    # params_grid = {'changepoint_range': a,
    #                'changepoint_prior_scale': b}
    # grid = ParameterGrid(params_grid)
    # for p in tqdm(grid):
    #     m = Prophet(**p,
    #                 yearly_seasonality=False,
    #                 weekly_seasonality=False,
    #                 daily_seasonality=True,
    #                 seasonality_mode='additive')
    #     m.fit(df_prophet)
    # print('optimum parameter loaded !!')

    m = Prophet(
        changepoint_prior_scale=Main.scale,  # 증가시 유연
        changepoint_range=0.90,  # 시계열의 첫 90%에 잠재적 변화점을 두다.
        yearly_seasonality=False,
        weekly_seasonality=False,
        daily_seasonality=True,
        seasonality_mode='additive')

    m.fit(df_prophet)

    future = m.make_future_dataframe(periods=7)  # 7일만 예상하겠다.
    forecast = m.predict(future)

    import matplotlib.font_manager as fm

    path = 'C:/Windows/Fonts/malgun.ttf'
    font_name = fm.FontProperties(fname=path, size=50).get_name()
    plt.rc('font', family=font_name)

    fig = m.plot(forecast, figsize=(6, 4), xlabel='날 짜', ylabel='감염자수(예상)')
    a = add_changepoints_to_plot(fig.gca(), m, forecast)
    #    fig2 = plot_plotly(m, forecast, xlabel='날 짜', ylabel='감염자수(예상)')
    # py.iplot(fig2, filename='corona19.html')
    date = time.strftime('%Y%m%d', time.localtime(time.time()))
    #    plotly.offline.plot(fig2, filename='corona_predict/corona19_{}.html'.format(date))
    #     plt.imshow(a)
    plt.savefig(Main.PATH + "{}.png".format(date))
Example #21
0
def get_custom_figures(filepath, txtFuture_periods, txtChangepoints,
                       txtChangepoint_range, txtChangepoint_scale,
                       ckbCountry_holidays, txtHolidays_scale,
                       ckbMonthly_seasonality, txtSeasonality_days,
                       txtFourier_monthly):
    df = pd.read_csv(filepath)
    df.columns = ['ds', 'y']
    custom_figures = {
        'n_changepoints': int(txtChangepoints),
        'changepoint_range': float(txtChangepoint_range),
        'changepoint_prior_scale': float(txtChangepoint_scale),
        'holidays_prior_scale': float(txtHolidays_scale),
    }
    if ckbCountry_holidays:
        ckbCountry = 'true'
    else:
        ckbCountry = 'false'
    if ckbMonthly_seasonality:
        ckbMonthly = 'true'
    else:
        ckbMonthly = 'false'
    messsage = 'Future periods = ' + txtFuture_periods + ' || n_changepoints = ' + txtChangepoints + ' || changepoint_range = ' + txtChangepoint_range + ' || changepoint_range = ' + txtChangepoint_scale + ' || add_country_holidays(VN) = ' + ckbCountry + ' || holidays_prior_scale = ' + txtHolidays_scale + ' || add_seasonality(monnthly) = ' + ckbMonthly + ' || Seasonality days = ' + txtSeasonality_days + ' || Fourier monthly = ' + txtFourier_monthly
    flash(messsage)
    m = Prophet(**custom_figures)
    if ckbCountry_holidays:
        m.add_country_holidays(country_name='VN')
    if ckbMonthly_seasonality:
        m.add_seasonality(name='monthly',
                          period=float(txtSeasonality_days),
                          fourier_order=int(txtFourier_monthly))
    m.fit(df)
    future = m.make_future_dataframe(periods=int(txtFuture_periods))
    forecast = m.predict(future)
    forecast_fig = m.plot(forecast)
    add_changepoints_to_plot(forecast_fig.gca(), m, forecast)
    components_fig = m.plot_components(forecast)
    return forecast_fig, components_fig
def prophet_analysis(df,split,freq,changepoints=3):
    train = df.iloc[:split]
    test = df.iloc[split:]

    # m_eval = Prophet(growth='linear')
    m_eval = Prophet(
        growth='linear',
        n_changepoints=changepoints,
        changepoint_range=0.8,
        yearly_seasonality=False,
        weekly_seasonality=False,
        daily_seasonality=False,
        seasonality_mode='additive',
        seasonality_prior_scale=20,
        changepoint_prior_scale=.5,
        mcmc_samples=0,
        interval_width=0.8,
        uncertainty_samples=500,
        ).add_seasonality(
            name='monthly',
            period=30.5,
            fourier_order=5
        ).add_seasonality(
            name='yearly',
            period=365.25,
            fourier_order=20
        ).add_seasonality(
            name='quarterly',
            period=365.25/4,
            fourier_order=5,
            prior_scale=15)
    m_eval.fit(train)
    eval_future=m_eval.make_future_dataframe(periods=test.shape[0],freq=freq)
    eval_forecast=m_eval.predict(eval_future)

    fig,axs=plt.subplots(1,1,figsize=(15,4))
    ax1 = sns.lineplot(x='ds',y='yhat',data=eval_forecast,label='Predictions',legend='full')
    ax1 = sns.lineplot(x='ds',y='y',data=train,label='Train True',legend='full',linestyle='-.')
    ax1 = sns.lineplot(x='ds',y='y',data=test,label='Test True',legend='full')

    ax =m_eval.plot(eval_forecast)
    ax = add_changepoints_to_plot(fig.gca(),m_eval,eval_forecast)

    predictions = eval_forecast.iloc[-test.shape[0]:]['yhat'] #grab predictions to compare with test set
    print('MAPE = ' + str((abs(np.array(test.y)-predictions)/(np.array(test.y))).mean()))
    print('RMSE = ' + str(rmse(predictions,test['y'])))
    print('MEAN = ' + str(df.y.mean()))
    return
Example #23
0
    def sav_figure(name, c_prophet, c_forecast, ca_cv=None):
        fig = c_prophet.plot(c_forecast, xlabel='Date', ylabel='CPU Secs')
        a = add_changepoints_to_plot(fig.gca(), c_prophet, c_forecast)
        plt.title('CPU Seconds (hourly)')
        plt.savefig(IMG_DIR + name + '_forecast.png')
        #plt.show()

        figC = c_prophet.plot_components(c_forecast)
        plt.savefig(IMG_DIR + name + '_forecastComp.png')
        #plt.show()

        if ca_cv:
            figV = plot_cross_validation_metric(ca_cv, metric='mape')
            plt.title(
                'CPU Seconds Forecast MAPE (mean absolute percent error)')
            plt.savefig(IMG_DIR + name + '_forecastCV.png')
    def plot_forecast(self):
        """Plot forecasted values"""
        if self.residuals_forecast is not None:
            fig, axes = super(ProphetForecaster, self)._plot_forecast(y=np.asarray(self._train_dt['y']),
                                                                      yhat=np.asarray(self.fittedvalues['yhat']),
                                                                      forecast=pd.Series(
                                                                          np.asarray(self.forecast['yhat']),
                                                                          index=self.forecast['ds']), _id='Prophet')
        else:
            fig_forecast = self._model.plot(self.forecast)
            fig_components = self._model.plot_components(self.forecast)
            if self._add_change_points:
                a = add_changepoints_to_plot(fig_forecast.gca(), self._model, self.forecast)

        plt.gcf().autofmt_xdate()
        plt.grid(True)
        plt.show()
Example #25
0
def show_trend(ncov_df,
               variable="Confirmed",
               n_changepoints=2,
               places=None,
               excluded_places=None):
    """
    Show trend of log10(@variable) using fbprophet package.
    @ncov_df <pd.DataFrame>: the clean data
    @variable <str>: variable name to analyse
        - if Confirmed, use Infected + Recovered + Deaths
    @n_changepoints <int>: max number of change points
    @places <list[tuple(<str/None>, <str/None>)]: the list of places
        - if the list is None, all data will be used
        - (str, str): both of country and province are specified
        - (str, None): only country is specified
        - (None, str) or (None, None): Error
    @excluded_places <list[tuple(<str/None>, <str/None>)]: the list of excluded places
        - if the list is None, all data in the "places" will be used
        - (str, str): both of country and province are specified
        - (str, None): only country is specified
        - (None, str) or (None, None): Error
    """
    # Data arrangement
    df = select_area(ncov_df, places=places, excluded_places=excluded_places)
    if variable == "Confirmed":
        df["Confirmed"] = df[["Infected", "Recovered", "Deaths"]].sum(axis=1)
    df = df.loc[:, ["Date", variable]]
    df.columns = ["ds", "y"]
    # Log10(x)
    warnings.resetwarnings()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        df["y"] = np.log10(df["y"]).replace([np.inf, -np.inf], 0)
    # fbprophet
    model = Prophet(growth="linear",
                    daily_seasonality=False,
                    n_changepoints=n_changepoints)
    model.fit(df)
    future = model.make_future_dataframe(periods=0)
    forecast = model.predict(future)
    # Create figure
    fig = model.plot(forecast)
    _ = add_changepoints_to_plot(fig.gca(), model, forecast)
    plt.title(f"log10({variable}) over time and chainge points")
    plt.ylabel(f"log10(the number of cases)")
    plt.xlabel("")
Example #26
0
def plotter(train = None, title = "", ylabel = "", fn = "test", plot = True, add_chpts = False, add_cv = True):
#figure
    fig = plt.figure(facecolor='w', figsize=(12, 7))
    ax = fig.add_subplot(111)
    plt.title(title, fontsize=18, y = 1.05)

    ### train result
    train_fig = train.model.plot(train.forecast,  ax = ax, xlabel = "Date", ylabel = ylabel)
    if add_chpts:
        cpts = add_changepoints_to_plot(train_fig.gca(), train.model, train.forecast)
    ax = plt.gca(); ax.yaxis.set_major_formatter(FuncFormatter(xt.y_fmt))
    c_txt = "RMSE=%.1f MAPE=%.1f%%"%(train.param.rmse, train.cv_metrics.manual_mape)

    #format date
    myFmt = mdates.DateFormatter('%d-%m')
    ax.xaxis.set_major_formatter(myFmt)

    #ticks -  locator puts ticks at regular intervals
    xloc = plticker.MultipleLocator(base=3.0) 
    ax.xaxis.set_major_locator(xloc)

    ## Rotate date labels automatically
    fig.autofmt_xdate()

    
    xt.save(train_fig, xt.name(odir, fn+"_prediction" + ("_changepoints" if add_chpts else "") ), c_txt)

    ### components
    train_comp_fig = train.model.plot_components(train.forecast)
    ax = plt.gca(); ax.yaxis.set_major_formatter(FuncFormatter(xt.y_fmt))
    xt.save(train_comp_fig, xt.name(odir, fn+"_components"))
    
    ### MAPE
    if add_cv:
        train_cv_mape = train.cv_mape_fig()
        ax = plt.gca()
        ax.yaxis.set_major_formatter(FuncFormatter(xt.y_fmt))
        xt.save(train_cv_mape, xt.name(odir, fn+"_cv_mape"))

    
    plt.tight_layout()
    if plot: plt.show()

    plt.close('all')
Example #27
0
def main():
    df = pd.read_csv('./data/example_wp_log_peyton_manning.csv')
    m = Prophet()
    m.fit(df)
    future = m.make_future_dataframe(periods=366)
    forecast = m.predict(future)
    fig = m.plot(forecast)
    for cp in m.changepoints:
        print(cp)
        plt.axvline(cp, c='gray', ls='--', lw=2)
    plt.show()

    deltas = m.params['delta'].mean(0)
    fig = plt.figure(facecolor='w', figsize=(10, 6))
    ax = fig.add_subplot(111)
    ax.bar(range(len(deltas)),
           deltas,
           facecolor='#0072B2',
           edgecolor='#0072B2')
    ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    ax.set_ylabel('Rate change')
    ax.set_xlabel('Potential changepoint')
    fig.tight_layout()
    fig.show()

    fig = m.plot(forecast)
    a = add_changepoints_to_plot(fig.gca(), m, forecast)
    fig.show()

    m = Prophet(changepoint_prior_scale=0.5)
    forecast = m.fit(df).predict(future)
    fig = m.plot(forecast)
    fig.show()

    m = Prophet(changepoint_prior_scale=0.001)
    forecast = m.fit(df).predict(future)
    fig = m.plot(forecast)
    fig.show()

    m = Prophet(changepoints=['2014-01-01'])
    forecast = m.fit(df).predict(future)
    fig = m.plot(forecast)
    fig.show()
Example #28
0
def test_change_point():
    df = pd.read_csv(prophet_path + os.sep + 'example_wp_log_R.csv')

    # 默认 first 80% of the time series in order to have plenty
    # m = Prophet()
    # m = Prophet(changepoint_range=0.9)
    # flexibility 的调整?
    # m = Prophet(changepoint_prior_scale=0.005)
    # Specifying the locations of the changepoints
    m = Prophet(changepoints=['2014-01-01'])

    m.fit(df)

    future = m.make_future_dataframe(periods=365)
    # future.tail()

    forecast = m.predict(future)
    fig = m.plot(forecast)
    a = add_changepoints_to_plot(fig.gca(), m, forecast)
coin = 'bitcoin'

startDate = 20160201
endDate = 20200430

tables = pd.read_html('https://coinmarketcap.com/currencies/' + coin +
                      '/historical-data/?start=' + str(startDate) + '&end=' +
                      str(endDate))
#print(tables[0])

#reverse dataframe
table = tables[2].iloc[::-1]
#clean dataframe
df = table[['Date', 'Close**']].copy()
#normalize data
df = df.rename(index=str, columns={"Date": "ds", "Close**": "y"})
print(df)

#model
m = Prophet(seasonality_mode='multiplicative')
#m.add_seasonality('self_define_cycle',period=8,fourier_order=8,mode='additive')
m.fit(df)

future = m.make_future_dataframe(periods=365, freq='D')
fcst = m.predict(future)
fig = m.plot(fcst, xlabel='Date', ylabel='Closing value', uncertainty=True)
a = add_changepoints_to_plot(fig.gca(), m, fcst)

plt.title('Bitcoin forecast')
plt.show()
'''taking a 30 days future prediction with current model'''
future_global=model.make_future_dataframe(periods=30,freq='D')
# print(future_global.head())
# print(future_global.shape)

'''predicting the future 30days data'''
prediction = model.predict(future_global)
# print(prediction)
print(prediction[['ds','yhat','yhat_lower','yhat_upper']].tail())

'''plotting the predicted model graph(make as exponential)'''
model.plot(prediction)
model.plot_components(prediction)

fig=model.plot(prediction)
a=add_changepoints_to_plot(fig.gca(),model,prediction)

'''checking the cross validation of the predicted model'''
df_cv=cross_validation(model,horizon='30 days',period='15 days',initial='90 days')
# print(df_cv.head())
# print(df_cv.shape)

'''checking the performance metrics of the cross validated predicted model'''
df_performance = performance_metrics(df_cv)
# print(df_performance.head())

df_performance = plot_cross_validation_metric(df_cv,metric='rmse')
df_performance = plot_cross_validation_metric(df_cv,metric='mse')
df_performance = plot_cross_validation_metric(df_cv,metric='mae')
df_performance = plot_cross_validation_metric(df_cv,metric='mape')
df_performance = plot_cross_validation_metric(df_cv,metric='mdape')