python-3.x 调用封装类的绘图方法无法输出结果

irtuqstp  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(140)

我尝试将ThymeBoost测试代码(basic examples)转换为名为TimeSeriesPredictor的Python类,如下所示:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from ThymeBoost import ThymeBoost as tb

class TimeSeriesPredictor:
    def __init__(self, y, seasonal_period=25, cost_penalty=.001, n_split_proposals=25, verbose=1):
        self.y = y
        self.seasonal_period = seasonal_period
        self.cost_penalty = cost_penalty
        self.n_split_proposals = n_split_proposals
        self.verbose = verbose
        self.boosted_model = None
        self.output = None
        self.predicted_output = None

    def fit_model(self, trend_estimator='linear', seasonal_estimator='fourier', split_cost='mse', global_cost='maicc', fit_type='local'):
        self.boosted_model = tb.ThymeBoost(
                            approximate_splits=True,
                            n_split_proposals=self.n_split_proposals,
                            verbose=self.verbose,
                            cost_penalty=self.cost_penalty,
                            )

        self.output = self.boosted_model.fit(self.y,
                           trend_estimator=trend_estimator,
                           seasonal_estimator=seasonal_estimator,
                           seasonal_period=self.seasonal_period,
                           split_cost=split_cost,
                           global_cost=global_cost,
                           fit_type=fit_type)

    def predict(self, n_steps):
        self.predicted_output = self.boosted_model.predict(self.output, n_steps)

    def plot_results(self):
        plt.plot(self.y)
        plt.plot(self.predicted_output)
        plt.show()

    def plot_components(self):
        self.boosted_model.plot_components(self.output)
        plt.show()

if __name__ == '__main__':

    #Here we will just create a random series with seasonality and a slight trend
    seasonality = ((np.cos(np.arange(1, 101))*10 + 50))
    np.random.seed(100)
    true = np.linspace(-1, 1, 100)
    noise = np.random.normal(0, 1, 100)
    y = true + noise + seasonality
    y = np.append(y, true + noise + seasonality)

    predictor = TimeSeriesPredictor(y)
    predictor.fit_model()
    predictor.predict(100)
    predictor.plot_results()
    predictor.plot_components()

但它只能打印出以下内容,而不能输出数字。如何改进此代码以解决此问题?谢谢
输出:

********** Round 1 **********
Using Split: None
Fitting initial trend globally with trend model:
median()
seasonal model:
fourier(10, False)
cost: 178.29215636637585
********** Round 2 **********
Using Split: 196
Fitting local with trend model:
linear((1, None))
seasonal model:
fourier(10, False)
cost: 170.15461510349348
********** Round 3 **********
Using Split: 158
Fitting local with trend model:
linear((1, None))
seasonal model:
fourier(10, False)
cost: 168.69710853512558
********** Round 4 **********
Using Split: 58
Fitting local with trend model:
linear((1, None))
seasonal model:
fourier(10, False)
cost: 168.3484150897301
==============================
Boosting Terminated 
Using round 4
aelbi1ox

aelbi1ox1#

你在WSL吗?如果是这样,请尝试从jupyter笔记本运行此代码。

相关问题