시작하기무료로 시작하기

결측값 보간하기

데이터에 결측 지점이 있을 때, 어떻게 채울 수 있을까요?

이 연습 문제에서는 서로 다른 보간 방법으로 결측값을 채우고, 매번 결과를 시각화해 보겠습니다. 먼저, 결측 지점을 보간하고 그 결과를 플로팅하는 함수(interpolate_and_plot())를 만듭니다.

단일 시계열이 prices라는 DataFrame에 로드되어 있습니다.

이 연습은 강의의 일부입니다

Python으로 배우는 시계열 데이터 Machine Learning

강의 보기

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Create a function we'll use to interpolate and plot
def interpolate_and_plot(prices, interpolation):

    # Create a boolean mask for missing values
    missing_values = prices.____()

    # Interpolate the missing values
    prices_interp = prices.____(interpolation)

    # Plot the results, highlighting the interpolated values in black
    fig, ax = plt.subplots(figsize=(10, 5))
    prices_interp.plot(color='k', alpha=.6, ax=ax, legend=False)
    
    # Now plot the interpolated values on top in red
    prices_interp[missing_values].plot(ax=ax, color='r', lw=3, legend=False)
    plt.show()
코드 편집 및 실행