Transforming raw data
In the last chapter, you calculated the rolling mean. In this exercise, you will define a function that calculates the percent change of the latest data point from the mean of a window of previous data points. This function will help you calculate the percent change over a rolling window.
This is a more stable kind of time series that is often useful in machine learning.
Den här övningen är en del av kursen
Machine Learning for Time Series Data in Python
Övningsinstruktioner
- Define a
percent_changefunction that takes an input time series and does the following:- Extract all but the last value of the input series (assigned to
previous_values) and the only the last value of the timeseries ( assigned tolast_value) - Calculate the percentage difference between the last value and the mean of earlier values.
- Extract all but the last value of the input series (assigned to
- Using a rolling window of 20, apply this function to
prices, and visualize it using the given code.
Interaktiv övning med praktiskt arbete
Testa den här övningen genom att slutföra den här exempelkoden.
# Your custom function
def percent_change(series):
# Collect all *but* the last value of this window, then the final value
previous_values = series[:____]
last_value = series[-1]
# Calculate the % difference between the last value and the mean of earlier values
percent_change = (____ - np.mean(previous_values)) / np.mean(previous_values)
return percent_change
# Apply your custom function and plot
prices_perc = prices.rolling(20).____
prices_perc.loc["2014":"2015"].plot()
plt.show()