开始使用免费开始使用

百分位数与偏函数

在本练习中,您将练习如何预先指定函数的部分参数,从而预先配置其运行方式。您将用这种方法,通过同一个 numpy 中的 percentile() 函数计算数据的多个百分位数。

本练习是课程的一部分

Python 中的时间序列机器学习

查看课程

练习说明

  • functools 导入 partial
  • 使用 partial() 函数配合列表推导式,创建若干特征生成器,用于计算数据的各个百分位数。
  • 使用我们为您定义的滚动窗口(prices_perc_rolling),结合 percentile_functions 计算分位数。
  • 使用提供给您的代码可视化结果。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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()
编辑并运行代码