開始使用免費開始

填補遺漏值

當資料點有遺漏時,要如何把它們補起來?

在這個練習中,你會用不同的插值方法來填補一些遺漏值,並在每次填補後視覺化結果。不過在開始之前,你會先建立一個函式(interpolate_and_plot()),用來對遺漏的資料點進行插值並繪圖。

一個單一的時間序列已經載入到名為 prices 的 DataFrame 中。

本練習屬於課程

Python 的時間序列資料機器學習

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼