Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge | Stationary Models
Time Series Analysis

book
Challenge

Oppgave

Swipe to start coding

Create an autoregressive model to predict the dataset aapl.csv. After, print the results and the model error.

  1. Read the aapl.csv dataset.
  2. Create an autoregressive model (AutoReg) with 3 lags for the X data and assign it to the model variable.
  3. Fit model to the data and assign it to the model_fit variable.
  4. Predict the first 30 values.
  5. Visualize the results: display the first 30 values of X within the first call of the print() function, and first 30 values of the predictions within the second call.
  6. Calculate the RMSE (square root of the mean squared error) and display it.

Løsning

# Importing libraries
from statsmodels.tsa.ar_model import AutoReg
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from math import sqrt
import pandas as pd

# Read dataset and use "Open" column as X
df = pd.read_csv("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/aapl.csv")
X = df["Open"].values

# Train autoregression (use lags=3)
model = AutoReg(X, lags=3, old_names=False)
model_fit = model.fit()

# Make predictions
predictions = model_fit.predict(start=0, end=30, dynamic=False)

# Visualize results
plt.plot(X[:30])
plt.plot(predictions[:30], color="red")
plt.show()

# Calculate MSE error
rmse = sqrt(mean_squared_error(X[:28], predictions))
print("Test RMSE: %.3f" % rmse)

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 5
# Importing libraries
from statsmodels.tsa.ar_model import AutoReg
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from math import sqrt
import pandas as pd

# Read dataset and use "Open" column as X
df = pd.___("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/aapl.csv")
X = df["Open"].values

# Train autoregression (use lags=3)
model = ___(___, lags=___, old_names=False)
model_fit = ___.___()

# Make predictions
predictions = model_fit.___(start=___, end=___, dynamic=False)

# Visualize results
plt.plot(___[:30])
plt.plot(___[:30], color="red")
plt.show()

# Calculate MSE error
rmse = ___(___(X[:28], predictions))
print("Test RMSE: %.3f" % rmse)

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt