Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
!pip install skorch
trainingData = read_csv('.. /input/train.csv')
 trainingData=trainingData[['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age','Outcome']]
testingData = read_csv('.. /input/test.csv')
 testingData=testingData[['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']]
trainingFeatures = trainingData.iloc[:, :-1]
trainingLabels = trainingData.iloc[:, -1]
imputer = SimpleImputer(missing_values=0,strategy='median')
 trainingFeatures = imputer.fit_transform(trainingFeatures)
 trainingFeatures = pd.DataFrame(trainingFeatures)
 trainingFeatures.columns=['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
testingData = imputer.transform(testingData)
 testingData = pd.DataFrame(testingData)
 testingData.columns=['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
print("
print("
Column Name
")
 print(( trainingFeatures[:] == 0 ).sum() )<drop_column>
warnings.filterwarnings('ignore' )
trainingFeatures2 = trainingFeatures.drop(['Id'], axis=1 )<train_on_grid>
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed = 1234
seed_everything(seed )
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
 X_train, X_test, y_train, y_test = train_test_split(trainingFeatures2, trainingLabels, test_size=0.2, random_state=rng)
 first_model = XGBClassifier().fit(X_train, y_train)
 model = GridSearchCV(first_model, {'max_depth': [2,4,6], 'n_estimators': [50,100,200]}, verbose=1)
 model.fit(X_train,y_train)
 predictions = model.predict(X_test)
 actuals = y_test
print(confusion_matrix(actuals, predictions)) 
<create_dataframe>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )
Feature Importance:
',fullList,'
' )<compute_train_metric>
def title_extract(df):
df['Title'] = df['Name'].str.extract('([a-zA-Z]+)\.')
 df['Title'] = df['Title'].apply(lambda x: 'Unknown' if x not in ['Miss','Master','Mr','Mrs', 'Dr', 'Rev'] else x)
 return df
kfold = KFold(n_splits=100, shuffle=True, random_state=rng)
 results = cross_val_score(model, trainingFeatures2, trainingLabels, cv=kfold)
 print("DecisionTreeClassifier:
Cross_Val_Score: %.2f%%(%.2f%%)" %(results.mean() *100, results.std() *100))
prediction = model.predict(X_test)
 cnf_matrix = confusion_matrix(y_test, prediction)
 dict_characters = {0: 'Healthy', 1: 'Diabetes'}
plot_confusion_matrix(cnf_matrix, classes=dict_characters,title='Confusion matrix' )<save_to_csv>
train = title_extract(train)
 test = title_extract(test )
test = testingData
test = pd.DataFrame(test)
 test.columns=['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
test2 = test.drop(['Id'], axis=1)
 my_predictions = model.predict(test2)
 Identifier = test.Id.astype(int)
 my_submission = pd.DataFrame({'Id': Identifier, 'Outcome': my_predictions})
 my_submission.to_csv('submission_mendoza.csv', index=False)
 my_submission.head()<set_options>
def fill_nan_age(df):
df['group_mean_age'] = round(df.groupby(['Sex', 'Title'])['Age'].transform('mean'))
df['Age'].fillna(df['group_mean_age'], inplace=True)
 del df['group_mean_age']
return df
plt.style.use('ggplot')
 %matplotlib inline
os.getcwd()
pd.set_option('display.max_columns', 500)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<set_options>
train = fill_nan_age(train)
 test = fill_nan_age(test )
warnings.filterwarnings('ignore')
 plt.style.use('ggplot')
 %matplotlib inline 
<load_from_csv>
def alone_family(df):
df['Family'] = train['SibSp'] + train['Parch']
df['Alone'] = pd.Series(np.where(df['Family'] == 0, 1, 0))
return df
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" )<load_from_csv>
train = alone_family(train)
 test = alone_family(test )
df_info = pd.read_csv(".. /input/exam-for-students20200129/country_info.csv" )<merge>
train = fare_cat(train)
 test = fare_cat(test )
df_train = df_train.merge(df_info, on=['Country'], how='left')
 df_test = df_test.merge(df_info, on=['Country'], how='left' )<feature_engineering>
def data_clean(drop_col, dummies, df):
df = df.drop(drop_col, axis=1)
 df = pd.get_dummies(data=df, columns=dummies)
 return df
df_train['ConvertedSalary'] = df_train['ConvertedSalary'].apply(np.log1p)
<feature_engineering>
col_drop = ['PassengerId', 'Name', 'Ticket', 'Cabin']
col_dummies = ['Sex', 'Embarked', 'age_cat', 'Fare_cat', 'Pclass', 'Title']
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()<prepare_x_and_y>
train_clean = data_clean(col_drop, col_dummies, train)
 train_clean.head()
y_train=df_train.ConvertedSalary
X_train=df_train.drop(['ConvertedSalary'], axis=1)
 X_test=df_test<count_unique_values>
test_clean = data_clean(col_drop, col_dummies, test)
 test_clean.head()
cats = []
for col in X_train.columns:
if X_train[col].dtype == 'object':
cats.append(col)
 print(col, X_train[col].nunique() )<categorify>
X = train_clean.drop('Survived', axis=1)
 y = train_clean['Survived']
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>
X = np.array(X, dtype='float32')
 y = np.array(y, dtype='float32' )
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>
class TitanicModel(nn.Module):
def __init__(self, neurons=10, dropout=0.2):
super(TitanicModel, self ).__init__()
self.dense0 = nn.Linear(X.shape[1], neurons)
 self.activation0 = nn.ReLU()
self.dropout0 = nn.Dropout(dropout)
 self.dense1 = nn.Linear(neurons, neurons)
 self.activation1 = nn.ReLU()
self.dropout1 = nn.Dropout(dropout)
 self.dense2 = nn.Linear(neurons, 1)
 self.output = nn.Sigmoid()
def forward(self, x):
x = self.dense0(x)
 x = self.activation0(x)
 x = self.dropout0(x)
 x = self.dense1(x)
 x = self.activation1(x)
 x = self.dropout1(x)
 x = self.dense2(x)
 x = self.output(x)
 return x
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>
model = NeuralNetBinaryClassifier(module=TitanicModel,
lr = 0.001,
optimizer__weight_decay = 0.001,
verbose=0,
train_split=False,
device='cuda' )
<categorify>
params = {'batch_size': [10],
'max_epochs': [25],
'optimizer': [torch.optim.Adam],
'lr': [0.01, 0.001],
'criterion': [nn.BCELoss],
'module__neurons': [10, 15, 20, 25],
'module__dropout': [0, 0.2]}
<categorify>
%%time
model_grid = GridSearchCV(estimator=model, param_grid=params,
scoring = 'accuracy', cv=3, verbose=0)
 model_grid = model_grid.fit(X, y )
<categorify>
print(f'Accuracy: {model_grid.best_score_ * 100 :.2f}%')
 print(model_grid.best_params_ )
<categorify>
test_clean = np.array(test_clean, dtype='float32' )
<categorify>
pred = model_grid.predict(test_clean )
<categorify>
submission = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': pred})
 submission.head()
encoder =OrdinalEncoder(cols=cats)
 X_train[cats] = encoder.fit_transform(X_train[cats])
 X_test[cats] = encoder.transform(X_test[cats] )<train_model>
submission.to_csv('submission.csv', index=False )
X_train.fillna(-9999,inplace=True)
 X_test.fillna(-9999,inplace=True )<train_model>
submission.to_csv('submission.csv', index=False )