LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Machine Learning for Time Series Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Import partial from functools.
  • 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 using percentile_functions.
  • Visualize the results using the code given to you.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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()
Code bearbeiten und ausführen