查看sktime + sklearn 以执行预测:您将能够使用它们执行大部分时间序列分析。示例,根据我的要点,展示如何将模型组合成两个模型来预测趋势
from pytrends.request import TrendReq
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.neighbors import KNeighborsRegressor
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import EnsembleForecaster, ReducedForecaster
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.performance_metrics.forecasting import smape_loss
from sktime.utils.plotting import plot_series
# fetch cyberbullying data from Google trends
pytrend = TrendReq(hl="en-US")
pytrend.build_payload(
kw_list=[
"cyberbullying",
]
)
cyberbullying_df = pytrend.interest_over_time()
# transfrom DataFrame to Uni-Series of period
fow = cyberbullying_df["cyberbullying"].to_period(freq="W")
y_train, y_test = temporal_train_test_split(fow, test_size=36)
fh = ForecastingHorizon(y_test.index, is_relative=False)
# forecaster ensemble of knn and gradient boosting regressor
forecaster = EnsembleForecaster(
[
(
"knn",
ReducedForecaster(
regressor=KNeighborsRegressor(n_neighbors=1),
window_length=52,
strategy="recursive",
scitype="regressor",
),
),
(
"gboost",
ReducedForecaster(
regressor=GradientBoostingRegressor(n_estimators=100, random_state=42),
window_length=52,
strategy="recursive",
scitype="regressor",
),
),
]
)
# train an ensemble forecasters and predict|forecast
forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
sktimes 还允许您使用 Facebook 的 prophet。试试看,因为它是我进行时间序列分析的工具:sktime