Kaggle competition: Santander Customer Satisfaction
<SOS> metric: AUC Kaggle data source: santander-customer-satisfaction<define_variables>
%%time
warnings.filterwarnings('ignore' )
age_mean_test = int(( round(real_test_titanic['Age'].mean() ,2)))
 age_sd_test = int(round(real_test_titanic['Age'].std() ,2))
randomvalue = [i for i in range(age_sd_test,age_mean_test)]
for _ in range(real_test_titanic.isnull().sum().Age):
number_to_insert = choice(randomvalue)
 real_test_titanic['Age'].fillna(number_to_insert, inplace = True )<count_missing_values>
%%time
test_data = pd.read_csv('.. /input/test.csv')
 train_data = pd.read_csv('.. /input/train.csv' )
real_test_titanic.isnull().sum()<feature_engineering>
%time 
(train_data.isnull().sum() ).any() > 0
real_test_titanic['Fare'] = real_test_titanic['Fare'].fillna(int(real_test_titanic['Fare'].mean()))<count_missing_values>
train_data.TARGET.value_counts()
real_test_titanic.isnull().sum()<count_values>
%%time
df_train = train_data[:3000]
df_test = train_data[3000:4000]
df_valid = train_data[4000:]
print(df_train.shape, df_test.shape, df_valid.shape )
total_passengers = train_titanic['Sex'].count()
total_males = train_titanic['Sex'].value_counts() ['male']
total_females = train_titanic['Sex'].value_counts() ['female']
survived_males = train_titanic.query('Survived==1')['Sex'].value_counts() ['male']
survived_females = train_titanic.query('Survived==1')['Sex'].value_counts() ['female']
survived_total = survived_males + survived_females
print("=" * 70)
 print("TRAINING SET ANALYSIS BASED ON GENDER")
 print("=" * 70)
 print("Total number of travellers : ",total_passengers)
 print("Percentage of males : ",(100 * total_males /total_passengers))
print("Percentage of females : ",(100 * total_females /total_passengers))
print("=" * 70)
 print("Total number of people survived : ", survived_total)
 print("Total number of survived males : ", survived_males)
 print("Total number of survived females : ", survived_females)
 print("=" * 70)
 print("Percentage of males survived : ", 100 *(survived_males/total_passengers))
print("Percentage of females survived : ", 100 *(survived_females/total_passengers))
print("=" * 70)
 print("Percentage of total people survived : ", 100 *(survived_total/total_passengers))
print("=" * 70)
 print("*" * 70)
 print("=" * 70)
 total_1stclass_passengers = train_titanic['Pclass'].value_counts() [1]
total_2ndclass_passengers = train_titanic['Pclass'].value_counts() [2]
total_3rdclass_passengers = train_titanic['Pclass'].value_counts() [3]
survived_1stclass_passengers = train_titanic.query('Survived==1')['Pclass'].value_counts() [1]
survived_2ndclass_passengers = train_titanic.query('Survived==1')['Pclass'].value_counts() [2]
survived_3rdclass_passengers = train_titanic.query('Survived==1')['Pclass'].value_counts() [3]
print("TRAINING SET ANALYSIS BASED ON PASSENGER CLASS")
 print("=" * 70)
 print("Total number of first class passengers : ", total_1stclass_passengers)
 print("Total number of second class passengers : ", total_2ndclass_passengers)
 print("Total number of third class passengers : ", total_3rdclass_passengers)
 print("=" * 70)
 print("Total number of survived first class passengers : ", survived_1stclass_passengers)
 print("Total number of survived second class passengers : ", survived_2ndclass_passengers)
 print("Total number of survived third class passengers : ", survived_3rdclass_passengers)
 print("Percentage of survived first class passengers : ", 100 * survived_1stclass_passengers/ total_passengers)
 print("Percentage of survived second class passengers : ", 100 * survived_2ndclass_passengers/ total_passengers)
 print("Percentage of survived first class passengers : ", 100 * survived_3rdclass_passengers/ total_passengers)
 print("=" * 70)
<drop_column>
%time
X_train = df_train.drop(['ID', 'TARGET'], axis=1)
 y_train = df_train.TARGET
X_test = df_test.drop(['ID', 'TARGET'], axis=1)
 y_test = df_test.TARGET
X_valid = df_valid.drop(['ID', 'TARGET'], axis=1)
 y_valid = df_valid.TARGET
data_for_sub = test_data.drop(['ID'], axis=1 )
train_titanic = train_titanic.drop(['Ticket', 'Cabin','Name'], axis=1)
 real_test_titanic = real_test_titanic.drop(['Ticket', 'Cabin','Name'], axis=1 )<define_variables>
xgb = xgb.XGBRFRegressor()
tree = ensemble.RandomForestRegressor()
ada = ensemble.AdaBoostRegressor()
grad = ensemble.GradientBoostingRegressor()
train_survived = train_titanic['Survived']<data_type_conversions>
def get_scores_and_params(pipeline, params):
search = RandomizedSearchCV(pipeline,
params, cv=3, n_iter=5, scoring="roc_auc",
n_jobs=-1,
verbose=2)
 search.fit(X_train, y_train)
 return search.best_score_, search.best_params_
train_titanic['Sex'] = train_titanic['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 train_titanic['Embarked'] = train_titanic['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int )<data_type_conversions>
pipelines = [Pipeline([('xgb', xgb)]),
Pipeline([('tree', tree)]),
Pipeline([('ada', ada)]),
Pipeline([('grad', grad)])]
getd = [
{'xgb__max_depth': sp.stats.randint(1, 11),
'xgb__n_estimators': [100, 200, 500, 1000],
'xgb__colsample_bytree': [0.5,0.6,0.7,0.8]}
,{'tree__n_estimators': [100, 200, 500, 1000],
'tree__min_samples_split':[2, 4, 8, 10],
'tree__min_samples_leaf': [1, 2, 3, 4]}
,{'ada__learning_rate': [0.3, 0.4,0.5,0.7,1],
'ada__n_estimators': [10, 50, 100, 500]}
,{'grad__learning_rate': [0.1,0.2,0.5,1],
'grad__max_depth': [3,5,7],
'grad__n_estimators': [1, 2, 3, 4]}
]
warnings.filterwarnings('ignore')
 for i in range(len(pipelines)) :
print(get_scores_and_params(pipelines[i], getd[i]))
real_test_titanic['Sex'] = real_test_titanic['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 real_test_titanic['Embarked'] = real_test_titanic['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int )<drop_column>
features = feat_imp[:40].index
print(features )
train_titanic = train_titanic.set_index('PassengerId')
 real_test_titanic = real_test_titanic.set_index('PassengerId' )<split>
X_train = X_train[features]
X_test = X_test[features]
X_valid = X_valid[features]
X = train_titanic.drop(['Survived'], axis = 1)
 y = train_titanic["Survived"]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.10)
 print("Dimension of Train data :", x_train.shape)
 print("Dimension of Test data :", x_test.shape )<normalization>
X_train
df = df[df.line_race != 0]
scaler = MinMaxScaler()
x_train_scaled = scaler.fit_transform(x_train)
 x_test_scaled = scaler.transform(x_test )<train_model>
clf.fit(X_train, y_train)
 preds = clf.predict(X_test)
 val_pred = clf.predict(X_valid)
 roc_auc_score(y_test, preds), roc_auc_score(y_valid, val_pred )
mlp_model = MLPClassifier(hidden_layer_sizes=(150,150,150),activation ='relu', max_iter=500, alpha=0.0001,
solver='sgd', verbose=10, learning_rate = 'adaptive', momentum=0.9)
 mlp_model.fit(x_train_scaled, y_train)
 y_pred = mlp_model.predict(x_test_scaled)
<compute_test_metric>
pros = clf.predict(data_for_sub[features] )
confusion = confusion_matrix(y_test, y_pred)
 print('Confusion Matrix
', confusion)
 print('Accuracy: {:.2f}'.format(accuracy_score(y_test, y_pred)) )<choose_model_class>
sub = pd.DataFrame()
sub['ID'] = test_data['ID']
sub['target'] = pros
sub.to_csv('submission.csv',index=False)
 test = pd.read_csv('submission.csv')
 test.head()