백분위수와 partial 함수
이 연습 문제에서는 함수의 인자를 미리 지정해, 실행 방식을 사전에 구성하는 방법을 연습해 봅니다. 이를 활용해 numpy의 동일한 percentile() 함수를 사용해 데이터의 여러 백분위수를 계산할 거예요.
이 연습은 강의의 일부입니다
Python으로 배우는 시계열 데이터 Machine Learning
연습 안내
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()