Kaggle competition: Categorical Feature Encoding Challenge
store.fillna(0, inplace=True )<concatenate>
train = pd.read_csv('/kaggle/input/cat-in-the-dat/train.csv')
 test = pd.read_csv('/kaggle/input/cat-in-the-dat/test.csv')
 sub = pd.read_csv('/kaggle/input/cat-in-the-dat/sample_submission.csv' )
train.head().append(train.tail() )<count_missing_values>
bin_cols = ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4']
nom_cols = ['nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9']
ord_cols = ['ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5']
cyc_cols = ['day', 'month']
train.isnull().sum()<count_missing_values>
df = train.append(test, ignore_index=True, sort=False)
 df.head(2 )
train.isnull().sum()<count_values>
def date_cyc_enc(df, col, max_vals):
df[col + '_sin'] = np.sin(2 * np.pi * df[col]/max_vals)
 df[col + '_cos'] = np.cos(2 * np.pi * df[col]/max_vals)
 return df
df = date_cyc_enc(df, 'day', 7)
 df = date_cyc_enc(df, 'month', 12)
train.Open.value_counts()<count_values>
df['bin_3'] = df['bin_3'].map({'T':1, 'F':0, 'Y':1, 'N':0})
 df['bin_4'] = df['bin_4'].map({'T':1, 'F':0, 'Y':1, 'N':0} )
np.sum([train['Sales'] == 0] )<count_missing_values>
high_card_feats = ['nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9']
for col in high_card_feats:
df[f'hash_{col}'] = df[col].apply(lambda x: hash(str(x)) % 5000)
 for col in high_card_feats:
enc_nom_1 =(df.groupby(col ).size())/ len(df)
 df[f'freq_{col}'] = df[col].apply(lambda x : enc_nom_1[x] )
test.isnull().sum()<count_missing_values>
for col in df.columns:
if col != 'target' and df[col].dtype == 'O':
df[col] = df[col].astype('category' )
test.isnull().sum()<filter>
from sklearn.metrics import roc_auc_score
test[test.Open.isnull() ]<correct_missing_values>
df.drop(['id'], axis=1, inplace=True)
 train_df = df[df['target'].isnull() !=True]
test_df = df[df['target'].isnull() ==True]
test_df.drop('target', axis=1, inplace=True )
test.fillna(value = 1, inplace = True )<merge>
X = train_df.drop(labels=['target'], axis=1)
 y = train_df['target'].values
X_train, X_cv, y_train, y_cv = train_test_split(X, y, test_size=0.25, random_state=1, stratify=y )
train = pd.merge(train, store, on='Store')
 test = pd.merge(test, store, on='Store')
 train = train.sort_values(['Date'],ascending = False)
 train_total = train.copy()
split_index = 6*7*1115
valid = train[:split_index]
train = train[split_index:]
valid = valid[(valid.Open != 0)&(valid.Sales >0)]
train = train[(train.Open != 0)&(train.Sales >0)]
train_total = train_total[(train_total.Open != 0)&(train_total.Sales >0)]<feature_engineering>
lgbm = LGBMClassifier(boosting_type='gbdt',
num_leaves=31,
max_depth=-1,
learning_rate=0.02,
n_estimators=2000,
subsample_for_bin=200000,
min_child_samples=20,
colsample_bytree=0.1,
random_state=0)
 lgbm.fit(X_train, y_train, eval_set=[(X_cv, y_cv)], eval_metric='auc', early_stopping_rounds=200, verbose=100)
 y_pred = lgbm.predict_proba(X_cv)[:,-1]
def process(data, isTest = False):
mappings = {'0':0, 'a':1, 'b':2, 'c':3, 'd':4}
data.StoreType.replace(mappings, inplace=True)
 data.Assortment.replace(mappings, inplace=True)
 data.StateHoliday.replace(mappings, inplace=True)
 data['Month'] = data.Date.dt.month
data['Year'] = data.Date.dt.year
data['Day'] = data.Date.dt.day
data['WeekOfYear'] = data.Date.dt.weekofyear
data['CompetitionOpen'] = 12 *(data.Year - data.CompetitionOpenSinceYear)+ \ 
(data.Month - data.CompetitionOpenSinceMonth)
 data['CompetitionOpen'] = data['CompetitionOpen'].apply(lambda x: x if x > 0 else 0)
 data['PromoOpen'] = 12 *(data.Year - data.Promo2SinceYear)+ \ 
(data.WeekOfYear - data.Promo2SinceWeek)/ 4.0
data['PromoOpen'] = data['PromoOpen'].apply(lambda x: x if x > 0 else 0)
 month2str = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', \
7:'Jul', 8:'Aug', 9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'}
data['month_str'] = data.Month.map(month2str)
 def check(row):
if isinstance(row['PromoInterval'],str)and row['month_str'] in row['PromoInterval']:
return 1
else:
return 0
data['IsPromoMonth'] = data.apply(lambda row: check(row),axis=1)
 features = ['Store', 'DayOfWeek', 'Promo', 'StateHoliday', 'SchoolHoliday',
'StoreType', 'Assortment', 'CompetitionDistance',
'CompetitionOpenSinceMonth', 'CompetitionOpenSinceYear', 'Promo2',
'Promo2SinceWeek', 'Promo2SinceYear', 'Year', 'Month', 'Day',
'WeekOfYear', 'CompetitionOpen', 'PromoOpen', 'IsPromoMonth']
if not isTest:
features.append('Sales')
 data = data[features]
return data
train = process(train)
 valid = process(valid)
 train_total = process(train_total)
 x_test = process(test,isTest = True )<prepare_x_and_y>
roc_auc_score(y_cv, y_pred )
valid.sort_index(inplace = True)
 train.sort_index(inplace = True)
 train_total.sort_index(inplace = True)
 x_train, y_train = train.drop(columns = ['Sales']), np.log1p(train['Sales'])
 x_valid, y_valid = valid.drop(columns = ['Sales']), np.log1p(valid['Sales'])
 x_train_total, y_train_total = train_total.drop(columns = ['Sales']), np.log1p(train_total['Sales'] )<compute_test_metric>
Xtest = test_df
def rmspe(y, yhat):
return np.sqrt(np.mean(( yhat/y-1)** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y,yhat )<train_model>
err=[]
y_pred_tot=[]
fold=StratifiedKFold(n_splits=10, shuffle=True, random_state=1994)
 i=1
for train_index, test_index in fold.split(X,y):
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y[train_index], y[test_index]
lgbm.fit(X_train, y_train, eval_set=[(X_cv, y_cv)], eval_metric='auc', early_stopping_rounds=100, verbose=100)
 preds = lgbm.predict_proba(X_test)[:,-1]
print("ROC AUC Score: ", roc_auc_score(y_test, preds))
err.append(roc_auc_score(y_test,preds))
p = lgbm.predict_proba(Xtest)[:,-1]
print(f'-------------------- Fold {i} completed !!! ------------------')
 i=i+1
y_pred_tot.append(p )
clf = RandomForestRegressor(n_estimators = 15)
 clf.fit(x_train, y_train)
 y_pred = clf.predict(x_valid)
 error = rmspe(np.expm1(y_valid), np.expm1(y_pred))
print('RMSPE: {:.4f}'.format(error))<train_model>
err_avg = np.mean(err,0)
 err_avg
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.03,
"max_depth": 10,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 10
}
num_boost_round = 4000
dtrain = xgb.DMatrix(x_train, y_train)
 dvalid = xgb.DMatrix(x_valid, y_valid)
 watchlist = [(dtrain, 'train'),(dvalid, 'eval')]
model = xgb.train(params, dtrain, num_boost_round, evals=watchlist, \
early_stopping_rounds= 100, feval=rmspe_xg, verbose_eval=True )<compute_test_metric>
y_pred = np.mean(y_pred_tot,0)
 y_pred
y_pred = model.predict(xgb.DMatrix(x_valid))
error = rmspe(np.expm1(y_valid), np.expm1(y_pred))
print('RMSPE: {:.4f}'.format(error)) 
<concatenate>
sub['target'] = y_pred
x_train_total.head().append(x_train_total.tail() )<train_model>
sub.to_csv('10fold_lgbm.csv', index=False )