EDA プロット II
もう一つ考えられるのは、乗車料金は一日の中で変化するかもしれないという点です。
目標は、各時間帯の運賃の中央値を、シンプルな折れ線グラフとして描くことです。hour 特徴量はすでに計算されています。日付関連の特徴量の扱いがわからなくても心配いりません。これは後の Feature Engineering の章で扱います。
この演習はコースの一部です
Pythonで挑むKaggleコンペティション
演習の手順
trainDataFrame を"hour"でグループ化し、"fare_amount"列の中央値を計算します。- 得られた
hour_priceDataFrame を使い、x 軸に"hour"、y 軸に"fare_amount"を置いた折れ線を描画します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create hour feature
train['pickup_datetime'] = pd.to_datetime(train.pickup_datetime)
train['hour'] = train.pickup_datetime.dt.hour
# Find median fare_amount for each hour
hour_price = train.____('____', as_index=False)['____'].____()
# Plot the line plot
plt.plot(hour_price[____], hour_price[____], marker='o')
plt.xlabel('Hour of the day')
plt.ylabel('Median fare amount')
plt.title('Fare amount based on day time')
plt.xticks(range(24))
plt.show()