Pythonで挑むKaggleコンペティション
Yauhen Babakhin
Kaggle Grandmaster


# データ読込
taxi_train = pd.read_csv('taxi_train.csv')
taxi_test = pd.read_csv('taxi_test.csv')
from sklearn.model_selection import train_test_split
# ローカル検証を作成
validation_train, validation_test = train_test_split(taxi_train,
test_size=0.3,
random_state=123)
import numpy as np # すべてのテスト観測に平均運賃を割り当て taxi_test['fare_amount'] = np.mean(taxi_train.fare_amount)# 予測結果を書き出し taxi_test[['id','fare_amount']].to_csv('mean_sub.csv', index=False)
| 検証RMSE | 公開LB RMSE | 公開LB順位 |
|---|---|---|
| 9.986 | 9.409 | 1449 / 1500 |
# 乗客数ごとの平均運賃を計算
naive_prediction_groups = taxi_train.groupby('passenger_count').fare_amount.mean()
# テストセットで予測 taxi_test['fare_amount'] = taxi_test.passenger_count.map(naive_prediction_groups)# 予測結果を書き出し taxi_test[['id','fare_amount']].to_csv('mean_group_sub.csv', index=False)
| 検証RMSE | 公開LB RMSE | 公開LB順位 |
|---|---|---|
| 9.978 | 9.407 | 1411 / 1500 |
# 数値特徴のみを選択
features = ['pickup_longitude', 'pickup_latitude',
'dropoff_longitude', 'dropoff_latitude', 'passenger_count']
from sklearn.ensemble import GradientBoostingRegressor # 勾配ブースティングモデルを学習 gb = GradientBoostingRegressor() gb.fit(taxi_train[features], taxi_train.fare_amount)# テストデータで予測 taxi_test['fare_amount'] = gb.predict(taxi_test[features])
# 予測結果を書き出し
taxi_test[['id','fare_amount']].to_csv('gb_sub.csv', index=False)
| 検証RMSE | 公開LB RMSE | 公開LB順位 |
|---|---|---|
| 5.996 | 4.595 | 1109 / 1500 |
| モデル | 検証RMSE | 公開LB RMSE |
|---|---|---|
| 単純平均 | 9.986 | 9.409 |
| グループ平均 | 9.978 | 9.407 |
| 勾配ブースティング | 5.996 | 4.595 |
| モデル | 検証RMSE | 公開LB RMSE |
|---|---|---|
| モデルA | 3.500 | 3.800 |
| モデルB | 3.300 | 4.100 |
| モデルC | 3.200 | 3.900 |
| モデル | 検証RMSE | 公開LB RMSE |
|---|---|---|
| モデルA | 3.400 | 3.900 |
| モデルB | 3.100 | 3.400 |
| モデルC | 2.900 | 3.300 |
Pythonで挑むKaggleコンペティション