Kaggle competition: Santander Customer Transaction Prediction
X = X.append(test )<merge>
df_train = pd.read_csv('.. /input/train.csv')
 test_path = '.. /input/test.csv'
df_test = pd.read_csv(test_path)
 df_test.drop(['ID_code'], axis=1, inplace=True)
 df_test = df_test.values
unique_samples = []
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]
print(len(real_samples_indexes))
print(len(synthetic_samples_indexes))
X = X.merge(items[['item_id','item_category_id', 'item_name']], on = 'item_id', how = 'left')
 X = X.merge(shops[['shop_id','city_id','shop_name']], on = 'shop_id', how = 'left' )<merge>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
X = X.merge(itemcat, on = 'item_category_id', how = 'left' )<merge>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 def findmeancounts(bins,values,x):
for i in range(len(bins)) :
if x < bins[i]:
return values[i]
return values[-1]
def magic2() :
feats = ["var_"+str(i)for i in range(200)]
df = pd.concat([df_train,df_test.ix[real_samples_indexes]])
 for feat in feats:
temp = df[feat].value_counts(dropna = True)
 df_train[feat+"vc"] = df_train[feat].map(temp ).map(lambda x:min(10,x)).astype(np.uint8)
 df_test[feat+"vc"] = df_test[feat].map(temp ).map(lambda x:min(10,x)).astype(np.uint8)
 print(feat,temp.shape[0],df_train[feat+"vc"].map(lambda x:int(x>2)).sum() ,df_train[feat+"vc"].map(lambda x:int(x>3)).sum())
 df_train[feat+"sum"] =(( df_train[feat] - df[feat].mean())* df_train[feat+"vc"].map(lambda x:int(x>1)) ).astype(np.float32)
 df_test[feat+"sum"] =(( df_test[feat] - df[feat].mean())* df_test[feat+"vc"].map(lambda x:int(x>1)) ).astype(np.float32)
 df_train[feat+"sum2"] =(( df_train[feat])* df_train[feat+"vc"].map(lambda x:int(x>2)) ).astype(np.float32)
 df_test[feat+"sum2"] =(( df_test[feat])* df_test[feat+"vc"].map(lambda x:int(x>2)) ).astype(np.float32)
 df_train[feat+"sum3"] =(( df_train[feat])* df_train[feat+"vc"].map(lambda x:int(x>4)) ).astype(np.float32)
 df_test[feat+"sum3"] =(( df_test[feat])* df_test[feat+"vc"].map(lambda x:int(x>4)) ).astype(np.float32)
 magic2()
df_train.iloc[:5,:]
X = X.merge(prices, on = ['item_id','shop_id','date_block_num'], how = 'left' )<feature_engineering>
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 featnum = x1.shape[1]//200 - 1
for c in range(200):
np.random.shuffle(ids)
 x1[:,[c] + [200 + featnum * c + idc for idc in range(featnum)]] = x1[ids][:,[c] + [200 + featnum * c + idc for idc in range(featnum)]]
xn.append(x1)
 for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 featnum = x1.shape[1]//200 - 1
for c in range(200):
np.random.shuffle(ids)
 x1[:,[c] + [200 + featnum * c + idc for idc in range(1)]] = x1[ids][:,[c] + [200 + featnum * c + idc for idc in range(1)]]
xs.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
features = [col for col in df_train.columns if col not in ['target', 'ID_code']]
X['month'] = X['date_block_num']%12 + 1<feature_engineering>
print(df_train['target'].value_counts())
 print(df_train.groupby('var_80vc')['target'].value_counts())
 print(df_train.groupby('var_81vc')['target'].value_counts())
 print(df_train.groupby('var_82vc')['target'].value_counts())
X['is_internet'] = X['shop_id'].apply(lambda x: 1 if x in [12,55] else 0 )<feature_engineering>
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : -1,
"num_leaves" : 15,
"learning_rate" : 0.01,
"bagging_freq": 5,
"bagging_fraction" : 0.6,
"feature_fraction" : 0.05,
"min_data_in_leaf": 50,
"min_sum_heassian_in_leaf": 10,
"tree_learner": "serial",
"boost_from_average": "false",
"lambda_l1" : 1.,
"bagging_seed" : 2007,
"verbosity" : 1,
"seed": 2007
}
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2007)
 oof = df_train[['ID_code', 'target']]
oof['predict'] = 0
predictions = np.zeros(( df_test.shape[0],5))
val_aucs = []
feature_importance_df = pd.DataFrame()
features = [col for col in df_train.columns if col not in ['target', 'ID_code']]
X_test = df_test[features].values
for fold,(trn_idx, val_idx)in enumerate(skf.split(df_train, df_train['target'])) :
X_train, y_train = df_train.iloc[trn_idx][features], df_train.iloc[trn_idx]['target']
X_valid, y_valid = df_train.iloc[val_idx][features], df_train.iloc[val_idx]['target']
N = 1
p_valid,yp = 0,0
for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values)
 weights = np.array([0.8] * X_t.shape[0])
 weights[:X_train.shape[0]] = 1.0
print(X_t.shape)
 trn_data = lgb.Dataset(X_t, label=y_t, weight = weights)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(lgb_params,
trn_data,
100000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=5000,
verbose_eval = 1000,
evals_result=evals_result 
)
 p_valid += lgb_clf.predict(X_valid)
 yp += lgb_clf.predict(X_test)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = lgb_clf.feature_importance()
fold_importance_df["fold"] = fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 oof['predict'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions[:,fold] = yp/N
X['target_block_num'] = X['date_block_num']-1
X['target_month'] = X['target_block_num']%12+1<sort_values>
mean_auc = np.mean(val_aucs)
 std_auc = np.std(val_aucs)
 all_auc = roc_auc_score(oof['target'], oof['predict'])
 print("Mean auc: %.9f, std: %.9f.All auc: %.9f." %(mean_auc, std_auc, all_auc))
cols =(feature_importance_df[["feature", "importance"]] 
.groupby("feature")
.mean() 
.sort_values(by="importance", ascending=False)[:1000].index)
 print(cols[:50])
 print(cols[-50:])
 best_features = feature_importance_df.loc[feature_importance_df.feature.isin(cols)]
<feature_engineering><EOS> 
sub_df = pd.DataFrame({"ID_code":df_test["ID_code"].values})
 sub_df["target"] = np.mean(predictions,axis = 1)
 sub_df.to_csv("lgb_submission.csv", index=False )