Get startedGet started for free

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.

This exercise is part of the course

Machine Learning for Time Series Data in Python

View Course

Exercise instructions

  • Define a percent_change function 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 to last_value)
    • Calculate the percentage difference between the last value and the mean of earlier values.
  • Using a rolling window of 20, apply this function to prices, and visualize it using the given code.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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()
Edit and Run Code