Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<import_modules>
%env JOBLIB_TEMP_FOLDER=/tmp
%matplotlib inline
import xgboost as xgb<train_model>
train = pd.read_csv(".. /input/train.csv", parse_dates=['pickup_datetime'], usecols=range(1,8), nrows=700_000)
 test = pd.read_csv(".. /input/test.csv", parse_dates=['pickup_datetime'] )
params = {'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'eta': 0.01,
'max_depth': 10,
'subsample': 0.6,
'colsample_bytree': 0.6,
'lambda':10,
'alpha':2,
'random_state': 42,
'silent': True}
tr_data = xgb.DMatrix(X_train, y_train)
 va_data = xgb.DMatrix(X_val, y_val)
 watchlist = [(tr_data, 'train'),(va_data, 'valid')]
model_xgb = xgb.train(params, tr_data, 1000, watchlist, maximize=False,verbose_eval=100 )<predict_on_test>
train = train.dropna(how = 'any', axis = 'rows')
 train = train[train.eval('(fare_amount > 0)&(passenger_count <= 6)')]
dtest = xgb.DMatrix(test_df)
 y_xgb_pred = model_xgb.predict(dtest, ntree_limit=model_xgb.best_ntree_limit )<import_modules>
train = train[(train.pickup_longitude >= -77)& 
(train.pickup_longitude <= -70)& 
(train.dropoff_longitude >= -77)& 
(train.dropoff_longitude <= 70)& 
(train.pickup_latitude >= 35)& 
(train.pickup_latitude <= 45)& 
(train.dropoff_latitude >= 35)& 
(train.dropoff_latitude <= 45)
 ]
from catboost import CatBoostRegressor<choose_model_class>
train.pickup_datetime = train.pickup_datetime.dt.tz_localize('UTC')
 train.pickup_datetime = train.pickup_datetime.dt.tz_convert(tz.gettz('America/New_York'))
train['year'] = train.pickup_datetime.dt.year
train['dayofweek'] = train.pickup_datetime.dt.dayofweek
train['dayofyear'] = train.pickup_datetime.dt.dayofyear
train['hourofday'] = train.pickup_datetime.dt.hour
train = train.drop('pickup_datetime', axis=1 )
cb_model = CatBoostRegressor(iterations=795,
learning_rate=0.05,
depth=10,
reg_lambda=10,
eval_metric='RMSE',
random_seed = 42,
bagging_temperature = 0.2,
od_type='Iter',
metric_period = 50,
od_wait=20)
 cb_model.fit(X_train, y_train,
eval_set=(X_val, y_val),
use_best_model=True,
verbose=50 )<predict_on_test>
airports = {'jfk': [40.6441666, -73.7822222],
'laguardia': [40.7747222, -73.8719444],
'newark': [40.6897222, -74.175]}
pickup = train.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(airports.get('jfk')) ).miles, axis=1)
 dropoff = train.apply(lambda x: distance.distance(( x.dropoff_latitude, x.dropoff_longitude),(airports.get('jfk')) ).miles, axis=1)
 train['to_jfk'] = pd.concat(( pickup, dropoff), axis=1 ).min(axis=1)
 pickup = train.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(airports.get('laguardia')) ).miles, axis=1)
 dropoff = train.apply(lambda x: distance.distance(( x.dropoff_latitude, x.dropoff_longitude),(airports.get('laguardia')) ).miles, axis=1)
 train['to_laguardia'] = pd.concat(( pickup, dropoff), axis=1 ).min(axis=1)
 pickup = train.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(airports.get('newark')) ).miles, axis=1)
 dropoff = train.apply(lambda x: distance.distance(( x.dropoff_latitude, x.dropoff_longitude),(airports.get('newark')) ).miles, axis=1)
 train['to_newark'] = pd.concat(( pickup, dropoff), axis=1 ).min(axis=1)
 del pickup, dropoff
cat_pred =cb_model.predict(test_df )<feature_engineering>
y = train.fare_amount
train = train.drop('fare_amount', axis=1)
sample_sub['target'] =(y_pred_lgb + y_xgb_pred + cat_pred)/ 3<save_to_csv>
xgb_param = {'eval_metric': 'rmse',
'n_estimators': 3000,
'max_depth': 9,
'learning_rate': 0.1,
'subsample': 0.9,
'colsample_bytree': 0.8,
'gamma': 1e-4,
'reg_alpha': 1e-4,
'verbose': 0,
'n_jobs': -1
}
xgb_model = xgb.XGBRegressor(**xgb_param)
 xgb_model.fit(train, y)
 xgb.plot_importance(xgb_model )
sample_sub.to_csv('submission.csv',columns=['id', 'target'], header=True, index=False )<set_options>
test_key = test['key']
test = test.drop('key', axis=1)
HTML('<iframe width="800" height="400" src="https://www.youtube.com/embed/-UeC4MR3PHM?start=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>' )<set_options>
test.pickup_datetime = test.pickup_datetime.dt.tz_localize('UTC')
 test.pickup_datetime = test.pickup_datetime.dt.tz_convert(tz.gettz('America/New_York'))
test['year'] = test.pickup_datetime.dt.year
test['dayofweek'] = test.pickup_datetime.dt.dayofweek
test['dayofyear'] = test.pickup_datetime.dt.dayofyear
test['hourofday'] = test.pickup_datetime.dt.hour
test = test.drop('pickup_datetime', axis=1)
 test['distance'] = test.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(x.dropoff_latitude, x.dropoff_longitude)).miles, axis = 1)
 pickup = test.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(airports.get('jfk')) ).miles, axis=1)
 dropoff = test.apply(lambda x: distance.distance(( x.dropoff_latitude, x.dropoff_longitude),(airports.get('jfk')) ).miles, axis=1)
 test['to_jfk'] = pd.concat(( pickup, dropoff), axis = 1 ).min(axis=1)
 pickup = test.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(airports.get('laguardia')) ).miles, axis=1)
 dropoff = test.apply(lambda x: distance.distance(( x.dropoff_latitude, x.dropoff_longitude),(airports.get('laguardia')) ).miles, axis=1)
 test['to_laguardia'] = pd.concat(( pickup, dropoff), axis = 1 ).min(axis=1)
 pickup = test.apply(lambda x: distance.distance(( x.pickup_latitude, x.pickup_longitude),(airports.get('newark')) ).miles, axis=1)
 dropoff = test.apply(lambda x: distance.distance(( x.dropoff_latitude, x.dropoff_longitude),(airports.get('newark')) ).miles, axis=1)
 test['to_newark'] = pd.concat(( pickup, dropoff), axis = 1 ).min(axis=1)
 del pickup, dropoff
py.init_notebook_mode(connected=True)
<set_options>
xgb_predict = xgb_model.predict(test )
<load_from_disk><EOS> 
xgb_submission = pd.DataFrame({ 'key': test_key,
'fare_amount': xgb_predict })
 xgb_submission.to_csv("xgb_submission.csv", index=False )