Challenge 1
Uppgift
Swipe to start coding
Time for new challenges! Here is the first challenge, the idea of which is to process the pr_HH Spot Price.csv
dataset to turn it from non-stationary to stationary:
- Read the dataset.
- Test for data stationarity (use
adfuller
) and display results. - Visualize the initial values of the
"Price"
column. - Transform data (the
"Price"
column of thedf
DataFrame) from non-stationary to stationary using the difference method (using the.diff()
method withperiods = 1
parameter). Drop NA values. Assign the result to thenew_diff
variable. - Visualize the modified data (
new_diff
). - Rerun the ADF test for updated data (
new_diff
).
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
27
28
# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
# Reading dataset
df = pd.read_csv("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/pr_HH+Spot+Price.csv")
# Make ADF Test
result = adfuller(df['Price'].values, autolag="AIC")
print("ADF Statistic: %f" % (result[0]))
print("p-value: %f" % (result[1]))
# Visualizing dataset
plt.plot(df["Price"])
plt.show()
# Use differencing method
new_diff = df["Price"].diff(periods=1).dropna()
# Plot stationary data
plt.plot(new_diff)
plt.show()
# Remake ADF test
new_result = adfuller(new_diff.values, autolag="AIC")
print("ADF Statistic: %f" % (new_result[0]))
print("p-value: %f" % (new_result[1]))
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 5. Kapitel 3
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
27
28
# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
# Reading dataset
df = pd.___("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/pr_HH+Spot+Price.csv")
# Make ADF Test
result = ___(df["___"].values, autolag="AIC")
print("ADF Statistic: %f" % (result[0]))
print("p-value: %f" % (result[1]))
# Visualizing dataset
plt.___(___)
plt.___()
# Use differencing method
new_diff = df["___"].___(periods=___).___()
# Plot stationary data
plt.plot(___)
plt.show()
# Remake ADF test
new_result = adfuller(___.values, autolag="AIC")
print("ADF Statistic: %f" % (new_result[0]))
print("p-value: %f" % (new_result[1]))
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal