Kaggle competition: Tabular Playground Series - Feb 2021
test_df.has_cactus = preds.numpy() [:, 0]<save_to_csv>
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_df.to_csv('submission.csv', index=False)
<set_options>
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" )
gc.collect()<define_variables>
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 )
imp_locations = [(40.639722, -73.778889),(40.6925, -74.168611), 
(40.77725, -73.872611)]<prepare_x_and_y>
cat_features = [f"cat{i}" for i in range(9 + 1)]
cal = USFederalHolidayCalendar()
holidays = pd.to_datetime(cal.holidays(start='2009-01-01', end='2015-12-31'))
def haverSine(df):
Radius = 6371
h = np.sqrt(( np.sin(df['abs_latitude_diff']/2)*
np.sin(df['abs_latitude_diff']/2)) + 
(np.cos(df['pickup_latitude'])*
np.cos(df['dropoff_latitude'])
 *(np.sin(df['abs_longitude_diff']/2)*
np.sin(df['abs_longitude_diff']/2))))
df['haversine'] = 2*Radius*np.arcsin(h)
 def calcHaversine(lat1, long1, lat2, long2):
Radius = 6371
h = np.sqrt(( np.sin(( lat1 - lat2)/2)*
np.sin(( lat1- lat2)/2)) + 
(np.cos(lat1)*
np.cos(lat2)
 *(np.sin(( long1 - long2)/2)*
np.sin(( long1 - long2)/2))))
return 2*np.arcsin(h)
 def getDateTime(df):
df['datetime'] = pd.to_datetime(df['pickup_datetime'],
format = "%Y-%m-%d %H:%M:%S %Z")
 def getYear(df):
df['year'] = df['datetime'].dt.year
def getTime(df):
df['time'] = df['datetime'].dt.hour
def getMonth(df):
df['month'] = df['datetime'].dt.month
def getWeekday(df):
df['weekday'] = df['datetime'].dt.weekday
def getDayOrNight(df):
df['light'] = df['time'].apply(lambda x:"D" if(x >5 and x <18)
 else "A" if(x >=18 and x<=23)else "N")
 def getLightEncoding(df):
df['light_d'] = df['light'].apply(lambda x:1 if x == "D" else 0)
 df['light_a'] = df['light'].apply(lambda x:1 if x == "N" else 0)
 df['light_n'] = df['light'].apply(lambda x:1 if x == "a" else 0)
 def distFromImpLocations(df):
for i in range(len(imp_locations)) :
df['imp_'+str(i)+"a"] = calcHaversine(df.dropoff_latitude,
df.dropoff_longitude,
imp_locations[i][0], imp_locations[i][1])
 df['imp_'+str(i)+"b"] = calcHaversine(df.dropoff_latitude,
df.dropoff_longitude,
imp_locations[i][0], imp_locations[i][1])
 def isHoliday(df):
df['isHoliday'] = df['datetime'].apply(lambda x:1 if x.date() in holidays
else 0)
<feature_engineering>
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]
train['abs_longitude_diff'] =(train['pickup_longitude'] -
train['dropoff_longitude'] ).abs()
train['abs_latitude_diff'] =(train['pickup_latitude'] -
train['dropoff_latitude'] ).abs()
haverSine(train)
 getDateTime(train)
 getYear(train)
 getMonth(train)
 getTime(train)
 getWeekday(train)
 getDayOrNight(train)
 distFromImpLocations(train)
 isHoliday(train )<count_missing_values>
numerical_features = [f"cont{i}" for i in range(13 + 1)]
train.isnull().sum()<feature_engineering>
train_x = pd.concat([
df_train[numerical_features],
train_ohe
], axis=1 )
train.loc[train['passenger_count'] ==0 ,'passenger_count'
]= train['passenger_count'].mode() [0]
train.loc[train['haversine'] == 0, 'haversine'
] = train['haversine'].mean()<filter>
test_x = pd.concat([
df_test[numerical_features],
test_ohe
], axis=1 )
print("OldSize = ", train.shape)
 train1 = train.loc[(train.abs_longitude_diff < 5.0)& 
(train['abs_latitude_diff'] < 5.0)& 
(train['fare_amount'] > 0.0)& 
(train.fare_amount<100.0)& 
(train.pickup_longitude<=-71.0)& 
(train.pickup_longitude>=-75)& 
(train.pickup_latitude>=40.0)& 
(train.pickup_latitude<=42.0)& 
(train.dropoff_longitude>=-75.0)& 
(train.dropoff_longitude<=-71.0)& 
(train.dropoff_latitude<=42.0)& 
(train.dropoff_latitude>=40.0),:]
print("NewSize = ",train1.shape )<categorify>
train_y = df_train["target"]
getLightEncoding(train1 )<concatenate>
folds = KFold(n_splits=5, shuffle=True, random_state=2021 )
train2 = np.column_stack(( train1['abs_longitude_diff'],
train1['abs_longitude_diff']**2,
train1['abs_latitude_diff'],
train1['abs_latitude_diff']**2,
train1.haversine,
train1['haversine']**2,
train1.pickup_longitude,
train1.pickup_latitude,
train1.dropoff_longitude,
train1.dropoff_latitude,
train1.imp_0a,
train1.imp_1a,
train1.imp_2a,
train1.light_d,
train1.light_a,
train1.weekday,
train1.year,
train1.month,
train1.passenger_count))
FA = train1['fare_amount'].values
print(train2.shape)
 print(FA.shape )<normalization>
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 normalize(sigma, mean, X):
X =(X - mean)/sigma
return X
train2[:,0:6] = normalize(sigma, mean, train2[:,0:6] )<split>
lgb_params = {
'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'learning_rate': 0.01,
'feature_pre_filter': False,
'lambda_l1': 6.271548464074981,
'lambda_l2': 6.442666191955093e-05,
'num_leaves': 244,
'feature_fraction': 0.4,
'bagging_fraction': 0.6165715549446614,
'bagging_freq': 6,
'min_child_samples': 100
}
lgb_params["learning_rate"] = 0.001
lgb_params["early_stopping_round"] = 1000
lgb_params["num_iterations"] = 20000
train_X, val_X, train_Y, val_Y = train_test_split(train2, FA, test_size = 0.001,
random_state = 3)
 print(train_X.shape, val_X.shape, train_Y.shape, val_Y.shape )<compute_test_metric>
folds_average_lgbm = FoldsAverageLGBM(folds )
def rmse(pred, actual):
assert(len(pred)== len(actual))
return np.sqrt(((pred - actual)**2 ).sum() /len(pred))<train_model>
folds_average_lgbm.fit(lgb_params, train_x, train_y )
lgbmObj = lgbm.LGBMRegressor(n_estimators=2000,
learning_rate = 0.03,
subsample=0.98,
subsample_freq=3,
reg_lambda = 0.0,
max_depth=7,
random_state=42)
 lgbmObj.fit(train_X, train_Y,eval_set=[(train_X,train_Y),(val_X,val_Y)],
eval_metric='rmse',verbose=False )<compute_test_metric>
np.sqrt(mean_squared_error(df_train.target, folds_average_lgbm.oof_preds))
print("Training Error = ", rmse(lgbmObj.predict(train_X), train_Y))
print("Validation Error = ", rmse(lgbmObj.predict(val_X), val_Y))<load_from_csv>
y_pred = folds_average_lgbm.predict(test_x )
test = pd.read_csv(".. /input/test.csv")
 print(test.shape )<count_missing_values>
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_optuna_lgbm_ohe_v2.csv", index=False)
 sub.head()