Challenge
Oppgave
Swipe to start coding
Create an autoregressive model to predict the dataset aapl.csv
. After, print the results and the model error.
- Read the
aapl.csv
dataset. - Create an autoregressive model (
AutoReg
) with 3 lags for theX
data and assign it to themodel
variable. - Fit model to the data and assign it to the
model_fit
variable. - Predict the first 30 values.
- Visualize the results: display the first 30 values of
X
within the first call of theprint()
function, and first 30 values of thepredictions
within the second call. - Calculate the RMSE (square root of the mean squared error) and display it.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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?
Takk for tilbakemeldingene dine!
Seksjon 4. Kapittel 5
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår