În noua versiune (v0.18.0
) din ahead
pachet, am extins forecast::thetaf
Funcție pentru a sprijini modelele liniare generalizate (GLMS) și a adăugat un mecanism de atenție.
Atenția este utilizată pe scară largă în rețelele neuronale curente (pentru că tind să uite; învinovăți -o pe gradienți 🙂) pentru a se concentra pe anumite părți ale datelor de intrare atunci când se fac predicții.
În acest caz, ajută modelul să învețe care părți ale seriei de timp sunt mai importante pentru prognoză, folosind medii ponderate ale observațiilor trecute.
Mai multe despre asta mai târziu într -o hârtie. La sfârșitul acestui post este prezentat un link către un caiet care conține Python și R.
Începeți cu:
options(repos = c( techtonique = "https://r-packages.techtonique.net", CRAN = "https://cloud.r-project.org" )) install.packages("ahead")
library(forecast) library(ahead) # glm.nb par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=MASS::glm.nb, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=MASS::glm.nb, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # glm par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=stats::glm, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=stats::glm, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # rlm par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=MASS::rlm, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=MASS::rlm, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # lqs par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=MASS::lqs, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=MASS::lqs, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # lm par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=stats::lm, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=stats::lm, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # gam par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=gam::gam, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=gam::gam, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # rq par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=quantreg::rq, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(USAccDeaths, h=25L, fit_func=quantreg::rq, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention")
# glm.nb par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=MASS::glm.nb, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=MASS::glm.nb, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # glm par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=stats::glm, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=stats::glm, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # rlm par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=MASS::rlm, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=MASS::rlm, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # lm par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=stats::lm, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=stats::lm, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # lqs par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=MASS::lqs, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=MASS::lqs, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # gam par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=gam::gam, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=gam::gam, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention") # rq par(mfrow=c(2,1)) obj1 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=quantreg::rq, attention = TRUE, type_pi = "conformal-split", method = "adj")) plot(obj1, main="With attention") obj2 <- suppressWarnings(ahead::glmthetaf(AirPassengers, h=25L, fit_func=quantreg::rq, attention = FALSE, type_pi = "conformal-split", method = "adj")) plot(obj2, main="Without attention")
from rpy2.robjects.packages import importr from rpy2.robjects import r import rpy2.robjects as ro import numpy as np import matplotlib.pyplot as plt from rpy2.robjects import pandas2ri # Import required R packages ahead = importr('ahead') mass = importr('MASS') base = importr('base') stats = importr('stats') # Get the data and fit the model with localconverter(ro.default_converter): # Get AirPassengers data data = r('USAccDeaths') data = np.array(data) # Fit the model fit = r(''' suppressWarnings( ahead::glmthetaf( USAccDeaths, h=25L, fit_func=MASS::glm.nb, attention=TRUE, type_pi="conformal-split", method="adj" ) ) ''') # Extract predictions and intervals forecasts = np.array(fit.rx2('mean')) lower = np.array(fit.rx2('lower')) upper = np.array(fit.rx2('upper')) # Create time indices time_train = np.arange(len(data)) time_test = np.arange(len(data), len(data) + len(forecasts)) # Create the plot plt.figure(figsize=(12, 6)) # Plot training data plt.plot(time_train, data, 'b-', label="Observed", alpha=0.7) # Plot forecasts and prediction intervals plt.plot(time_test, forecasts, 'r--', label="Forecast") plt.fill_between(time_test, lower, upper, color="r", alpha=0.2, label="95% Prediction Interval") # Customize the plot plt.title('USAccDeaths Forecast with Attention') plt.xlabel('Time') plt.ylabel('-') plt.legend() plt.grid(True, alpha=0.3) # Show the plot plt.tight_layout() plt.show()