EDA 圖表 I
在產生了幾個基本統計量之後,現在該提出並驗證一些關於資料相依性的想法。計程車競賽的 train DataFrame 已經在你的工作環境中可用。
先從一張散佈圖開始,畫出車資與乘車距離之間的關係。直覺上,距離越長,價格越高。
若要取得兩個地理座標之間的公里距離,你會使用 Haversine distance。其計算已由替你定義的 haversine_distance() 函式提供。這個函式預期輸入為 train DataFrame。
本練習屬於課程
用 Python 拿下 Kaggle 競賽
練習說明
- 建立新變數「distance_km」,其值為上車與下車點之間的 Haversine distance。
- 繪製散佈圖,x 軸放「fareamount」,y 軸放「distancekm」。要繪製散佈圖,請使用 matplotlib 的
scatter()方法。 - 將乘車距離的範圍限制在 0 到 50 公里之間,以避免繪出離群值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate the ride distance
train['distance_km'] = ____(train)
# Draw a scatterplot
plt.____(x=____[____], y=____[____], alpha=0.5)
plt.xlabel('Fare amount')
plt.ylabel('Distance, km')
plt.title('Fare amount based on the distance')
# Limit on the distance
plt.ylim(0, ____)
plt.show()