Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
import numpy as np
import pandas as pd
from pandas.plotting import scatter_matrix
import keras
from keras.models import Sequential
from keras.layers import Dense,MaxPooling2D,Flatten,Dropout
from keras.layers.convolutional import Conv2D
from keras import backend
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<data_type_conversions>
input_neurons=10
output_neurons=1
X_train = train.iloc[:, 2:].values.astype('float64')
 y_train = train['target'].values
X_test = test.iloc[:, 1:].values.astype('float64' )<train_model>
np.random.seed(7 )
class GaussianMixtureNB(BaseEstimator, ClassifierMixin):
def __init__(self, n_components=1, reg_covar=1e-06):
self.n_components = n_components
self.reg_covar = reg_covar
def fit(self, X, y):
self.log_prior_ = np.log(np.bincount(y)/ len(y))
shape =(len(self.log_prior_), X.shape[1])
 self.log_pdf_ = [[GaussianMixture(n_components=self.n_components,
reg_covar=self.reg_covar)
.fit(X[y == i, j:j + 1])
.score_samples for j in range(shape[1])]
for i in range(shape[0])]
def predict_proba(self, X):
shape =(len(self.log_prior_), X.shape[1], X.shape[0])
 log_likelihood = np.sum([[self.log_pdf_[i][j](X[:, j:j + 1])
 for j in range(shape[1])]
for i in range(shape[0])], axis=1 ).T
log_joint = self.log_prior_ + log_likelihood
return np.exp(log_joint - logsumexp(log_joint, axis=1, keepdims=True))
def predict(self, X):
return self.predict_proba(X ).argmax(axis=1 )<split>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
i_train, i_valid = next(StratifiedShuffleSplit(n_splits=1 ).split(X_train, y_train))<train_model>
def simplify_ages(df):
df['Age'] = df['Age'].fillna(df.Age.mean())
 bins =(-1, 0, 5, 12, 18, 25, 35, 60, 120)
 group_names = ['Unknown', 'Baby', 'Child', 'Teenager', 'Student', 'Young Adult', 'Adult', 'Senior']
categories = pd.cut(df['Age'], bins, labels=group_names)
 df['Age'] = categories.cat.codes
return df
def simplify_cabins(df):
df['Cabin'] = df['Cabin'].fillna('N')
 df['Cabin'] = df['Cabin'].apply(lambda x: x[0])
 df['Cabin'] = pd.Categorical(df['Cabin'])
 df['Cabin'] = df['Cabin'].cat.codes
return df
def simplify_fares(df):
df['Fare'] = df.Fare.fillna(df.Fare.mean())
 bins =(-1, 0, 8, 15, 31, 1000)
 group_names = ['Unknown', 'First Quartile', 'Second Quartile',
'Third Quartile', 'Fourth Quartile']
categories = pd.cut(df['Fare'], bins, labels=group_names)
 df['Fare'] = categories.cat.codes
return df
def simplify_sex(df):
df['Sex'] = pd.Categorical(df['Sex'])
 df['Sex'] = df['Sex'].cat.codes
return df
def simplify_embarked(df):
df['Embarked'] = df.Embarked.fillna(df.Embarked.mode() [0])
 df['Embarked'] = pd.Categorical(df['Embarked'])
 df['Embarked'] = df['Embarked'].cat.codes + 1
return df
def normalize_titles(df):
df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Title'] = df['Title'].replace('Ms', 'Mrs')
 df['Title'] = df['Title'].replace('Mrs', 'Mrs')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Miss', 'Miss')
 df['Title'] = df['Title'].replace('Master', 'Master')
 df['Title'] = df['Title'].replace('Mr', 'Mr')
 df['Title'] = df['Title'].replace('Capt', 'Officer')
 df['Title'] = df['Title'].replace('Major', 'Officer')
 df['Title'] = df['Title'].replace('Dr', 'Officer')
 df['Title'] = df['Title'].replace('Col', 'Officer')
 df['Title'] = df['Title'].replace('Rev', 'Officer')
 df['Title'] = df['Title'].replace('Jonkheer', 'Royalty')
 df['Title'] = df['Title'].replace('Don', 'Royalty')
 df['Title'] = df['Title'].replace('Dona', 'Royalty')
 df['Title'] = df['Title'].replace('Countess', 'Royalty')
 df['Title'] = df['Title'].replace('Lady', 'Royalty')
 df['Title'] = df['Title'].replace('Sir', 'Royalty')
 return df
def simplify_titles(df):
df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df=normalize_titles(df)
 df['Title'] = pd.Categorical(df['Title'])
 df['Title'] = df['Title'].cat.codes + 1
return df
def simplify_family_size_and_is_alone(df):
df['FamilySize'] = df ['SibSp'] + df['Parch'] + 1
df['IsAlone'] = 1
df.loc[df['FamilySize'] > 1,'IsAlone'] = 0
return df
def simplify_is_child(df):
df['IsChild'] = 0
df.loc[df['Age'] < 18,'IsChild'] = 1
return df
def transform_features(df):
df = simplify_titles(df)
 df= simplify_is_child(df)
 df = simplify_ages(df)
 df = simplify_cabins(df)
 df = simplify_family_size_and_is_alone(df)
 df = simplify_fares(df)
 df = simplify_sex(df)
 df = simplify_embarked(df)
 return df
pipeline = make_pipeline(StandardScaler() ,
GaussianMixtureNB(n_components=10, reg_covar=0.03))
pipeline.fit(X_train[i_train], y_train[i_train])
 print('Training AUC is {}.' 
.format(roc_auc_score(y_train[i_train],
pipeline.predict_proba(X_train[i_train])[:, 1])))
 print('Validation AUC is {}.' 
.format(roc_auc_score(y_train[i_valid],
pipeline.predict_proba(X_train[i_valid])[:, 1])) )<train_model>
train_df=transform_features(train_df)
 test_df=transform_features(test_df )
pipeline.fit(X_train, y_train )<save_to_csv>
xtrain_df = train_df.drop(['PassengerId','Ticket','Survived','Name','Parch','SibSp'], axis=1)
 ytrain_df = train_df['Survived']
xtest_df = test_df.drop(['PassengerId','Ticket','Name','Parch','SibSp'], axis=1 )
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = pipeline.predict_proba(X_test)[:, 1]
submission.to_csv('submission.csv', index=False )<load_from_csv>
model = Sequential()
model.add(Dense(32, input_dim=input_neurons, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(16, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(5, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(output_neurons, activation='sigmoid'))
test = pd.read_csv('.. /input/test.csv')
 train = pd.read_csv('.. /input/train.csv')
<split>
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'] )
features = train.columns.values[2:202]
x_train = train[features].values
y_train = train.target.values
skf = StratifiedKFold(n_splits=10)
 skf.get_n_splits(x_train, y_train )<init_hyperparams>
model.fit(xtrain_df, ytrain_df,epochs=50, batch_size=1,verbose=1 )
random_state = 2567
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : -1,
"num_leaves" : 13,
"learning_rate" : 0.01,
"bagging_freq": 5,
"bagging_fraction" : 0.4,
"feature_fraction" : 0.05,
"min_data_in_leaf": 80,
"min_sum_heassian_in_leaf": 10,
"tree_learner": "serial",
"boost_from_average": "false",
"bagging_seed" : random_state,
"verbosity" : 1,
"seed": random_state
} 
<train_model>
scores = model.evaluate(xtrain_df, ytrain_df)
 print("
%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
oof = np.zeros(len(train))
predictions = np.zeros(len(test))
for train_index, test_index in skf.split(x_train, y_train):
print("TRAIN:", train_index, "TEST:", test_index)
 x_train_cv, x_test_cv = x_train[train_index], x_train[test_index]
y_train_cv, y_test_cv = y_train[train_index], y_train[test_index]
trn_data = lgb.Dataset(x_train_cv, label=y_train_cv)
 val_data = lgb.Dataset(x_test_cv, label=y_test_cv)
 num_round = 10000
clf = lgb.train(lgb_params, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 100)
 oof[test_index] = clf.predict(x_test_cv, num_iteration=clf.best_iteration)
 predictions += clf.predict(test[features], num_iteration=clf.best_iteration)/ 10<save_to_csv>
predictions = model.predict_classes(xtest_df,verbose=0)
 predictions=predictions.flatten()
results = pd.Series(predictions,name="Survived")
 submission = pd.concat([pd.Series(range(892,1310),name = "PassengerId"),results],axis = 1)
 submission.to_csv("titanic_datagen.csv",index=False)
 backend.clear_session()