Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Machine Learning for Time Series Data in Python

Cursus bekijken

Oefeninstructies

  • Use a dictionary comprehension to create multiple time-shifted versions of prices_perc using the lags specified in shifts.
  • Convert the result into a DataFrame.
  • Use the given code to visualize the results.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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()
Code bewerken en uitvoeren