# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Import packages

# +
from keras.optimizers import Adam
from keras.models import Sequential, load_model
from keras.layers import Dense, AlphaDropout, BatchNormalization
from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.preprocessing import Normalizer, QuantileTransformer
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.metrics import classification_report
from time import time
import pandas as pd


# -

# ### Helper function to get and process the data

def dataget(train_path, test_path):

    train_data = pd.read_csv(train_path)
    test_data = pd.read_csv(test_path)
    #Join the train and test data to cleanse and enhance the data
    df = train_data.append(test_data, ignore_index=True)
    Titles_Dictionary = {
                        "Capt":         "Officer",
                        "Col":          "Officer",
                        "Major":        "Officer",
                        "Jonkheer":     "Royalty",
                        "Don":          "Royalty",
                        "Sir":          "Royalty",
                        "Dr":           "Officer",
                        "Rev":          "Officer",
                        "the Countess": "Royalty",
                        "Dona":         "Royalty",
                        "Mme":          "Mrs",
                        "Mlle":         "Miss",
                        "Ms":           "Mrs",
                        "Mr":           "Mr",
                        "Mrs":          "Mrs",
                        "Miss":         "Miss",
                        "Master":       "Master",
                        "Lady":         "Royalty"
                        }
    ## Extract Title and map to the Titles from each Name
    df['Title'] = df['Name'].apply(lambda x: Titles_Dictionary[x.split(',')[1].split('.')[0].strip()])
    ## Fill missing Embarked with 'C'
    df['Embarked'].fillna('C', inplace=True)
    ## Note down the Imputed Ages
    df['Imputed'] = df['Age'].isnull().astype('uint8')
    columns = ['Age','Fare']
    groups = ['Title', 'Embarked']
    ## Fill null Ages with the mean Age based on Title, Embarked
    df[columns] = df.groupby(groups)[columns].transform(lambda x: x.fillna(x.mean()))
    ## Convert to categorical data
    categories = ['Title', 'Sex', 'Pclass', 'SibSp', 'Parch', 'Embarked']
    df[categories] = df[categories].apply(lambda x: x.astype('category'))
    df = df.drop(columns=['Cabin', 'Name', 'Ticket'])
    #df = df.drop(columns=['Title', 'SibSp', 'Imputed', 'Pclass', 'Parch', 'Embarked', 'Fare'])
    df = df.round(2)
    original = df.copy()
    df = pd.get_dummies(df, drop_first=True)
    test_data = df[df.Survived.isnull()].copy()
    test_data = test_data.drop(columns=['Survived'])
    train_data = df.dropna().copy()
    train_data['Survived'] = train_data['Survived'].astype('uint8')
    train_data = train_data.drop(columns=['PassengerId'])

    return original, train_data, test_data


# ## Neural Network

def build_model(optimizer=Adam(amsgrad=True),
                total_features=1,
                activation='elu',
                units=1,
                dropout_value=0.3,
                multi_layer=True,
                op_activation='sigmoid',
                loadprevmodel=False,
                modelname='Titanic-Kaggle-best'
               ):
    if loadprevmodel:
        try:
            model = load_model(modelname + '.h5')
            print('Model loaded successfully')
        except IOError:
            print('Loading previous model failed, Building a new model')       
    model = Sequential()
    model.add(Dense(input_dim=total_features, activation=activation, units=units))
    if activation == 'selu':
        model.add(AlphaDropout(dropout_value))
    else:
        model.add(BatchNormalization())
    if multi_layer:
        model.add(Dense(activation=activation, units=max(3,int(units/2))))
        if activation == 'selu':
            model.add(AlphaDropout(min(dropout_value, dropout_value * 0.9)))
        else:
            model.add(BatchNormalization())
    model.add(Dense(units=1, activation=op_activation))
    model.compile(optimizer=optimizer,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model


train_path = 'train.csv'
test_path = 'test.csv'
original, train_data, test_data = dataget(train_path, test_path)
df = original.copy()
df.drop(columns=['PassengerId'], inplace=True)
#df.dropna(inplace=True)
print(df.head(10))

print(df.describe())

print(df.info())

search = True
modelh5 = 'Titanic-Kaggle'
loadmodelh5 = 'Titanic-Kaggle-best'
batch_size = 891
epochs = 300
normalizer = Normalizer(norm='l1')
df = train_data
train = df.dropna()
y_train = train['Survived'].values
x_train = train.drop(columns=['Survived']).values
quantile_transformer = QuantileTransformer(output_distribution='normal')
X_train = normalizer.fit_transform(x_train)
#X_train = quantile_transformer.fit_transform(x_train)

# +
clf = KerasClassifier(build_model,
                      total_features=X_train.shape[1],
                      units=5,
                      batch_size=batch_size,
                      epochs=epochs,
                      verbose=0
                     )
dr = [element/100 for element in range(1,50,5)]
param_dist = {'units': list(range(int(X_train.shape[1]/2), int(X_train.shape[1] * 2))),
              'dropout_value': dr,
              'activation': ['elu', 'relu', 'selu'],
              'multi_layer': [True,False]
}
n_iter_search = 2

if search:
    random_search = RandomizedSearchCV(clf, param_distributions=param_dist, n_jobs =1,
                                       n_iter=n_iter_search, scoring='accuracy', random_state=42)

    start = time()
    random_search.fit(X_train, y_train)

    print("RandomizedSearchCV took %.2f seconds for %d candidates"
          " parameter settings." % ((time() - start), n_iter_search))

    print(random_search.best_score_)
    print(random_search.best_params_)

# +
#http://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#sphx-glr-auto-examples-preprocessing-plot-all-scaling-py
# -

#params = {'units': 20, 'multi_layer': False, 'dropout_value': 0.31, 'activation': 'relu'} quantile
params = {'units': 20, 'multi_layer': False, 'dropout_value': 0.31, 'activation': 'relu'}
clf = KerasClassifier(build_model,
                      total_features=X_train.shape[1],
                      batch_size=batch_size,
                      epochs=1500,
                      verbose=0,
                     **params)
clf.fit(X_train, y_train)
y_true, y_pred = y_train, clf.predict(X_train)

print(classification_report(y_true, y_pred))

# +
df = test_data
resultdf = pd.DataFrame(data=df['PassengerId'])
df = df.drop(columns=['PassengerId'])
#test_x = quantile_transformer.transform(df)
test_x = normalizer.transform(df)
predictions = clf.predict(test_x)
resultdf['Survived'] = predictions.astype(int)

resultdf.to_csv('submission.csv', index=False)

StarcoderdataJupyterScriptsDedupFiltered