Kaggle competition: Tabular Playground Series - Feb 2021
test.isnull().sum()<feature_engineering>
import lightgbm as lgb
import optuna.integration.lightgbm as oplgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
import category_encoders as ce
import seaborn as sns
test['abs_longitude_diff'] =(test['pickup_longitude']
- test['dropoff_longitude'] ).abs()
test['abs_latitude_diff'] =(test['pickup_latitude']
- test['dropoff_latitude'] ).abs()
haverSine(test)
 getDateTime(test)
 getYear(test)
 getMonth(test)
 getTime(test)
 getWeekday(test)
 getDayOrNight(test)
 distFromImpLocations(test)
 isHoliday(test )<feature_engineering>
df_train = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/train.csv")
 df_test = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/test.csv")
 df_sample = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/sample_submission.csv" )
test.loc[test['passenger_count'] ==0 ,'passenger_count'
]= test['passenger_count'].mode() [0]
test.loc[test['haversine'] == 0, 'haversine'
] = test['haversine'].mean()<categorify>
train_id = df_train["id"]
test_id = df_test["id"]
df_train.drop("id", axis=1, inplace=True)
 df_test.drop("id", axis=1, inplace=True )
getLightEncoding(test)
 test.head()<concatenate>
cat_features = [f"cat{i}" for i in range(9 + 1)]
test_X = np.column_stack(( test.abs_longitude_diff,
test['abs_longitude_diff']**2,
test.abs_latitude_diff,
test['abs_latitude_diff']**2,
test['haversine'],
test['haversine']**2,
test.pickup_longitude,
test.pickup_latitude,
test.dropoff_longitude,
test.dropoff_latitude,
test.imp_0a,
test.imp_1a,
test.imp_2a,
test.light_d,
test.light_a,
test.weekday,
test.year,
test.month,
test.passenger_count))
print(test_X.shape )<compute_test_metric>
onehot_encoder = ce.one_hot.OneHotEncoder()
onehot_encoder.fit(pd.concat([df_train[cat_features], df_test[cat_features]], axis=0))
train_ohe = onehot_encoder.transform(df_train[cat_features])
 test_ohe = onehot_encoder.transform(df_test[cat_features])
 train_ohe.columns = [f"OHE_{col}" for col in train_ohe]
test_ohe.columns = [f"OHE_{col}" for col in test_ohe]
test_X[:,:6] = normalize(sigma, mean, test_X[:,:6] )<save_to_csv>
numerical_features = [f"cont{i}" for i in range(13 + 1)]
prediction = lgbmObj.predict(test_X)
 sub = pd.DataFrame()
sub['key'] = test.key
sub['fare_amount'] = prediction
sub.to_csv("prediction.csv", index = False )<set_options>
train_x = pd.concat([
df_train[numerical_features],
train_ohe
], axis=1 )
warnings.filterwarnings('ignore')
 %matplotlib inline
plt.style.use('seaborn')
<load_from_csv>
test_x = pd.concat([
df_test[numerical_features],
test_ohe
], axis=1 )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 features = [c for c in train.columns if c not in ['ID_code', 'target']]<init_hyperparams>
train_y = df_train["target"]
params = {'objective' : "binary",
'boost':"gbdt",
'metric':"auc",
'boost_from_average':"false",
'num_threads':8,
'learning_rate' : 0.01,
'num_leaves' : 13,
'max_depth':-1,
'tree_learner' : "serial",
'feature_fraction' : 0.05,
'bagging_freq' : 5,
'bagging_fraction' : 0.4,
'min_data_in_leaf' : 80,
'min_sum_hessian_in_leaf' : 10.0,
'verbosity' : 1}<train_model>
oplgb_train_data = oplgb.Dataset(train_x, train_y )
y_pred_lgb = np.zeros(len(X_test))
num_round = 1000000
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
X_train, X_valid = X[train_index], X[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 lgb_model = lgb.train(params,train_data,num_round,
valid_sets = [train_data, valid_data],
verbose_eval=1000,early_stopping_rounds = 3500)
 y_pred_lgb += lgb_model.predict(X_test, num_iteration=lgb_model.best_iteration)/5 
<predict_on_test>
oplgb_params = {
"objective": "regression",
"metric": "root_mean_squared_error",
"verbosity": -1,
"learning_rate": 0.01
}
<save_to_csv>
folds = KFold(n_splits=5, shuffle=True, random_state=2021 )
submission_lgb = pd.DataFrame({
"ID_code": test.ID_code.values,
"target":y_pred_lgb
})
 submission_lgb.to_csv('submission_lgb.csv', index=False)
 print("Handle a successful" )<set_options>
tuner_cv = oplgb.LightGBMTunerCV(oplgb_params, oplgb_train_data, num_boost_round=1000, early_stopping_rounds=100, folds=folds, verbose_eval=100, time_budget=21600)
 tuner_cv.run()
warnings.filterwarnings('ignore' )<load_from_csv>
tuner_cv.best_params
%%time
train_df = pd.read_csv(PATH+"train.csv")
 test_df = pd.read_csv(PATH+"test.csv" )<count_missing_values>
class FoldsAverageLGBM:
def __init__(self, folds):
self.folds = folds
self.models = []
def fit(self, lgb_params, train_x, train_y):
oof_preds = np.zeros_like(train_y)
 self.train_x = train_x
self.train_y = train_y.values
for tr_idx, va_idx in tqdm(folds.split(train_x)) :
tr_x, va_x = self.train_x.iloc[tr_idx], self.train_x.iloc[va_idx]
tr_y, va_y = self.train_y[tr_idx], self.train_y[va_idx]
lgb_train_dataset = lgb.Dataset(tr_x, tr_y)
 lgb_valid_dataset = lgb.Dataset(va_x, va_y)
 model = lgb.train(lgb_params, lgb_train_dataset, valid_sets=[lgb_valid_dataset], verbose_eval=100)
 self.models.append(model)
 oof_pred = model.predict(va_x)
 oof_preds[va_idx] = oof_pred
self.oof_preds = oof_preds
def predict(self, test_x):
preds = []
for model in tqdm(self.models):
pred = model.predict(test_x)
 preds.append(pred)
 preds = np.mean(preds, axis=0)
 return preds
def get_feature_importance(self, importance_type="gain"):
feature_names = self.models[0].feature_name()
feature_importances_list = [model.feature_importance(importance_type)for model in self.models]
out_df = pd.DataFrame()
for i, name in enumerate(feature_names):
out_df[name] = [v[i] for v in feature_importances_list]
return out_df
def missing_data(data):
total = data.isnull().sum()
percent =(data.isnull().sum() /data.isnull().count() *100)
 tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 types = []
for col in data.columns:
dtype = str(data[col].dtype)
 types.append(dtype)
 tt['Types'] = types
return(np.transpose(tt))<count_missing_values>
lgb_params = dict(tuner_cv.best_params)
 lgb_params["learning_rate"] = 0.005
lgb_params["early_stopping_round"] = 200
lgb_params["num_iterations"] = 20000
%%time
missing_data(train_df )<count_missing_values>
folds_average_lgbm = FoldsAverageLGBM(folds )
%%time
missing_data(test_df )<count_values>
folds_average_lgbm.fit(lgb_params, train_x, train_y )
print("There are {}% target values with 1".format(100 * train_df["target"].value_counts() [1]/train_df.shape[0]))<sort_values>
np.sqrt(mean_squared_error(df_train.target, folds_average_lgbm.oof_preds))
%%time
correlations = train_df[features].corr().abs().unstack().sort_values(kind="quicksort" ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']]
correlations.head(10 )<count_unique_values>
y_pred = folds_average_lgbm.predict(test_x )
%%time
features = train_df.columns.values[2:202]
unique_max_train = []
unique_max_test = []
for feature in features:
values = train_df[feature].value_counts()
unique_max_train.append([feature, values.max() , values.idxmax() ])
 values = test_df[feature].value_counts()
unique_max_test.append([feature, values.max() , values.idxmax() ] )<feature_engineering>
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_optuna_lgbm_ohe_v1.csv", index=False)
 sub.head()