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

book
Challenge

Tâche

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.

Solution

# 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)

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 5
single

single

# 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)

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt