Kaggle competition: Titanic - Machine Learning from Disaster
<predict_on_test>
data_train = pd.read_csv('/kaggle/input/titanic/train.csv', sep = ',', header = 0)
 data_test = pd.read_csv('/kaggle/input/titanic/test.csv', sep = ',', header = 0)
 print(data_train)
 data_train.info()
print(data_test)
 data_test.info()
<load_from_csv>
combine1 = [data_train]
for data_train in combine1:
data_train['Salutation'] = data_train.Name.str.extract('([A-Za-z]+ ).', expand=False)
 for data_train in combine1:
data_train['Salutation'] = data_train['Salutation'].replace(['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 data_train['Salutation'] = data_train['Salutation'].replace('Mlle', 'Miss')
 data_train['Salutation'] = data_train['Salutation'].replace('Ms', 'Miss')
 data_train['Salutation'] = data_train['Salutation'].replace('Mme', 'Mrs')
 combine2 = [data_test]
for data_test in combine2:
data_test['Salutation'] = data_test.Name.str.extract('([A-Za-z]+ ).', expand=False)
 for data_test in combine2:
data_test['Salutation'] = data_test['Salutation'].replace(['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 data_test['Salutation'] = data_test['Salutation'].replace('Mlle', 'Miss')
 data_test['Salutation'] = data_test['Salutation'].replace('Ms', 'Miss')
 data_test['Salutation'] = data_test['Salutation'].replace('Mme', 'Mrs')
 Salutation_mapping = {'Mr':1, 'Miss':2, 'Mrs':3, 'Master':4, 'Rare':5}
for data_train in combine1:
data_train['Salutation'] = data_train['Salutation'].map(Salutation_mapping)
 data_train['Salutation'] = data_train['Salutation'].fillna(0)
 for data_test in combine2:
data_test['Salutation'] = data_test['Salutation'].map(Salutation_mapping)
 data_test['Salutation'] = data_test['Salutation'].fillna(0)
 Age_Salutation_dn1 = data_train[['Age', 'Salutation']].dropna()
Age_Salutation_dn2 = data_test[['Age', 'Salutation']].dropna()
Age_Salutation_dn1 = np.array(Age_Salutation_dn1)
 Age_Salutation_dn2 = np.array(Age_Salutation_dn2)
 t_age_Mr, t_age_Miss, t_age_Mrs, t_age_Master, t_age_Rare = 0, 0, 0, 0, 0
Mr_count, Miss_count, Mrs_count, Master_count, Rare_count = 0, 0, 0, 0, 0
for i in range(len(Age_Salutation_dn1)) :
age1 = Age_Salutation_dn1[i][0]
if Age_Salutation_dn1[i][1] == 1:
t_age_Mr += age1
Mr_count += 1
elif Age_Salutation_dn1[i][1] == 2:
t_age_Miss += age1
Miss_count += 1
elif Age_Salutation_dn1[i][1] == 3:
t_age_Mrs += age1
Mrs_count += 1
elif Age_Salutation_dn1[i][1] == 4:
t_age_Master += age1
Master_count += 1
else:
t_age_Rare += age1
Rare_count += 1
for i in range(len(Age_Salutation_dn2)) :
age2 = Age_Salutation_dn2[i][0]
if Age_Salutation_dn2[i][1] == 1:
t_age_Mr += age2
Mr_count += 1
elif Age_Salutation_dn2[i][1] == 2:
t_age_Miss += age2
Miss_count += 1
elif Age_Salutation_dn2[i][1] == 3:
t_age_Mrs += age2
Mrs_count += 1
elif Age_Salutation_dn2[i][1] == 4:
t_age_Master += age2
Master_count += 1
else:
t_age_Rare += age2
Rare_count += 1
m_age_Mr = t_age_Mr / Mr_count
m_age_Miss = t_age_Miss / Miss_count
m_age_Mrs = t_age_Mrs / Mrs_count
m_age_Master = t_age_Master / Master_count
m_age_Rare = t_age_Rare / Rare_count
print('Mr:', m_age_Mr)
 print('Miss:', m_age_Miss)
 print('Mrs:', m_age_Mrs)
 print('Master:', m_age_Master)
 print('Rare:', m_age_Rare )
sample_submission = pd.read_csv(".. /input/exam-for-students20200129/sample_submission.csv", index_col=0)
 df_test = pd.read_csv(".. /input/exam-for-students20200129/test.csv")
 df_train = pd.read_csv(".. /input/exam-for-students20200129/train.csv" )<feature_engineering>
data_set = DataFrame(data_train.drop(['PassengerId', 'Name', 'Ticket'], axis=1))
data_set = data_set.fillna({'Fare':0, 'Cabin':0,'Embarked':0})
 data_set = data_set.replace({'male':0, 'female':1, 'S':1, 'C':2, 'Q':3})
 data_set = data_set.replace({'A':1, 'B':2, 'C':3, 'D':4, 'E':5, 'F':6, 'G':7, 'T':8}, regex=True)
 for i in range(len(data_set)) :
salutation = data_set['Salutation'][i]
if salutation == 1:
if pd.isnull(data_set['Age'][i]):
data_set['Age'].loc[i] = m_age_Mr
elif salutation == 2:
if pd.isnull(data_set['Age'][i]):
data_set['Age'].loc[i] = m_age_Miss
elif salutation == 3:
if pd.isnull(data_set['Age'][i]):
data_set['Age'].loc[i] = m_age_Mrs
elif salutation == 4:
if pd.isnull(data_set['Age'][i]):
data_set['Age'].loc[i] = m_age_Master
else:
if pd.isnull(data_set['Age'][i]):
data_set['Age'].loc[i] = m_age_Rare
data_set['FamilySize'] = data_set['SibSp'] + data_set['Parch'] + 1
isAlone = []
FamilySize_M = []
FamilySize_L = []
isCabinNo = []
roundAgeArray = []
roundFareArray = []
for i in range(len(data_set)) :
if data_set['FamilySize'][i] == 1:
isAlone.append(1)
 else:
isAlone.append(0)
 if data_set['Cabin'][i] == 0:
isCabinNo.append(1)
 else:
isCabinNo.append(0)
 if data_set['FamilySize'][i] < 2:
FamilySize_M.append(0)
 FamilySize_L.append(0)
 elif data_set['FamilySize'][i] < 5:
FamilySize_M.append(1)
 FamilySize_L.append(0)
 else:
FamilySize_M.append(0)
 FamilySize_L.append(1)
 roundAge = int(data_set['Age'][i] / 10)
 roundAgeArray.append(roundAge)
 if data_set['Fare'][i] > 0:
roundFare = int(math.log(data_set['Fare'][i]))
else:
roundFare = 0
roundFareArray.append(roundFare)
 data_set['isAlone'] = isAlone
data_set['FamilySize_M'] = FamilySize_M
data_set['FamilySize_L'] = FamilySize_L
data_set['isCabinNo'] = isCabinNo
data_set['roundAge'] = roundAgeArray
data_set['roundFare'] = roundFareArray
data_set.info()
df_train['SalaryType_flg'] = df_train.SalaryType.isnull()
df_test['SalaryType_flg'] = df_test.SalaryType.isnull()
df_train['Country_flg'] = df_train.Country.isnull()
df_test['Country_flg'] = df_test.Country.isnull()
df_train['Employment_flg'] = df_train.Employment.isnull()
df_test['Employment_flg'] = df_test.Employment.isnull()
df_train['Currency_flg'] = df_train.Currency.isnull()
df_test['Currency_flg'] = df_test.Currency.isnull()
df_train['YearsCodingProf'] = df_train.YearsCodingProf.isnull()
df_test['YearsCodingProf'] = df_test.YearsCodingProf.isnull()<feature_engineering>
testData = np.array(data_set)
 testData = testData.astype('int32')
 for i in range(len(testData)) :
print(testData[i])
 data_set = data_set.dropna()
x = DataFrame(data_set[['Pclass', 'Sex', 'Age', 'Salutation', 'FamilySize', 'isAlone']])
 t = DataFrame(data_set['Survived'])
 x = np.array(x)
 t = np.array(t)
 t = t.ravel()
x = x.astype('float32')
 t = t.astype('int32')
 print('x shape:', x.shape)
 print(x[:10])
 print('t shape:', t.shape)
 print(t[:10] )
df_train['ConvertedSalary'] = df_train['ConvertedSalary'].apply(np.log1p)
<prepare_x_and_y>
dataset = TupleDataset(x, t)
 train_val, innerTest = split_dataset_random(dataset, int(len(dataset)* 0.9), seed=0)
 train, valid = split_dataset_random(train_val, int(len(train_val)* 0.7), seed=0)
 train_iter = SerialIterator(train, batch_size=64, repeat=True, shuffle=True)
 print(dataset[0] )
y_train=df_train.ConvertedSalary
X_train=df_train.drop(['ConvertedSalary'], axis=1)
 X_test=df_test<count_unique_values>
class Net(chainer.Chain):
def __init__(self, n_in=6, n_hidden=100, n_out=2):
super().__init__()
with self.init_scope() :
self.l1 = L.Linear(n_in, n_hidden)
 self.l2 = L.Linear(n_hidden, n_hidden)
 self.l3 = L.Linear(n_hidden, n_out)
 def forward(self, x):
h = F.sigmoid(self.l1(x))
h = F.sigmoid(self.l2(h))
h = self.l3(h)
 return h
net = Net()
optimizer = optimizers.Adam(alpha=0.0001, beta1=0.9, beta2=0.999, eps=1e-08, eta=1.0,
weight_decay_rate=0.00001, amsgrad=False, adabound=False, final_lr=0.1, gamma=0.001)
 optimizer.setup(net)
 gpu_id = 0
n_epoch =2000
net.to_gpu(gpu_id)
 results_train, results_valid = {}, {}
results_train['loss'], results_train['accuracy'] = [], []
results_valid['loss'], results_valid['accuracy'] = [], []
train_iter.reset()
count = 1
for epoch in range(n_epoch):
while True:
train_batch = train_iter.next()
x_train, t_train = chainer.dataset.concat_examples(train_batch, gpu_id)
 y_train = net(x_train)
 loss_train = F.softmax_cross_entropy(y_train, t_train)
 acc_train = F.accuracy(y_train, t_train)
 net.cleargrads()
loss_train.backward()
optimizer.update()
count += 1
if train_iter.is_new_epoch:
with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
x_valid, t_valid = chainer.dataset.concat_examples(valid, gpu_id)
 y_valid = net(x_valid)
 loss_valid = F.softmax_cross_entropy(y_valid, t_valid)
 acc_valid = F.accuracy(y_valid, t_valid)
 loss_train.to_cpu()
loss_valid.to_cpu()
acc_train.to_cpu()
acc_valid.to_cpu()
if epoch % 10 == 0:
print('epoch: {}, iteration: {}, loss(train): {:.4f}, loss(valid): {:.4f}, acc(train): {:.4f}, acc(valid): {:.4f}'.format(epoch, count, loss_train.array.mean() , loss_valid.array.mean() , acc_train.array.mean() , acc_valid.array.mean()))
results_train['loss'].append(loss_train.array)
 results_train['accuracy'].append(acc_train.array)
 results_valid['loss'].append(loss_valid.array)
 results_valid['accuracy'].append(acc_valid.array)
 break
cats = []
for col in X_train.columns:
if X_train[col].dtype == 'object':
cats.append(col)
 print(col, X_train[col].nunique() )<categorify>
x_test, t_test = chainer.dataset.concat_examples(innerTest, device=gpu_id)
 with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
y_test = net(x_test)
 loss_test = F.softmax_cross_entropy(y_test, t_test)
 acc_test = F.accuracy(y_test, t_test)
 print('test loss: {:.4f}'.format(loss_test.array.get()))
print('test accuracy: {:.4f}'.format(acc_test.array.get()))
target = 'ConvertedSalary'
col='SalaryType'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 enc_test = enc_test.rename(columns={'purpose': 'purpose_te'})
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train=pd.concat([X_train, enc_train], axis=1)
 X_test=pd.concat([X_test, enc_test], axis=1)
 X_train = X_train.rename(columns={0: 'SalaryType_te'})
 X_test = X_test.rename(columns={0: 'SalaryType_te'} )<categorify>
net.to_cpu()
chainer.serializers.save_npz('net.npz', net)
 !ls
target = 'ConvertedSalary'
col='Country'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 enc_test = enc_test.rename(columns={'purpose': 'purpose_te'})
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train=pd.concat([X_train, enc_train], axis=1)
 X_test=pd.concat([X_test, enc_test], axis=1)
 X_train = X_train.rename(columns={0: 'Country_te'})
 X_test = X_test.rename(columns={0: 'Country_te'} )<categorify>
test = DataFrame(data_test.drop(['PassengerId', 'Name', 'Ticket'], axis=1))
for i in range(len(test)) :
salutation = test['Salutation'][i]
if salutation == 1:
if pd.isnull(test['Age'][i]):
test['Age'].loc[i] = m_age_Mr
elif salutation == 2:
if pd.isnull(test['Age'][i]):
test['Age'].loc[i] = m_age_Miss
elif salutation == 3:
if pd.isnull(test['Age'][i]):
test['Age'].loc[i] = m_age_Mrs
elif salutation == 4:
if pd.isnull(test['Age'][i]):
test['Age'].loc[i] = m_age_Master
else:
if pd.isnull(test['Age'][i]):
test['Age'].loc[i] = m_age_Rare
test = test.fillna({'Fare':0, 'Cabin':0,'Embarked':0})
 test = test.replace({'male':0, 'female':1, 'S':1, 'C':2, 'Q':3})
 test = test.replace({'A':1, 'B':2, 'C':3, 'D':4, 'E':5, 'F':6, 'G':7, 'T':8}, regex=True)
 test['FamilySize'] = test['SibSp'] + test['Parch'] + 1
isAlone = []
FamilySize_M = []
FamilySize_L = []
isCabinNo = []
roundAgeArray = []
roundFareArray = []
for i in range(len(test)) :
if test['FamilySize'][i] == 1:
isAlone.append(1)
 else:
isAlone.append(0)
 if test['Cabin'][i] == 0:
isCabinNo.append(1)
 else:
isCabinNo.append(0)
 if test['FamilySize'][i] < 2:
FamilySize_M.append(0)
 FamilySize_L.append(0)
 elif test['FamilySize'][i] < 5:
FamilySize_M.append(1)
 FamilySize_L.append(0)
 else:
FamilySize_M.append(0)
 FamilySize_L.append(1)
 roundAge = int(test['Age'][i] / 10)
 roundAgeArray.append(roundAge)
 if test['Fare'][i] > 0:
roundFare = int(math.log(test['Fare'][i]))
else:
roundFare = 0
roundFareArray.append(roundFare)
 test['isAlone'] = isAlone
test['FamilySize_M'] = FamilySize_M
test['FamilySize_L'] = FamilySize_L
test['isCabinNo'] = isCabinNo
test['roundAge'] = roundAgeArray
test['roundFare'] = roundFareArray
test = DataFrame(test[['Pclass', 'Sex', 'Age', 'Salutation', 'FamilySize', 'isAlone']])
 test.info()
target = 'ConvertedSalary'
col='Employment'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 enc_test = enc_test.rename(columns={'purpose': 'purpose_te'})
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train=pd.concat([X_train, enc_train], axis=1)
 X_test=pd.concat([X_test, enc_test], axis=1)
 X_train = X_train.rename(columns={0: 'Employment_te'})
 X_test = X_test.rename(columns={0: 'Employment_te'} )<categorify>
class newNet(chainer.Chain):
def __init__(self,n_in=6, n_hidden=100, n_out=2):
super().__init__()
with self.init_scope() :
self.l1 = L.Linear(n_in, n_hidden)
 self.l2 = L.Linear(n_hidden, n_hidden)
 self.l3 = L.Linear(n_hidden, n_out)
 def forward(self, x):
h = F.sigmoid(self.l1(x))
h = F.sigmoid(self.l2(h))
h = self.l3(h)
 return h
loaded_net = newNet()
chainer.serializers.load_npz('net.npz', loaded_net)
 test = np.array(test)
 test = test.astype('float32')
 with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
result = loaded_net(test)
 PassengerId = 891
outputArray = []
for i in range(len(test)) :
PassengerId += 1
predict = np.argmax(result[i,:].array)
 innerArray = [PassengerId, predict]
outputArray.append(innerArray)
 df = pd.DataFrame(outputArray, columns=['PassengerId', 'Survived'])
 df.to_csv(path_or_buf='gender_submission.csv', index=False)
 df_test_list = pd.DataFrame(outputArray)
 df_test_list