Creating time-shifted features
In machine learning for time series, it's common to use information about previous time points to predict a subsequent time point.
In this exercise, you'll "shift" your raw data and visualize the results. You'll use the percent change time series that you calculated in the previous chapter, this time with a very short window. A short window is important because, in a real-world scenario, you want to predict the day-to-day fluctuations of a time series, not its change over a longer window of time.
This exercise is part of the course
Machine Learning for Time Series Data in Python
Exercise instructions
- Use a dictionary comprehension to create multiple time-shifted versions of
prices_perc
using the lags specified inshifts
. - Convert the result into a DataFrame.
- Use the given code to visualize the results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# These are the "time lags"
shifts = np.arange(1, 11).astype(int)
# Use a dictionary comprehension to create name: value pairs, one pair per shift
shifted_data = {"lag_{}_day".format(day_shift): prices_perc.____(____) for day_shift in shifts}
# Convert into a DataFrame for subsequent use
prices_perc_shifted = ____(shifted_data)
# Plot the first 100 samples of each
ax = prices_perc_shifted.iloc[:100].plot(cmap=plt.cm.viridis)
prices_perc.iloc[:100].plot(color='r', lw=2)
ax.legend(loc='best')
plt.show()