外れ値の扱い
この演習では、外れ値(ほかのデータと大きく異なるため、いわゆる「通常の」データとは異なる扱いをするデータ点)を処理します。前の演習で求めた(時間に対する)パーセント変化を用いて外れ値を検出します。まずは、時系列全体の中央値で外れ値を置き換える関数を作成します。
この演習はコースの一部です
Pythonで学ぶMachine Learningによる時系列データ解析
演習の手順
- 入力の Series を受け取り、次を行う関数を定義してください。
- 各データ点について系列の平均からの距離の絶対値を計算し、平均から標準偏差の3倍を超えるデータ点のブールマスクを作成します。
- このブールマスクを使って、外れ値を系列全体の中央値に置き換えます。
- この関数をデータに適用し、与えられたコードで結果を可視化してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
def replace_outliers(series):
# Calculate the absolute difference of each timepoint from the series mean
absolute_differences_from_mean = np.abs(series - np.mean(series))
# Calculate a mask for the differences that are > 3 standard deviations from zero
this_mask = absolute_differences_from_mean > (np.____(series) * ____)
# Replace these values with the median accross the data
series[this_mask] = np.____(series)
return series
# Apply your preprocessing function to the timeseries and plot the results
prices_perc = prices_perc.____
prices_perc.loc["2014":"2015"].plot()
plt.show()