Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<create_dataframe>
data_path = '.. /input/'
df_train = pd.read_csv(data_path + 'train.csv')
 df_test = pd.read_csv(data_path + 'test.csv')
 df_train = df_train.sample(n=5000000)
 print(df_train.shape)
 print(df_test.shape )
lgb_train = lgb.Dataset(X_train_df, y_train_df, free_raw_data=False)
 lgb_eval = lgb.Dataset(X_val_df, y_val_df, free_raw_data=False )<init_hyperparams>
df_train.isnull().sum()
<init_hyperparams>
df_train = df_train.dropna()
df_train.isnull().sum()
gsLGBM.best_params_ 
<train_model>
df_test.isnull().sum()
m3 = lgb.LGBMRegressor(valid_sets = [lgb_train, lgb_eval], verbose_eval = 30, num_boost_round = 10000, early_stopping_rounds = 10, n_jobs=4, **opt_parameters_LGBM)
 m3.fit(X_train_df, y_train_df, eval_set =(X_val_df, y_val_df), eval_metric = 'rmse' )<predict_on_test>
df_train = df_train.loc[(df_train['fare_amount'] > 0)&(df_train['fare_amount'] < 200)]
df_train = df_train.loc[(df_train['pickup_longitude'] > -300)&(df_train['pickup_longitude'] < 300)]
df_train = df_train.loc[(df_train['pickup_latitude'] > -300)&(df_train['pickup_latitude'] < 300)]
df_train = df_train.loc[(df_train['dropoff_longitude'] > -300)&(df_train['dropoff_longitude'] < 300)]
df_train = df_train.loc[(df_train['dropoff_latitude'] > -300)&(df_train['dropoff_latitude'] < 300)]
df_train = df_train.loc[df_train['passenger_count'] <= 8]
df_train.head()
X_test = test_df
y_test_lgbm = m3.predict(X_test )<init_hyperparams>
df_test = df_test.loc[(df_test['pickup_longitude'] > -300)&(df_test['pickup_longitude'] < 300)]
df_test = df_test.loc[(df_test['pickup_latitude'] > -300)&(df_test['pickup_latitude'] < 300)]
df_test = df_test.loc[(df_test['dropoff_longitude'] > -300)&(df_test['dropoff_longitude'] < 300)]
df_test = df_test.loc[(df_test['dropoff_latitude'] > -300)&(df_test['dropoff_latitude'] < 300)]
df_test = df_test.loc[df_test['passenger_count'] <= 8]
df_test.head()
opt_parameters_ADA = {'learning_rate': 0.028555288989857153, 'n_estimators': 36}<train_model>
df_train = df_train.drop(['index'], axis=1)
 df_train.head()
m4 = AdaBoostRegressor(base_estimator=DecisionTreeRegressor(max_depth=3, min_samples_leaf=1, min_impurity_decrease=10, random_state=47), random_state=47, **opt_parameters_ADA)
 m4.fit(X_train_df, y_train_df )<predict_on_test>
ids = df_test['key']
train_Y = df_train['fare_amount']
df_train = df_train.drop(['fare_amount'], axis=1)
 print(df_train.shape)
 print(df_test.shape )
X_test = test_df
y_test_ada = m4.predict(X_test )<choose_model_class>
df_train['pickup_datetime'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S UTC'))
df_train['pickup_year'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%Y')).astype('int64')
 df_train['pickup_month'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%m')).astype('int64')
 df_train['pickup_day'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%d')).astype('int64')
 df_train['pickup_hour'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%H')).astype('int64')
 df_train['pickup_minute'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%M')).astype('int64')
 df_train['pickup_second'] = df_train['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%S')).astype('int64')
 df_test['pickup_datetime'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S UTC'))
df_test['pickup_year'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%Y')).astype('int64')
 df_test['pickup_month'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%m')).astype('int64')
 df_test['pickup_day'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%d')).astype('int64')
 df_test['pickup_hour'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%H')).astype('int64')
 df_test['pickup_minute'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%M')).astype('int64')
 df_test['pickup_second'] = df_test['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%S')).astype('int64' )
m5 = CatBoostRegressor(random_seed=47 )<train_model>
df_train = df_train.drop(['pickup_datetime'], axis=1)
 df_test = df_test.drop(['pickup_datetime'], axis=1 )
gsCB.fit(X_train_df, y_train_df)
 print('Best score reached: {} with params: {} '.format(gsCB.best_score_, gsCB.best_params_))<predict_on_test>
df_train['longitude_diff'] = df_train['dropoff_longitude'] - df_train['pickup_longitude']
df_train['latitude_diff'] = df_train['dropoff_latitude'] - df_train['pickup_latitude']
df_train['distance'] =(( df_train['longitude_diff']**2)+(df_train['latitude_diff']**2)) **0.5
df_test['longitude_diff'] = df_test['dropoff_longitude'] - df_test['pickup_longitude']
df_test['latitude_diff'] = df_test['dropoff_latitude'] - df_test['pickup_latitude']
df_test['distance'] =(( df_test['longitude_diff']**2)+(df_test['latitude_diff']**2)) **0.5
X_test = test_df
y_test_cb = m5.predict(X_test )<choose_model_class>
df_train = df_train.drop(['key'], axis=1)
 df_test = df_test.drop(['key'], axis=1 )
gbm = xgb.XGBRegressor(
 learning_rate = 0.01,
n_estimators= 100,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'reg:squaredlogerror',
nthread= -1,
verbosity=3,
random_state=20)
 lgbm_params = m1.get_params()
lgbm_params["early_stopping_rounds"] = None
m1.set_params(**lgbm_params)
 estimators = [('lgbm', m1),('ada', m2),('lasso', m3),('ridge', m4)]
gbm = StackingRegressor(estimators=estimators, final_estimator=gbm, cv=5, verbose=1 )<drop_column>
x_train,x_test,y_train,y_test = train_test_split(df_train, train_Y, test_size=0.2, random_state=0)
 def xgbmodel(x_train,x_test,y_train,y_test):
matrix_train = xgb.DMatrix(x_train, label=y_train)
 matrix_test = xgb.DMatrix(x_test, label=y_test)
 model = xgb.train(params={'objective':'reg:linear', 'eval_metric':'rmse'},
dtrain=matrix_train,
num_boost_round=200,
early_stopping_rounds=20,
evals=[(matrix_test,'test')])
 return model
myxgbmodel = xgbmodel(x_train,x_test,y_train,y_test)
 pred = myxgbmodel.predict(xgb.DMatrix(df_test), ntree_limit=myxgbmodel.best_ntree_limit )
<train_model>
submission = pd.DataFrame({'key':ids, 'fare_amount':pred})
 submission.to_csv('submission.csv', index=False )