Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<train_on_grid>
root = Path(".. /input" )
rf = RandomForestClassifier(max_features='auto', oob_score=True, random_state=1, n_jobs=-1)
 param_grid = { "criterion" : ["entropy"],
"min_samples_leaf" : [6, 4, 2],
"min_samples_split" : [12],
"n_estimators": [825, 800, 775, 750]}
gs = GridSearchCV(estimator=rf, param_grid=param_grid, scoring='accuracy', cv=3, n_jobs=-1)
 gs = gs.fit(train.iloc[:, 1:], train.iloc[:, 0])
 print(gs.best_params_ )<choose_model_class>
train = pd.read_csv(root.joinpath("train.csv"))
test = pd.read_csv(root.joinpath("test.csv"))
rf = RandomForestClassifier(criterion='entropy',
n_estimators=775,
min_samples_split=12,
min_samples_leaf=2,
max_features='auto',
oob_score=True,
random_state=2,
n_jobs=-1)
 rf.fit(train.iloc[:, 1:], train.iloc[:, 0])
 Y_pred = rf.predict(test)
 print("%.4f" % rf.oob_score_ )<save_to_csv>
train_df = train.copy()
test_df = test.copy()
train_df.drop(columns=["ID_code", "target"], inplace=True)
 test_df.drop(columns=["ID_code"], inplace=True)
 target = train.target
output = pd.DataFrame({'PassengerId': passenger_id, 'Survived': Y_pred})
 output.to_csv('my_submission.csv', index=False )<load_from_csv>
def augment_train(df_train, y_train):
t0 = df_train[y_train == 0].copy()
t1 = df_train[y_train == 1].copy()
i = 0
N = 3
for I in range(0):
for col in df_train.columns:
i = i + 1000
np.random.seed(i)
 np.random.shuffle(t0[col].values)
 np.random.shuffle(t1[col].values)
 df_train = pd.concat([df_train, t0.copy() ])
 df_train = pd.concat([df_train, t1.copy() ])
 y_train = pd.concat([y_train, pd.Series([0] * t0.shape[0]), pd.Series([1] * t1.shape[0])])
 return df_train, y_train
train = pd.read_csv('/kaggle/input/1056lab-brain-cancer-classification/train.csv')
 test = pd.read_csv('/kaggle/input/1056lab-brain-cancer-classification/test.csv')
 train['type'] = train['type'].map({'normal':0, 'ependymoma':1, 'glioblastoma':2, 'medulloblastoma':3, 'pilocytic_astrocytoma':4} )<prepare_x_and_y>
model = CatBoostClassifier(subsample=0.36,
custom_loss='Logloss',
random_strength = 0,
max_depth=3,
eval_metric="AUC",
learning_rate=0.02,
iterations=60000,
bootstrap_type='Bernoulli',
l2_leaf_reg=0.3,
task_type="GPU",
random_seed=432013,
od_type="Iter",
border_count=128
)
Y = train['type'].values
X = train.drop('type',axis=1 ).values<choose_model_class>
def run_cat(model, trt, tst, tar,n_splits=5, plot=False):
kf = KFold(n_splits=n_splits, random_state=432013, shuffle=True)
 oof = np.zeros(len(trt))
feature_importance_df = pd.DataFrame()
y_valid_pred = 0 * tar
y_test_pred = 0
for n_fold,(train_index, valid_index)in enumerate(kf.split(trt, tar)) :
y_train, y_valid = tar.iloc[train_index], tar.iloc[valid_index]
X_train, X_valid = trt.iloc[train_index,:], trt.iloc[valid_index,:]
X_train, y_train = augment_train(X_train, y_train)
 X_train,X_valid = generate_fe(trn=X_train,tst=X_valid)
 _train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 print("Fold ", n_fold)
 fit_model = model.fit(_train,
verbose_eval=1000,
early_stopping_rounds=1000,
eval_set=[_valid],
use_best_model=True,
plot=False, 
)
 pred = fit_model.predict_proba(X_valid)[:,1]
oof[valid_index] = pred
print("auc = ", roc_auc_score(y_valid, pred))
y_valid_pred.iloc[valid_index] = pred
y_test_pred += fit_model.predict_proba(test_fe)[:,1]
y_test_pred /= n_splits
print("average auc:", roc_auc_score(tar, oof))
return y_test_pred, oof
model = lgb.LGBMClassifier()<compute_test_metric>
def generate_fe(trn, tst):
real,syn = detect_test(test_df[features])
 al = pd.concat([trn,tst,test_df.iloc[real]],axis=0)
 for c in features:
trn[c+"_test"]=trn[c].map(al[c].value_counts())
 trn[c+"_test"] = trn[c+"_test"]*trn[c]
tst[c+"_test"]=tst[c].map(al[c].value_counts())
 tst[c+"_test"] = tst[c+"_test"]*tst[c]
return trn, tst
def met_f(y_test,y_pred):
return f1_score(y_test,y_pred,average='micro' )<normalization>
features = [c for c in train_df.columns if c not in ["ID_code","target"]]
selector = SelectFromModel(model, threshold="mean")
 selector.fit(X, Y)
 X_selected = selector.transform(X)
 test_selected = selector.transform(test.values )<import_modules>
def detect_test(test_df):
df_test=test_df.values
unique_count = np.zeros_like(df_test)
 for feature in tqdm(range(df_test.shape[1])) :
_, index_, count_ = np.unique(df_test[:, feature], return_counts=True, return_index=True)
 unique_count[index_[count_ == 1], feature] += 1
real_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)> 0)[:, 0]
synthetic_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)== 0)[:, 0]
return real_samples_indexes,synthetic_samples_indexes
model = lgb.LGBMClassifier()
stratifiedkfold = StratifiedKFold(n_splits=3)
 score_func = {'auc': make_scorer(met_f)}
scores = cross_validate(model, X_selected, Y, cv = stratifiedkfold, scoring=score_func)
 print('auc:', scores['test_auc'])
 print('auc:', scores['test_auc'].mean() )<load_from_csv>
def generate_fe_test(tst):
re,sy = detect_test(tst[features])
 al = pd.concat([train_df,test_df.iloc[re]],axis=0)
 for c in features:
tst[c+"_test"]=tst[c].map(al[c].value_counts())
 tst[c+"_test"] = tst[c+"_test"]*tst[c]
return tst
test_fe = generate_fe_test(test_df[features] )
model = lgb.LGBMClassifier()
model.fit(X_selected,Y)
 p = model.predict(test_selected)
 sample = pd.read_csv('/kaggle/input/1056lab-brain-cancer-classification/sampleSubmission.csv',index_col = 0)
 sample['type'] = p
sample.to_csv('predict_lgbm_sfm.csv',header = True )<feature_engineering>
y_test_pred, oof = run_cat(model,train_df, test_df, target )
<import_modules><EOS> 
submission = pd.read_csv(root.joinpath("sample_submission.csv"))
submission['target'] = y_test_pred
pd.Series(oof ).to_csv("Cat_oof.csv", index = False)
 submission.to_csv('submission_cb_light_0.8999.csv', index=False )