Kaggle competition: Santander Customer Transaction Prediction
mapping = {'N': 0,
'C': 1,
'B': 2,
'D': 3,
'E': 4,
'A': 5,
'F': 6,
'G': 6,
'T': 6}
all_data['Cabin'] = all_data['Cabin'].map(mapping )<feature_engineering>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )
all_data['title'] = all_data['Name'].apply(lambda x : x.split('.')[0])
 all_data['title'] = all_data['title'].apply(lambda x : x.split(' ')[-1] )<categorify>
def missing_value(data, head=False):
missing = pd.DataFrame(data.isnull().sum() ).rename(columns={0:'total'})
 if head:
return missing.sort_values('total', ascending=False ).head(10)
 else:
return missing.sort_values('total', ascending=False )
mapping = {'Mr': 0,
'Miss': 1,
'Mrs': 2,
'Master': 3,
'Dr': 4,
'Rev': 4,
'Col': 4,
'Mlle': 4,
'Major': 4,
'Ms': 4,
'Mme': 4,
'Lady': 4,
'Countess':4,
'Don': 4,
'Sir': 4,
'Jonkheer': 4,
'Capt': 4,
'Dona': 4}
all_data['title'] = all_data['title'].map(mapping )<feature_engineering>
missing_value(test_data, head=True )
all_data['FamilySize'] = all_data['SibSp'] + all_data['Parch'] + 1<categorify>
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,cross_val_score,StratifiedKFold,GridSearchCV
from sklearn.feature_selection import RFECV
from sklearn.preprocessing import StandardScaler, normalize, MinMaxScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.pipeline import Pipeline
import imblearn as iml
from sklearn.metrics import f1_score,make_scorer, roc_auc_score,roc_curve, confusion_matrix,auc,make_scorer
from sklearn.decomposition import PCA
mapping = {1: 1, 2: 2, 3: 3, 4: 0, 6: 0, 5: 0, 7: 0, 11: 0, 8: 0}
all_data['FamilySize'] = all_data['FamilySize'].map(mapping )<filter>
all_data['is_alone'] = all_data['FamilySize'].apply(lambda x : 1 if x == 1 else 0 )<categorify>
all_data['Age'].fillna(all_data.groupby(['title'])['Age'].transform('median'), inplace = True )<string_transform>
def _add_decomposition(df, decomp, ncomp, flag):
for i in range(1, ncomp+1):
df[flag+"_"+str(i)] = decomp[:,i-1]
def splitAgeColumns(x):
if x <= 16.0:
return 0
elif x > 16.0 and x <= 26.0:
return 1
elif x > 26.0 and x <= 36.0:
return 2
elif x > 36.0 and x <= 46.0:
return 3
else:
return 4<feature_engineering>
all_data['Age'] = all_data['Age'].apply(lambda x : splitAgeColumns(x))<feature_engineering>
idx = features = train_data.columns.values[2:]
for df in [train_data, test_data]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurt(axis=1)
 df['med'] = df[idx].median(axis=1)
 df['var'] = df[idx].var(axis=1)
all_data['calculate_fare'] = all_data['Fare'] /(all_data['Parch'] + all_data['SibSp'] + 1 )<string_transform>
features = train_data.drop(columns=['target','ID_code'] ).columns
for feature in features:
train_data['r2_'+feature] = np.round(train_data[feature], 2)
 test_data['r2_'+feature] = np.round(test_data[feature], 2)
 train_data['r1_'+feature] = np.round(train_data[feature], 1)
 test_data['r1_'+feature] = np.round(test_data[feature], 1 )
def splitFareColumns(x):
if x <= 5.0:
return 0
elif x > 5.0 and x <= 10.0:
return 1
elif x > 10.0 and x <= 15.0:
return 2
elif x > 15.0 and x <= 20.0:
return 3
elif x > 20.0 and x <= 25.0:
return 4
else:
return 5<feature_engineering>
train_data=train_data.iloc[:,202:]
all_data['calculate_fare'] = all_data['calculate_fare'].apply(lambda x : splitFareColumns(x))<string_transform>
test_data=test_data.iloc[:,201:]
def splitSibSpColumns(x):
if x <= 0.0:
return 0
elif x > 0.0 and x <= 1.0:
return 1
else:
return 2
def splitParchColumns(x):
if x <= 0.0:
return 0
elif x > 0.0 and x <= 2.0:
return 1
else:
return 2<feature_engineering>
pipeline = Pipeline([('StanderScaler', StandardScaler())])
 train_data = pipeline.fit_transform(train_data)
 test_data = pipeline.transform(test_data )
all_data['SibSp'] = all_data['SibSp'].apply(lambda x : splitSibSpColumns(x))
all_data['Parch'] = all_data['Parch'].apply(lambda x : splitParchColumns(x))<feature_engineering>
train_data = pd.DataFrame(data=train_data,columns=features[200:])
 test_data = pd.DataFrame(data=test_data,columns=features[200:] )
data = [train, test]
for dataset in data:
dataset['pclass_fare'] = dataset['Fare'] * dataset['Pclass']<count_missing_values>
percent_null_value(train )<drop_column>
df = all_data.drop(['Name', 'Ticket', 'Survived', 'Fare'], axis = 1 )<categorify>
import lightgbm as lgbm
mapping = {'male' : 0, 'female' : 1}
df['Sex'] = df['Sex'].map(mapping )<categorify>
def lightGBM(train,target,test, n_folds):
params = {
'boosting_type':'gbdt',
'boost': 'gbdt',
'objective':'binary',
'learning_rate':0.008,
'metric':'auc',
'max_depth':2,
'num_leaves':13,
"bagging_fraction" : 0.4,
"feature_fraction" : 1.0,
"min_child_samples":80,
"bagging_freq" : 5,
"bagging_seed" : 2020,
"verbosity" : -1,
"random_state" : 2020,
'early_stopping_round':5500,
'min_data_in_leaf':80,
'boost_from_average':False,
'seed':2020,
'tree_learner': 'serial'
}
skfold = StratifiedKFold(n_splits=n_folds, random_state=42)
 train_pred = np.zeros(len(train))
predictions = np.zeros(len(test))
for fold_,(xidx, validx)in enumerate(skfold.split(train_data,target)) :
print("Fold idx:{}".format(fold_ + 1))
lgbmTrain = lgbm.Dataset(train.iloc[xidx],label=target.iloc[xidx])
 lgbmVal = lgbm.Dataset(train.iloc[validx],label=target.iloc[validx])
 evals_result = {}
model = lgbm.train(params,lgbmTrain,120000,valid_sets=[lgbmTrain,lgbmVal], evals_result=evals_result)
 train_pred[validx] = model.predict(train.iloc[validx], num_iteration=model.best_iteration)
 predictions += model.predict(test, num_iteration=model.best_iteration)/skfold.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, train_pred)))
 return predictions, model, evals_result
def getOneHotEncode(df, col):
cat = pd.get_dummies(df[col], prefix = col)
 df = pd.concat([df, cat], axis = 1)
 df.drop([col], axis = 1, inplace = True)
 return df<define_variables>
prediction, model, eval_result = lightGBM(train_data,Target, test_data,5 )
one_hot_columns = ['Pclass', 'Cabin', 'Embarked', 'title']<sort_values>
model.best_score
df = df.sort_values('PassengerId' )<categorify>
submitLGB = pd.DataFrame({'ID_code':test_ids, 'target':prediction})
 submitLGB.to_csv('LightGBM.csv', index = False)
 submitLGB.head()