Kaggle competition: Santander Customer Transaction Prediction
for feature in categorical:
le=LabelEncoder()
print(feature)
 X_train_categorical[feature]=le.fit_transform(X_train_categorical[feature])
 X_test_categorical[feature]=le.transform(X_test_categorical[feature] )<prepare_x_and_y>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 features = [x for x in train_df.columns if x.startswith("var")]
X_train_numeric=train[numeric]
X_test_numeric=test_df[numeric]<concatenate>
for var in features:
if np.corrcoef(train_df['target'], train_df[var])[1][0] < 0:
train_df[var] = train_df[var] * -1
test_df[var] = test_df[var] * -1
label_cat_not_num_train=pd.concat([X_train_categorical,X_train_numeric],axis=1)
 label_cat_not_num_train.head().T<concatenate>
var_stats = {}
for var in features:
var_stats[var] = train_df[var].append(test_df[ind][var] ).value_counts()
label_cat_not_num_test=pd.concat([X_test_categorical,X_test_numeric],axis=1)
 label_cat_not_num_test.head().T<data_type_conversions>
def logit(p):
return np.log(p)- np.log(1 - p)
 def var_to_feat(vr, var_stats, feat_id):
new_df = pd.DataFrame()
new_df["var"] = vr.values
new_df["hist"] = pd.Series(vr ).map(var_stats)
 new_df["feature_id"] = feat_id
new_df["var_rank"] = new_df["var"].rank() /200000.
 return new_df.values
def downcast_type(df):
for feature in categorical:
df[feature]=df[feature].astype(np.int8 )<drop_column>
TARGET = np.array(list(train_df['target'].values)* 200)
 TRAIN = []
var_mean = {}
var_var = {}
for var in features:
tmp = var_to_feat(train_df[var], var_stats[var], int(var[4:]))
var_mean[var] = np.mean(tmp[:,0])
 var_var[var] = np.var(tmp[:,0])
 tmp[:,0] =(tmp[:,0]-var_mean[var])/var_var[var]
TRAIN.append(tmp)
 TRAIN = np.vstack(TRAIN)
 del train_df
_=gc.collect()
print(TRAIN.shape, len(TARGET))
X_train=label_cat_not_num_train[label_cat_not_num_train.date_block_num<=19]
X_val=label_cat_not_num_train[label_cat_not_num_train.date_block_num>19]
X_train=X_train.drop(["date_block_num"],axis=1)
 X_val=X_val.drop(["date_block_num"],axis=1 )<prepare_x_and_y>
model = lgb.LGBMClassifier(**{
'learning_rate': 0.04,
'num_leaves': 31,
'max_bin': 1023,
'min_child_samples': 1000,
'reg_alpha': 0.1,
'reg_lambda': 0.2,
'feature_fraction': 1.0,
'bagging_freq': 1,
'bagging_fraction': 0.85,
'objective': 'binary',
'n_jobs': -1,
'n_estimators':200,})
 MODELS = []
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=11111)
 for fold_,(train_indexes, valid_indexes)in enumerate(skf.split(TRAIN, TARGET)) :
print('Fold:', fold_)
 model = model.fit(TRAIN[train_indexes], TARGET[train_indexes],
eval_set =(TRAIN[valid_indexes], TARGET[valid_indexes]),
verbose = 10,
eval_metric='auc',
early_stopping_rounds=25,
categorical_feature = [2])
 MODELS.append(model)
 del TRAIN, TARGET
_=gc.collect()
y_train=X_train["item_cnt_day"]
y_val=X_val["item_cnt_day"]
X_val=X_val.drop(["item_cnt_day"],axis=1)
 X_train=X_train.drop(["item_cnt_day"],axis=1 )<compute_test_metric>
ypred = np.zeros(( 200000,200))
for feat,var in enumerate(features):
tmp = var_to_feat(test_df[var], var_stats[var], int(var[4:]))
tmp[:,0] =(tmp[:,0]-var_mean[var])/var_var[var]
for model_id in range(10):
model = MODELS[model_id]
ypred[:,feat] += model.predict_proba(tmp)[:,1] / 10.
 ypred = np.mean(logit(ypred), axis=1)
 sub = test_df[['ID_code']]
sub['target'] = ypred
sub['target'] = sub['target'].rank() / 200000.
 sub.to_csv('sample_submission1.csv', index=False)