Percentiles and partial functions
In this exercise, you'll practice how to pre-choose arguments of a function so that you can pre-configure how it runs. You'll use this to calculate
several percentiles of your data using the same percentile() function in numpy.
This exercise is part of the course
Machine Learning for Time Series Data in Python
Exercise instructions
- Import
partialfromfunctools. - Use the
partial()function to create several feature generators that calculate percentiles of your data using a list comprehension. - Using the rolling window (
prices_perc_rolling) we defined for you, calculate the quantiles usingpercentile_functions. - Visualize the results using the code given to you.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import partial from functools
____
percentiles = [1, 10, 25, 50, 75, 90, 99]
# Use a list comprehension to create a partial function for each quantile
percentile_functions = [____(np.percentile, q=percentile) for percentile in percentiles]
# Calculate each of these quantiles on the data using a rolling window
prices_perc_rolling = prices_perc.rolling(20, min_periods=5, closed='right')
features_percentiles = prices_perc_rolling.____(____)
# Plot a subset of the result
ax = features_percentiles.loc[:"2011-01"].plot(cmap=plt.cm.viridis)
ax.legend(percentiles, loc=(1.01, .5))
plt.show()