始める無料で始める

欠損値の補完

データに欠損があるとき、どのように埋めればよいでしょうか?

この演習では、いくつかの補間手法を使って欠損値を補完し、そのたびに結果を可視化します。まずは、欠損データ点を補間してプロットするために使う関数(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()
コードを編集して実行