Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_output>
%matplotlib inline
sns.set(style="ticks", color_codes=True)
edu_order = [15, 11, 5, 12, 10, 1, 14, 7, 2, 8, 4, 13, 0, 3, 6, 9]
args = [base['education'].unique() , edu_order]
education_classes = pd.DataFrame({'education.classes': base['education'].apply(catg, args = args)} )<define_variables>
seed = 7
np.random.seed(seed )
median = np.median(base[base['capital.gain'] > 0]['capital.gain'])
 aux = pd.cut(base['capital.gain'],
bins = [-1, 0, median, base['capital.gain'].max() +1],
labels = [0, 1, 2])
 capital_gain_clusters = pd.DataFrame({'capital.gain.clusters': aux})
 capital_gain_clusters = capital_gain_clusters.astype(np.int)
 median = np.median(base[base['capital.loss'] > 0]['capital.loss'])
 aux = pd.cut(base['capital.loss'],
bins = [-1, 0, median, base['capital.loss'].max() +1],
labels = [0, 1, 2])
 capital_loss_clusters = pd.DataFrame({'capital.loss.clusters': aux})
 capital_loss_clusters = capital_loss_clusters.astype(np.int )<concatenate>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train_copy = train.copy()
test_copy = test.copy()
all_data = pd.concat([train, test], sort = False)
 train.head()
new_data = pd.concat([sul_global, private, education_classes,
hours_per_week_clusters, capital_gain_clusters,
capital_loss_clusters], axis = 1 )<create_dataframe>
train.groupby('Pclass' ).Survived.mean()
aux = base['income'].apply(equals, args = [['>50K']])
 aux = pd.concat([new_data, pd.DataFrame({'income': aux})], axis = 1)
 new = aux.astype(np.int)
 aux.head()<drop_column>
train.groupby('Sex' ).Survived.mean()
base = base.drop(['fnlwgt', 'education', 'sex', 'native.country', 'workclass', 'marital.status'], axis = 1)
 base.columns<concatenate>
train.groupby(['SibSp'] ).Survived.mean()
base = pd.concat([new_data, base], axis = 1 )<categorify>
train.groupby(['Parch'] ).Survived.mean()
names = ['occupation', 'relationship', 'race']
enc_x = []
for i in range(len(names)) :
enc_x.append(prep.LabelEncoder())
 enc_y = prep.LabelEncoder()<categorify>
all_data['Title'] = all_data['Name'].str.extract('([A-Za-z]+)\.', expand=True )
i = 0
for name in names:
base[name] = enc_x[i].fit_transform(base[name])
 i += 1
base['income'] = enc_y.fit_transform(base['income'] )<drop_column>
all_data['Title'].value_counts()
unselected_columns = []
unselected_columns.append('capital.loss')
 unselected_columns.append('capital.gain')
 unselected_columns.append('sul.global')
 unselected_columns.append('private')
 unselected_columns.append('education.classes')
 unselected_columns.append('hours.per.week.clusters')
 base = base.drop(unselected_columns, axis = 1)
 base.head()<data_type_conversions>
mappings = {'Dr':'Respected_Male', 'Col':'Respected_Male', 'Major':'Respected_Male', 'Capt':'Respected_Male',
'Mme':'Noble', 'Mlle':'Noble', 'Countess': 'Noble', 'Lady': 'Noble', 'Sir':'Noble',
'Ms' : 'Miss', 'Rev': 'Other', 'Jonkheer': 'Other', 'Dona': 'Other', 'Don': 'Other'
}
all_data.replace({'Title': mappings}, inplace = True )
aux = base.astype(np.int )<import_modules>
all_data['Title'].value_counts()
from sklearn.preprocessing import StandardScaler<prepare_x_and_y>
one_hot_encoding_list = []
one_hot_encoding_list.append('Title' )
X = base.drop(['income'], axis = 1)
 y = base['income']<normalization>
all_data['Age'].isnull().sum()
scaler_x = StandardScaler()
X = scaler_x.fit_transform(X )<import_modules>
title_grouped = all_data.groupby(['Title'])
 for title in all_data.Title.unique() :
all_data.loc[(all_data.Age.isnull())&(all_data.Title == title), 'Age'] = title_grouped.get_group(title ).Age.mean()
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score<find_best_params>
all_data['AgeBin'] = pd.qcut(all_data['Age'], 5 )
scores_mean = []
scores_std = []
k_lim_inf = 1
k_lim_sup = 30
folds = 5
k_max = None
max_std = 0
max_acc = 0
i = 0
print('Finding best k...')
 for k in range(k_lim_inf, k_lim_sup):
KNNclf = KNeighborsClassifier(n_neighbors=k, p = 1)
 score = cross_val_score(KNNclf, X, y, cv = folds)
 scores_mean.append(score.mean())
 scores_std.append(score.std())
 if scores_mean[i] > max_acc:
k_max = k
max_acc = scores_mean[i]
max_std = scores_std[i]
i += 1
if not(k%3):
print(' K = {0} | Best CV acc = {1:2.2f}% +/-{3:4.2f}%(best k = {2})'.format(k, max_acc*100, k_max, max_std*100))
print('
Best k: {}'.format(k_max))<train_model>
label = preprocessing.LabelEncoder()
all_data['AgeBin'] = label.fit_transform(all_data['AgeBin'])
 cat_plot('AgeBin', data = all_data )
k = k_max
KNNclf = KNeighborsClassifier(n_neighbors=k, p = 1)
 KNNclf.fit(X, y )<load_from_csv>
all_data['FamilySize'] = all_data['SibSp'] + all_data['Parch'] + 1
df_test = pd.read_csv('.. /input/adultb/test_data.csv', na_values='?')
 df_test.set_index('Id', inplace = True)
 df_test.head()<concatenate>
all_data['FamilySize'].value_counts(dropna = False )
features = ['age', 'education.num', 'occupation', 'relationship', 'race', 'hours.per.week']
base_test = pd.concat([new_data, df_test[features]], axis = 1 )<sort_values>
all_data['Alone'] = 0;
all_data.loc[all_data['FamilySize'] == 1, 'Alone'] = 1
all_data['SmallFamily'] = 0;
all_data.loc[(all_data['FamilySize'] > 1)&(all_data['FamilySize'] <= 4), 'SmallFamily'] = 1
all_data['BigFamily'] = 0;
all_data.loc[all_data['FamilySize'] > 4, 'BigFamily'] = 1
total = base_test.isnull().sum().sort_values(ascending = False)
 percent =(( base_test.isnull().sum() /base_test.isnull().count())*100 ).sort_values(ascending = False)
 missing_data = pd.concat([total, percent], axis = 1, keys = ['Total', '%'])
 missing_data.head()<sort_values>
all_data.Cabin.isnull().sum()
total = base_test.isnull().sum().sort_values(ascending = False)
 percent =(( base_test.isnull().sum() /base_test.isnull().count())*100 ).sort_values(ascending = False)
 missing_data = pd.concat([total, percent], axis = 1, keys = ['Total', '%'])
 missing_data.head()<categorify>
all_data['HaveCabin'] = 0;
all_data.loc[all_data['Cabin'].notnull() , 'HaveCabin'] = 1
names = ['occupation', 'relationship', 'race']
i = 0
for name in names:
base_test[name] = enc_x[i].transform(base_test[name])
 i += 1<normalization>
all_data['AnyoneSurvivedFromGroup'] = 0.5
for _, grp_df in all_data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['AnyoneSurvivedFromGroup'] == 0)|(row['AnyoneSurvivedFromGroup']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
all_data.loc[all_data['PassengerId'] == passID, 'AnyoneSurvivedFromGroup'] = 1
elif(smin == 0.0):
all_data.loc[all_data['PassengerId'] == passID, 'AnyoneSurvivedFromGroup'] = 0
X_prev = scaler_x.transform(base_test.values )<predict_on_test>
all_data[all_data['AnyoneSurvivedFromGroup'] != 0.5]['PassengerId'].count()
temp = KNNclf.predict(X_prev)
 temp = enc_y.inverse_transform(temp)
 temp = {'Income': temp}
predictions = pd.DataFrame(temp )<save_to_csv>
for _, grp_df in all_data.groupby('Cabin'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['AnyoneSurvivedFromGroup'] == 0)|(row['AnyoneSurvivedFromGroup']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
all_data.loc[all_data['PassengerId'] == passID, 'AnyoneSurvivedFromGroup'] = 1
elif(smin == 0.0):
all_data.loc[all_data['PassengerId'] == passID, 'AnyoneSurvivedFromGroup'] = 0
predictions.to_csv("submission.csv", index = True, index_label = 'Id' )<import_modules>
all_data[all_data['AnyoneSurvivedFromGroup'] != 0.5]['PassengerId'].count()
import pandas as pd
import sklearn<load_from_csv>
all_data.Fare.isnull().sum()
adult = pd.read_csv(".. /input/adult-db/train_data.csv",header=0, index_col=0, na_values="?" )<count_values>
pclass_grouped = all_data.groupby(['Pclass'])
 for pclass in all_data.Pclass.unique() :
all_data.loc[(all_data.Fare.isnull())&(all_data.Pclass == pclass), 'Fare'] = pclass_grouped.get_group(pclass ).Fare.mean()
adult["native.country"].value_counts()<set_options>
all_data['FareBin'] = pd.qcut(all_data['Fare'], 4)
 label = preprocessing.LabelEncoder()
all_data['FareBin'] = label.fit_transform(all_data['FareBin'] )
%matplotlib inline<count_values>
all_data.isnull().sum()
adult["sex"].value_counts()<categorify>
all_data.loc[all_data['Embarked'].isnull() , 'Embarked'] = all_data['Embarked'].mode() [0]
nadult = adult.dropna()
adult_fill = adult.fillna(-1)
 for col in [1,3,5,6,7,8,9,13]:
nadult = adult.iloc[:,col].dropna()
label_encoder = LabelEncoder().fit(nadult)
 nadult_encoded = label_encoder.transform(nadult)
 adult_fill.iloc[np.where(adult_fill.iloc[:,col].values==-1)[0],col] = label_encoder.inverse_transform([int(mode(nadult_encoded)) ])
 for col in [0,2,4,10,11,12]:
adult_fill.iloc[np.where(adult_fill.iloc[:,col].values==-1)[0],col] = int(np.mean(adult.iloc[:,col].dropna().values))
adult_fill<prepare_x_and_y>
one_hot_encoding_list.append('Embarked' )
minmaxscaler = MinMaxScaler()
col_inds = [0,1,4,5,6,7,8,10,11,12]
Xadult_unscaled = adult_fill.iloc[:,col_inds].apply(LabelEncoder().fit_transform)
 Xadult = minmaxscaler.fit_transform(Xadult_unscaled)
 Yadult = adult_fill.income
print(Xadult_unscaled.columns.values )<find_best_params>
all_data.drop(['PassengerId', 'Name', 'Ticket', 'Cabin', 'Age', 'Fare', 'SibSp', 'Parch', 'FamilySize', 'FareBin'], axis = 1, inplace = True )
score_medio = np.zeros(50)
 std_score = np.zeros(50)
 for i in range(1,51):
knn = KNeighborsClassifier(n_neighbors=i, p=1)
 scores = cross_val_score(knn, Xadult, Yadult, cv=10)
 score_medio[i-1]=np.mean(scores)
 std_score[i-1]=np.std(scores)
 print(np.argmax(score_medio)+1)
 print(np.amax(score_medio))<load_from_csv>
sex_mappings = {'male': 0,'female': 1}
all_data['Sex'].replace(sex_mappings, inplace = True )
testAdult = pd.read_csv(".. /input/adult-db/test_data.csv",header=0, index_col=0, na_values="?")
 testAdult.shape<categorify>
one_hot_encoding_list.append('Pclass' )
testAdult_fill = testAdult.fillna(-1)
 for col in [1,3,5,6,7,8,9,13]:
nTestAdult = testAdult.iloc[:,col].dropna()
label_encoder = LabelEncoder().fit(nTestAdult)
 ntestAdult_encoded = label_encoder.transform(nTestAdult)
 testAdult_fill.iloc[np.where(testAdult_fill.iloc[:,col].values==-1)[0],col] = label_encoder.inverse_transform([int(mode(ntestAdult_encoded)) ])
 for col in [0,2,4,10,11,12]:
testAdult_fill.iloc[np.where(testAdult_fill.iloc[:,col].values==-1)[0],col] = int(np.mean(testAdult.iloc[:,col].dropna().values))
testAdult_fill<categorify>
all_data = pd.get_dummies(data = all_data, columns = one_hot_encoding_list )
XtestAdult_unscaled = testAdult_fill.iloc[:,col_inds].apply(LabelEncoder().fit_transform)
 XtestAdult = minmaxscaler.transform(XtestAdult_unscaled )<train_model>
train_data = all_data[:len(train)]
test_data = all_data[len(train):]
knn = KNeighborsClassifier(n_neighbors=34,p=1)
 knn.fit(Xadult,Yadult )<predict_on_test>
X = train_data.iloc[: , 1:].values
y = train_data.iloc[:, 0].values
test_data = test_data.iloc[: , 1:]
X_test = test_data.values
print(str(X.shape))
print(str(X_test.shape))
YtestAdult = knn.predict(XtestAdult)
 YtestAdult<create_dataframe>
def create_model() :
model = Sequential()
model.add(Dense(16, input_dim = X.shape[1], activation = 'relu', kernel_regularizer=l2(0.01)))
 model.add(Dense(16, activation = 'relu', kernel_regularizer=l2(0.01)))
 model.add(Dense(1, activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 return model
prediction = pd.DataFrame(testAdult.index)
 prediction["income"] = YtestAdult<save_to_csv>
num_epochs = max_val_acc_epoch
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
 cvscores = []
for train, test in kfold.split(X, y):
model = create_model()
history = model.fit(X[train], y[train], epochs=num_epochs, batch_size=10, verbose = 0)
 scores = model.evaluate(X[test], y[test], verbose=0)
 print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
cvscores.append(scores[1] * 100)
 print("%.2f%%(+/- %.2f%%)" %(np.mean(cvscores), np.std(cvscores)) )
prediction.to_csv("adult_prediction_5.csv", index=False )<set_options>
model = create_model()
history = model.fit(X, y, epochs=num_epochs, batch_size=10, verbose = 0 )
%matplotlib inline 
<install_modules>
prediction = model.predict(X_test )
!pip install -q -U tf-hub-nightly
!pip install -q tfds-nightly 
<define_variables>
submission = pd.DataFrame(test_copy[['PassengerId']])
 submission['Survived'] = prediction
submission['Survived'] = submission['Survived'].apply(lambda x: 0 if x < 0.5 else 1 )
<load_from_csv><EOS> 
submission.to_csv('submission.csv', index = False )