Kaggle competition: New York City Taxi Trip Duration
def target_encoding(path_source_dataframe, path_output_dataframe):
df_train_test = pd.read_feather(path_source_dataframe)
 df_train_test.loc[:, "target"] = pd.read_feather(".. /working/train_test.ftr")["target"]
df_train = df_train_test.dropna(subset=["target"] ).copy()
df_test = df_train_test.loc[df_train_test["target"].isnull() ].copy()
fold = KFold(n_splits=5, shuffle=True, random_state=111)
 encoder = xfeat.TargetEncoder(fold=fold, target_col="target", output_suffix="")
 df_train = encoder.fit_transform(cudf.from_pandas(df_train))
df_test = encoder.transform(cudf.from_pandas(df_test))
pd.concat([df_train.to_pandas() , df_test.to_pandas() ], sort=False ).drop(
 "target", axis=1 ).reset_index(drop=True ).to_feather(path_output_dataframe)
 print("(2')Target encoding of categorical variables")
 target_encoding(".. /working/feature_1way_label_encoding.ftr",
".. /working/feature_1way_label_encoding_with_te.ftr" )<categorify>
%matplotlib inline
print("(3)2-order combination of categorical features: 78 features(13 * 12 / 2 = 78)")
 xfeat.Pipeline([
xfeat.SelectCategorical() ,
xfeat.ConcatCombination(drop_origin=True, r=2),
xfeat.LabelEncoder(output_suffix=""),
] ).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_2way_label_encoding.ftr"
)<categorify>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 sample_submission = pd.read_csv('.. /input/sample_submission.csv' )
print("(4)3-order combination of categorical features")
 xfeat.Pipeline([
xfeat.SelectCategorical() ,
xfeat.ConcatCombination(drop_origin=True, include_cols=["v22"], r=3),
xfeat.LabelEncoder(output_suffix=""),
] ).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_3way_including_v22_label_encoding.ftr" 
)
 print("(5)Convert numerical to categorical using round: 12 features")
 df_rnum =(
 xfeat.Pipeline(
 [
xfeat.SelectNumerical() ,
xfeat.LambdaEncoder(
 lambda x: str(x)[:-2],
output_suffix="_rnum",
exclude_cols=["target"], 
),
] 
)
.fit_transform(pd.read_feather(".. /working/train_test.ftr")) 
.reset_index(drop=True)
)
 df_rnum.to_feather(".. /working/feature_round_num.ftr")
 rnum_cols = [col for col in df_rnum.columns if col.endswith("_rnum")]
xfeat.Pipeline([xfeat.LabelEncoder(output_suffix="")] ).fit_transform(
 pd.read_feather(".. /working/feature_round_num.ftr")[rnum_cols]
).reset_index(drop=True ).to_feather(".. /working/feature_round_num_label_encoding.ftr")
 print("(6)2-order Arithmetic combinations.")
 xfeat.Pipeline(
 [
xfeat.SelectNumerical() ,
xfeat.ArithmeticCombinations(
 exclude_cols=["target"], drop_origin=True, operator="+", r=2, 
),
]
).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_arithmetic_combi2.ftr" 
)
 print("(7)Add more combinations: 11-order concat combinations.")
 xfeat.Pipeline(
 [
xfeat.SelectCategorical() ,
xfeat.ConcatCombination(drop_origin=True, include_cols=["v22"], r=11),
xfeat.LabelEncoder(output_suffix=""),
]
).fit_transform(pd.read_feather(".. /working/train_test.ftr")).reset_index(
 drop=True
).to_feather(
 ".. /working/feature_11way_including_v22_label_encoding.ftr" 
)
 print("(3')Target encoding of categorical variables")
 target_encoding(".. /working/feature_2way_label_encoding.ftr",
".. /working/feature_2way_label_encoding_with_te.ftr")
 print("(4')Target encoding of categorical variables")
 target_encoding(".. /working/feature_3way_including_v22_label_encoding.ftr",
".. /working/feature_3way_including_v22_label_encoding_with_te.ftr")
 print("(5')Target encoding of categorical variables")
 target_encoding(".. /working/feature_round_num_label_encoding.ftr",
".. /working/feature_round_num_label_encoding_with_te.ftr")
 print("(7')Target encoding of categorical variables")
 target_encoding(".. /working/feature_11way_including_v22_label_encoding.ftr",
".. /working/feature_11way_including_v22_label_encoding_with_te.ftr" )<prepare_x_and_y>
train.isnull().sum()
def catboost_model() :
print("Load numerical features")
 df_num = pd.concat(
 [
pd.read_feather(".. /working/feature_num_features.ftr"),
pd.read_feather(".. /working/feature_arithmetic_combi2.ftr"),
],
axis=1, 
)
 y_train = df_num["target"].dropna()
df_num.drop(["target"], axis=1, inplace=True)
 print("Load categorical features")
 df = pd.concat(
 [
pd.read_feather(".. /working/feature_1way_label_encoding.ftr"),
pd.read_feather(".. /working/feature_2way_label_encoding.ftr"),
pd.read_feather(".. /working/feature_3way_including_v22_label_encoding.ftr"),
pd.read_feather(".. /working/feature_round_num_label_encoding.ftr"),
pd.read_feather(".. /working/feature_11way_including_v22_label_encoding.ftr"),
],
axis=1, 
)
 cat_cols = df.columns.tolist()
df = pd.concat([df, df_num], axis=1)
 print("Fit")
 params = {
"loss_function": "Logloss",
"eval_metric": "Logloss",
"learning_rate": 0.03,
"iterations": 3000,
"l2_leaf_reg": 3,
"random_seed": 432013,
"subsample": 0.66,
"od_type": "Iter",
"rsm": 0.2,
"depth": 6,
"border_count": 128,
}
model = cat.CatBoostClassifier(**params)
 train_data = cat.Pool(
 df.iloc[: y_train.shape[0]], label=y_train, cat_features=cat_cols 
)
 fit_model = model.fit(train_data, verbose=30)
 y_pred = fit_model.predict_proba(df.iloc[y_train.shape[0] :])
 submission = pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/sample_submission.csv.zip")
 submission.loc[:, "PredictedProb"] = y_pred[:, 1]
submission.to_csv(".. /working/solution_cat.csv", index=False)
 LIGHTGBM_BASE_PARAMS = {
"objective": "binary",
"metric": "binary_logloss",
"verbosity": 1,
"boosting_type": "gbdt",
"num_leaves": 32,
"feature_fraction": 0.8,
"bagging_fraction": 1.0,
"bagging_freq": 0,
"learning_rate": 0.03,
"max_bin": 255,
"seed": 1,
"min_data_in_leaf": 20,
}
LIGHTGBM_BASE_FIT_PARAMS = {
"verbose_eval": 30,
"num_boost_round": 3000,
}
def lightgbm_model() :
print("Load numerical features")
 df_num = pd.concat(
 [
pd.read_feather(".. /working/feature_num_features.ftr"),
pd.read_feather(".. /working/feature_arithmetic_combi2.ftr"),
],
axis=1, 
)
 y_train = df_num["target"].dropna()
df_num.drop(["target"], axis=1, inplace=True)
 print("Load categorical features")
 df = pd.concat(
 [
pd.read_feather(".. /working/feature_1way_label_encoding_with_te.ftr"),
pd.read_feather(".. /working/feature_2way_label_encoding_with_te.ftr"),
pd.read_feather(".. /working/feature_3way_including_v22_label_encoding_with_te.ftr"),
pd.read_feather(".. /working/feature_round_num_label_encoding_with_te.ftr"),
pd.read_feather(".. /working/feature_11way_including_v22_label_encoding_with_te.ftr"),
],
axis=1, 
)
 cat_cols = df.columns.tolist()
df = pd.concat([df, df_num], axis=1)
 X_train, X_test = df.values[:y_train.shape[0]], df.values[y_train.shape[0]:]
dtrain = lgb.Dataset(X_train, y_train)
 fit_params = LIGHTGBM_BASE_FIT_PARAMS.copy()
fit_params["valid_sets"] = [dtrain]
bst = lgb.train(LIGHTGBM_BASE_PARAMS, dtrain, **fit_params)
 y_pred = bst.predict(X_test)
 submission = pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/sample_submission.csv.zip")
 submission.loc[:, "PredictedProb"] = y_pred
submission.to_csv(".. /working/solution_lgb.csv", index=False)
 lightgbm_model()
catboost_model()
y1 = pd.read_csv(".. /working/solution_cat.csv")
 y2 = pd.read_csv(".. /working/solution_lgb.csv")
 df_sub = pd.read_csv(PATH_SUB_CSV)
 df_sub.loc[:, "PredictedProb"] = 0.6 * y1.PredictedProb.values + 0.4 * y2.PredictedProb.values
df_sub[["ID", "PredictedProb"]].to_csv(".. /working/solution_blend.csv", index=False )<load_from_csv>
train.isnull().sum()
print('Load data...')
 train = pd.read_csv(".. /input/train.csv")
 target = train['target'].values
train = train.drop(['ID','target','v8','v23','v25','v31','v36','v37','v46','v51','v53','v54','v63','v73','v75','v79','v81','v82','v89','v92','v95','v105','v107','v108','v109','v110','v116','v117','v118','v119','v123','v124','v128'],axis=1)
 test = pd.read_csv(".. /input/test.csv")
 id_test = test['ID'].values
test = test.drop(['ID','v8','v23','v25','v31','v36','v37','v46','v51','v53','v54','v63','v73','v75','v79','v81','v82','v89','v92','v95','v105','v107','v108','v109','v110','v116','v117','v118','v119','v123','v124','v128'],axis=1)
 print('Clearing...')
 for(train_name, train_series),(test_name, test_series)in zip(train.iteritems() ,test.iteritems()):
if train_series.dtype == 'O':
train[train_name], tmp_indexer = pd.factorize(train[train_name])
 test[test_name] = tmp_indexer.get_indexer(test[test_name])
 else:
tmp_len = len(train[train_series.isnull() ])
 if tmp_len>0:
train.loc[train_series.isnull() , train_name] = -999
tmp_len = len(test[test_series.isnull() ])
 if tmp_len>0:
test.loc[test_series.isnull() , test_name] = -999
X_train = train
X_test = test
print('Training...')
 extc = ExtraTreesClassifier(n_estimators=850,max_features= 60,criterion= 'entropy',min_samples_split= 4,
max_depth= 40, min_samples_leaf= 2, n_jobs = -1)
 extc.fit(X_train,target)
 print('Predict...')
 y_pred = extc.predict_proba(X_test)
 pd.DataFrame({"ID": id_test, "PredictedProb": y_pred[:,1]} ).to_csv('extra_trees.csv',index=False )<load_from_csv>
train.dropna(inplace=True)
 train.isnull().sum()
print('Load data...')
 train = pd.read_csv(".. /input/train.csv")
 target = train['target'].values
train = train.drop(['ID','target','v8','v23','v25','v31','v36','v37','v46','v51','v53','v54','v63','v73','v75','v79','v81','v82','v89','v92','v95','v105','v107','v108','v109','v110','v116','v117','v118','v119','v123','v124','v128'],axis=1)
 test = pd.read_csv(".. /input/test.csv")
 id_test = test['ID'].values
test = test.drop(['ID','v8','v23','v25','v31','v36','v37','v46','v51','v53','v54','v63','v73','v75','v79','v81','v82','v89','v92','v95','v105','v107','v108','v109','v110','v116','v117','v118','v119','v123','v124','v128'],axis=1)
 print('Clearing...')
 for(train_name, train_series),(test_name, test_series)in zip(train.iteritems() ,test.iteritems()):
if train_series.dtype == 'O':
train[train_name], tmp_indexer = pd.factorize(train[train_name])
 test[test_name] = tmp_indexer.get_indexer(test[test_name])
 else:
tmp_len = len(train[train_series.isnull() ])
 if tmp_len>0:
train.loc[train_series.isnull() , train_name] = -999
tmp_len = len(test[test_series.isnull() ])
 if tmp_len>0:
test.loc[test_series.isnull() , test_name] = -999
X_train = train
X_test = test
print('Training...')
 extc = ExtraTreesClassifier(n_estimators=850,max_features= 60,criterion= 'entropy',min_samples_split= 4,
max_depth= 40, min_samples_leaf= 2, n_jobs = -1)
 extc.fit(X_train,target)
 print('Predict...')
 y_pred = extc.predict_proba(X_test)
 pd.DataFrame({"ID": id_test, "PredictedProb": y_pred[:,1]} ).to_csv('extra_trees.csv',index=False )<import_modules>
train['pickup_datetime'] = pd.to_datetime(train['pickup_datetime'], format='%Y-%m-%d %H:%M:%S')
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'], format='%Y-%m-%d %H:%M:%S' )
import pandas as pd
import gc<define_variables>
train['hour'] = train.loc[:,'pickup_datetime'].dt.hour;
train['week'] = train.loc[:,'pickup_datetime'].dt.week;
train['weekday'] = train.loc[:,'pickup_datetime'].dt.weekday;
train['hour'] = train.loc[:,'pickup_datetime'].dt.hour;
train['month'] = train.loc[:,'pickup_datetime'].dt.month;
PATH = '/kaggle/input/instacart-market-basket-analysis/'<load_from_csv>
test['hour'] = test.loc[:,'pickup_datetime'].dt.hour;
test['week'] = test.loc[:,'pickup_datetime'].dt.week;
test['weekday'] = test.loc[:,'pickup_datetime'].dt.weekday;
test['hour'] = test.loc[:,'pickup_datetime'].dt.hour;
test['month'] = test.loc[:,'pickup_datetime'].dt.month;
aisles = pd.read_csv(PATH + 'aisles.csv')
 products = pd.read_csv(PATH + 'products.csv')
 order_products_train = pd.read_csv(PATH + 'order_products__train.csv')
 order_products_prior = pd.read_csv(PATH + 'order_products__prior.csv')
 departments = pd.read_csv(PATH + 'departments.csv')
 orders = pd.read_csv(PATH + 'orders.csv' )<merge>
cat_vars = ['store_and_fwd_flag']
op = pd.merge(orders, order_products_prior, on='order_id', how='inner')
 op.head()<create_dataframe>
for col in cat_vars:
train[col] = train[col].astype('category' ).cat.codes
train.head()
total_prd_per_order = op.groupby(by=['user_id', 'order_id'])['product_id'].aggregate('count' ).to_frame('total_products_per_order' ).reset_index()
total_prd_per_order.head(10 )<create_dataframe>
for col in cat_vars:
test[col] = test[col].astype('category' ).cat.codes
test.head()
avg_products = total_prd_per_order.groupby(by=['user_id'])['total_products_per_order'].mean().to_frame('u_avg_prd' ).reset_index()
avg_products.head()<drop_column>
selection_train = ["passenger_count","store_and_fwd_flag", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude", "week", "weekday", "hour", "month"]
selection_test = ["passenger_count","store_and_fwd_flag", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude", "week", "weekday", "hour", "month"]
y_train = np.log(train["trip_duration"])
 X_train = train[selection_train]
X_test = test[selection_test]
del total_prd_per_order<merge>
X = np.random.randn(10, 3)
 scaler = StandardScaler()
scaler.fit(X )
users = users.merge(avg_products, on='user_id', how='left')
 del avg_products
users.head()<merge>
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
 X_test_scaled = scaler.transform(X_test )
users = users.merge(dow, on='user_id', how='left')
 del dow
users.head()<merge>
from sklearn.ensemble import RandomForestRegressor
users = users.merge(hod, on='user_id', how='left')
 del hod
users.head()<prepare_output>
from sklearn.ensemble import RandomForestRegressor
prd = op.groupby(by='product_id')['order_id'].agg('count' ).to_frame('prd_count_p' ).reset_index()
prd.head()<merge>
mrf = RandomForestRegressor(n_estimators=20)
 mrf.fit(X_train, y_train )
prd = prd.merge(reorder_p, on='product_id', how='left')
 del reorder_p
prd.head()<merge>
from sklearn.model_selection import cross_val_score
uxp = uxp.merge(reorder_uxp, on=['user_id', 'product_id'], how='left')
 del reorder_uxp
uxp.head()<merge>
data = uxp.merge(users, on='user_id', how='left')
 data.head()<merge>
y_test_pred = mrf.predict(X_test)
 y_test_pred[:5]
data = data.merge(prd, on='product_id', how='left')
 data.head()<drop_column>
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission.head()
del [users, prd, uxp]<prepare_output>
submission['trip_duration']= np.exp(y_test_pred)
 submission.to_csv('asmasem_submission.csv', index=False )