Kaggle competition: New York City Taxi Trip Duration
data = data.merge(orders_future, on='user_id', how='left')
 data.head(10 )<filter>
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
data_train = data[data.eval_set=='train']
data_train.head()<merge>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print(f"training set shape : {train.shape}")
 print(f"testing set shape : {test.shape}" )
data_train = data_train.merge(order_products_train[['product_id','order_id', 'reordered']], on=['product_id','order_id'], how='left')
 data_train.head(15 )<feature_engineering>
train.isna().sum()
data_train['reordered'] = data_train['reordered'].fillna(0)
 data_train.head(15 )<drop_column>
print(train.loc[train['trip_duration'] > 500000] )
data_train = data_train.set_index(['user_id', 'product_id'])
 data_train.head(15 )<drop_column>
le = LabelEncoder()
le.fit(train['store_and_fwd_flag'])
 train['store_and_fwd_flag'] = le.transform(train['store_and_fwd_flag'])
 test['store_and_fwd_flag'] = le.transform(test['store_and_fwd_flag'])
 train.head()
data_train = data_train.drop(['eval_set', 'order_id'], axis=1)
 data_train.head(15 )<filter>
train['pickup_datetime'] = pd.to_datetime(train['pickup_datetime'])
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'])
 train['month'] = train['pickup_datetime'].dt.month
train['day'] = train['pickup_datetime'].dt.day
train['weekday'] = train['pickup_datetime'].dt.weekday
train['hour'] = train['pickup_datetime'].dt.hour
train['minute'] = train['pickup_datetime'].dt.minute
test['month'] = test['pickup_datetime'].dt.month
test['day'] = test['pickup_datetime'].dt.day
test['weekday'] = test['pickup_datetime'].dt.weekday
test['hour'] = test['pickup_datetime'].dt.hour
test['minute'] = test['pickup_datetime'].dt.minute
train.head()
data_test = data[data.eval_set=='test']
data_test.head()<drop_column>
selection_train = ["passenger_count", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude","month", "day", "weekday", "hour", "minute"]
selection_test = ["passenger_count", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude", "month", "day", "weekday", "hour", "minute"]
y_train = train["trip_duration"]
X_train = train[selection_train]
X_test = test[selection_test]
data_test = data_test.set_index(['user_id', 'product_id'])
 data_test.head()<drop_column>
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
 X_test_scaled = scaler.transform(X_test )
data_test = data_test.drop(['eval_set','order_id'], axis=1)
 data_test.head()<split>
m1 = RandomForestRegressor()
X_train, X_val, y_train, y_val = train_test_split(data_train.drop('reordered', axis=1), data_train.reordered, test_size=0.8, random_state=42)
<train_on_grid>
clf = RandomForestClassifier()
parameters = {'n_estimators': [14,16,20],
'max_features': [3,4,5],
'max_depth': [4,5,6]
}
acc_scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring=acc_scorer)
 grid_obj = grid_obj.fit(X_train, y_train)
 clf = grid_obj.best_estimator_
model = clf.fit(X_train, y_train )<compute_test_metric>
print("Start Training...")
 m1.fit(X_train, y_train)
y_val_pred =(model.predict_proba(X_val)[:,1] >= 0.30 ).astype(int)
 print("RESULTS ON VALIDATION SET
====================")
 print("F1 Score: ",f1_score(y_val, y_val_pred, average='binary'), "
====================")
 print("Classification Report
", classification_report(y_val, y_val_pred), "
====================")
 print("Confusion Matrix
", confusion_matrix(y_val, y_val_pred))<predict_on_test>
y_pred = m1.predict(X_test)
test_pred = model.predict(data_test ).astype(int)
 test_pred =(model.predict_proba(data_test)[:,1] >= 0.30 ).astype(int)
 test_pred[0:20]<predict_on_test>
filename = 'finalized_model.sav'
joblib.dump(m1, filename)
data_test['prediction'] = test_pred
data_test.head(10 )<filter>
my_submission = pd.DataFrame({'id': test.id, 'trip_duration': np.expm1(y_pred)})
 my_submission.head()
orders_test = orders.loc[orders.eval_set=='test',("user_id", "order_id")]
orders_test.head()<merge>
my_submission.to_csv('submission.csv', index=False)
 print("Submission done !" )