<jupyter_start><jupyter_text>Titanic: Machine Learning from Disaster  Import Dependencies<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from sklearn.model_selection import train_test_split
from jupyterthemes import jtplot
import csv
# jtplot.style()
%matplotlib inline

np.random.seed(1)<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis and Data Cleaning<jupyter_code>data = pd.read_csv('train.csv')
# test_data = pd.read_csv('test.csv')
data.head()
#check total null values in each column
print(data.isnull().sum())
# plot of survival
f, ax = plt.subplots(1,figsize=(10,8))
data['Survived'].value_counts().plot.pie(autopct='%1.1f%%',ax=ax);
data['Survived'].value_counts()
# see survival and sex relation
data.groupby(['Sex','Survived'])['Survived'].count().plot(kind='bar');
pd.crosstab(data.Pclass, data.Survived, margins=True)
pd.crosstab([data.Sex, data.Survived], data.Pclass,margins=True)
pd.crosstab(data.SibSp, data.Survived, margins=True)
pd.crosstab(data.Parch, data.Survived, margins=True)
data['Fare_Range'] = pd.qcut(data['Fare'], 4)
data.groupby(['Fare_Range'])['Survived'].mean().to_frame()
data['Fare_cat'] = 0
data.loc[data['Fare'] <= 7.91, 'Fare_cat'] = 0
data.loc[(data['Fare'] > 7.91) & (data['Fare'] <= 14.454), 'Fare_cat'] = 1
data.loc[(data['Fare'] > 14.454) & (data['Fare'] <= 31), 'Fare_cat'] = 2
data.loc[(data['Fare'] > 31) & (data['Fare'] <= 513), 'Fare_cat'] = 3
print('Oldest Passenger was of:',data['Age'].max(),'Years')
print('Youngest Passenger was of:',data['Age'].min(),'Years')
print('Average Age on the ship:',data['Age'].mean(),'Years')
data['Initial'] = data.Name.str.extract('([A-Za-z]+)\.', expand=True)
data.head()
data.groupby('Initial')['Name'].count()
# there are some errors in data, let's fix them
data['Initial'].replace(['Mlle', 'Mme', 'Ms', 'Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],['Miss', 'Miss', 'Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'], inplace=True)
data.groupby('Initial')['Age'].mean()
## Assigning the NaN Values with the Ceil values of the mean ages
data.loc[(data.Age.isnull())&(data.Initial=='Mr'),'Age']=33
data.loc[(data.Age.isnull())&(data.Initial=='Mrs'),'Age']=36
data.loc[(data.Age.isnull())&(data.Initial=='Master'),'Age']=5
data.loc[(data.Age.isnull())&(data.Initial=='Miss'),'Age']=22
data.loc[(data.Age.isnull())&(data.Initial=='Other'),'Age']=46
data.Age.isnull().any() #check for nan values in age
data['Embarked'].fillna('S',inplace=True)
data['Age_band']=0
data.loc[data['Age']<=16,'Age_band']=0
data.loc[(data['Age']>16)&(data['Age']<=32),'Age_band']=1
data.loc[(data['Age']>32)&(data['Age']<=48),'Age_band']=2
data.loc[(data['Age']>48)&(data['Age']<=64),'Age_band']=3
data.loc[data['Age']>64,'Age_band']=4
data['Family_Size'] = data['Parch'] + data['SibSp'] + 1
data['Sex'].replace(['male','female'],[0,1],inplace=True)
data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True)
data['Age_band'].value_counts().to_frame()
data.head(2)<jupyter_output><empty_output><jupyter_text>Predictive Modeling  DNN   Initializing parameters with `he initialization`<jupyter_code>def Initialize_parameters_deep(layer_dims):
    np.random.seed(3)
    parameters = {}
    for l in range(1, len(layer_dims)):
        parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1]) * np.sqrt(2/layer_dims[l-1])
        parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
    return parameters<jupyter_output><empty_output><jupyter_text>Activation Functions<jupyter_code>def sigmoid(Z):
    return 1 / (1 + np.exp(-1 * Z))

def relu(Z):
    return np.maximum(0, Z)<jupyter_output><empty_output><jupyter_text>Forward Propogation<jupyter_code>def linear_activation_forward(A_prev, W, b, activation):
    Z = np.dot(W, A_prev) + b
    linear_cache = (A_prev, W, b)

    if activation == 'sigmoid':
        A = sigmoid(Z)
    elif activation == 'relu':
        A = relu(Z)
        
    activation_cache = Z
    cache = (linear_cache, activation_cache)
    return A, cache
def forward_propogation(X, parameters):
    
    A_prev = X
    L = len(parameters)//2
    caches = []
    for l in range(1, L):
        Wl = parameters['W' + str(l)]
        bl = parameters['b' + str(l)]
        A_prev, cache = linear_activation_forward(A_prev, Wl, bl, 'relu')
        caches.append(cache)

    AL, cache = linear_activation_forward(A_prev, parameters['W' + str(L)], parameters['b' + str(L)], 'sigmoid')
    caches.append(cache)
    return AL, caches<jupyter_output><empty_output><jupyter_text>Cost Computation<jupyter_code>#np.mulliply is diff than X*Y

def compute_cost(AL, Y): #cross_entropy_cost
    m = Y.shape[1]
    cost = -1 / m * np.sum((Y * np.log(AL) + ((1 - Y) * np.log(1 - AL))))
    cost = np.squeeze(cost)
    return cost

def compute_cost_with_regularization(AL, Y, parameters, lambd):
    m = Y.shape[1]
    cost = 0
    for l in range(1, len(parameters)//2):
        cost += np.sum(np.square(parameters['W' + str(l)]))

    cross_entropy_cost = compute_cost(AL, Y)
    L2_regularization_cost = lambd * cost / (2 * m)

    return  cross_entropy_cost + L2_regularization_cost<jupyter_output><empty_output><jupyter_text>Backward Propogation<jupyter_code>def sigmoid_backward(dA, activation_cache):
    Z = activation_cache
    A = sigmoid(Z)
    dZ = dA * A * (1 - A)
    return dZ
def relu_backward(dA, activation_cache):
    Z = activation_cache
    dZ = np.array(dA, copy=True)
    dZ[Z <= 0] = 0
    return dZ
def linear_activation_backward(dA, cache, activation):
    
    linear_cache, activation_cache = cache
    
    if activation == 'sigmoid':
        dZ = sigmoid_backward(dA, activation_cache)
    elif activation == 'relu':
        dZ = relu_backward(dA, activation_cache)
        
    A_prev, W, b = linear_cache
    m = A_prev.shape[1]
    dW = 1 / m * np.dot(dZ, A_prev.T)
    db = 1 / m * np.sum(dZ, axis=1, keepdims=True)
    dA_prev = np.dot(W.T, dZ)

    return dA_prev, dW, db

def backward_propogation(AL, Y, caches):
    L = len(caches)
    grads = {}
    dAL = - np.divide(Y, AL) + np.divide(1 - Y, 1 - AL)
    grads['dA' + str(L)], grads['dW' + str(L)], grads['db' + str(L)] = linear_activation_backward(dAL, caches[L-1], 'sigmoid')
    A_prev = AL
    for l in range(L-1, 0, -1):
        cache = caches[l-1]
        dA = grads['dA' + str(l+1)]
        dA_prev, dW, db = linear_activation_backward(dA, cache, 'relu')
        grads['dA' + str(l)] = dA_prev
        grads['dW' + str(l)] = dW
        grads['db' + str(l)] = db
        
    return grads
def backward_propogation_with_regularization(AL, Y, caches, lambd):
    L = len(caches)
    grads = {}
    m = Y.shape[1]
    dAL = - np.divide(Y, AL) + np.divide(1 - Y, 1 - AL)
    grads['dA' + str(L)], grads['dW' + str(L)], grads['db' + str(L)] = linear_activation_backward(dAL, caches[L-1], 'sigmoid')
    grads['dW' + str(L)] += (lambd * caches[L-1][0][1]) / m
    A_prev = AL

    for l in range(L-1, 0, -1):
        cache = caches[l-1]
        dA = grads['dA' + str(l+1)]
        dA_prev, dW, db = linear_activation_backward(dA, cache, 'relu')
        grads['dA' + str(l)] = dA_prev
        grads['dW' + str(l)] = dW + (lambd * cache[0][1]) / m
        grads['db' + str(l)] = db
        
    return grads<jupyter_output><empty_output><jupyter_text>Updating parameters<jupyter_code>def update_parameters(parameters, grads, learning_rate):
    for l in range(1, len(parameters)//2 + 1 ):
        parameters['W' + str(l)] -= learning_rate * grads['dW' + str(l)]
        parameters['b' + str(l)] -= learning_rate * grads['db' + str(l)]
    return parameters<jupyter_output><empty_output><jupyter_text>Building the model<jupyter_code>def the_model(X, Y, layers_dims, learning_rate, num_iterations, lambd):
    
    np.random.seed(1)
    costs = []
    
    parameters = Initialize_parameters_deep(layers_dims)
#     parameters = np.load('parameters.npy').item()
    for i in range(num_iterations+1):
        AL, caches = forward_propogation(X, parameters)
        if lambd == 0:
            cost = compute_cost(AL, Y)
            grads = backward_propogation(AL, Y, caches)
        else:
            cost = compute_cost_with_regularization(AL, Y, parameters, lambd)
            grads = backward_propogation_with_regularization(AL, Y, caches, lambd)
            
        parameters = update_parameters(parameters, grads, learning_rate)
        if (i%20000==0):
            print('Cost at iteration %s is %s' %(i, cost))
        if (i%1000==0):
            costs.append(cost)
            
    plt.plot(np.squeeze(costs))
    plt.ylabel('cost')
    plt.xlabel('iterations (per 10000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()
    
    np.save("parameters", parameters)
    return parameters<jupyter_output><empty_output><jupyter_text>Accuracy Prediction function<jupyter_code>def predictAccuracy(X, Y, parameters):
    
    m = X.shape[1]
    p = np.zeros((1, m))
    probas, caches = forward_propogation(X, parameters)
    
    # convert probas to 0/1 predictions
    for i in range(0, probas.shape[1]):
        if probas[0, i] > 0.5:
            p[0, i] = 1
        else:
            p[0, i] = 0
    
    print("Accuracy: "  + str(np.sum((p == Y)) / m))
        
    return np.squeeze(p)<jupyter_output><empty_output><jupyter_text>Training the model  Preparing train and test set<jupyter_code>train, test = train_test_split(data, test_size=0.3,random_state=0,stratify=data['Survived'])

X_train = train[['Pclass', 'Sex', 'Age_band', 'Embarked', 'Initial','Fare_cat', 'Family_Size']].values
X_train = X_train.T.astype(float)
X_test = test[['Pclass', 'Sex', 'Age_band', 'Embarked', 'Initial', 'Fare_cat', 'Family_Size']].values
X_test = X_test.T.astype(float)

Y_train = train['Survived'].values
Y_train = Y_train.reshape(1, Y_train.shape[0])

Y_test = test['Survived'].values
Y_test = Y_test.reshape(1, Y_test.shape[0])
print(X_train.shape, X_test.shape)
print(Y_train.shape, Y_test.shape)
%%time
layers_dims = [7, 6, 1]
learning_rate = 0.001
num_iterations = 100000
lambd = 0.9
parameters = the_model(X_train, Y_train, layers_dims, learning_rate, num_iterations, lambd)<jupyter_output><empty_output><jupyter_text>Accuracy Prediction<jupyter_code>%%time
prob = predictAccuracy(X_train, Y_train, parameters)
%%time
prob = predictAccuracy(X_test, Y_test, parameters)
np.save("parameters-v2", parameters)<jupyter_output><empty_output><jupyter_text>Evaluation Time!  Test Data cleaning<jupyter_code>test_data = pd.read_csv('test.csv')
test_data.isnull().sum()
test_data['Initial'] = test_data.Name.str.extract('([A-Za-z]+)\.', expand=True)
test_data.head()
test_data.groupby('Initial')['Age'].count()
test_data['Initial'].replace(['Col', 'Dona','Dr', 'Ms', 'Rev'], ['Other', 'Miss', 'Mr', 'Miss', 'Other'], inplace=True)
test_data.groupby('Initial')['Age'].count()
test_data.groupby('Initial')['Age'].mean()
## Assigning the NaN Values with the Ceil values of the mean ages
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Mr'),'Age']=33
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Mrs'),'Age']=39
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Master'),'Age']=7
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Miss'),'Age']=22
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Other'),'Age']=43
test_data['Age_band']=0
test_data.loc[test_data['Age']<=16,'Age_band']=0
test_data.loc[(test_data['Age']>16)&(test_data['Age']<=32),'Age_band']=1
test_data.loc[(test_data['Age']>32)&(test_data['Age']<=48),'Age_band']=2
test_data.loc[(test_data['Age']>48)&(test_data['Age']<=64),'Age_band']=3
test_data.loc[test_data['Age']>64,'Age_band']=4
data['Age_band'].value_counts().to_frame()
test_data['Fare_cat'] = 0
test_data.loc[test_data['Fare'] <= 7.91, 'Fare_cat'] = 0
test_data.loc[(test_data['Fare'] > 7.91) & (test_data['Fare'] <= 14.454), 'Fare_cat'] = 1
test_data.loc[(test_data['Fare'] > 14.454) & (test_data['Fare'] <= 31), 'Fare_cat'] = 2
test_data.loc[(test_data['Fare'] > 31) & (test_data['Fare'] <= 513), 'Fare_cat'] = 3
test_data['Family_Size'] = data['Parch'] + data['SibSp'] + 1
test_data['Sex'].replace(['male','female'],[0,1],inplace=True)
test_data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
test_data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True)<jupyter_output><empty_output><jupyter_text>Run Model on Test data<jupyter_code>X = test_data[['Pclass', 'Sex', 'Age_band', 'Embarked', 'Initial', 'Fare_cat', 'Family_Size']].values
X = X.T.astype(float)
X.shape
def predict(X, parameters):
    m = X.shape[1]
    p = np.zeros((1, m))
    probas, caches = forward_propogation(X, parameters)
    
    for i in range(0, probas.shape[1]):
        if probas[0, i] > 0.5:
            p[0, i] = 1
        else:
            p[0, i] = 0

    return np.squeeze(p)
Y = predict(X, parameters)<jupyter_output><empty_output><jupyter_text>Generate csv file for submission<jupyter_code>with open('submission-v2.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerow(['PassengerId', 'Survived'])
    for index, row in test_data.iterrows():
        writer.writerow([row['PassengerId'], int(Y[index])])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19151324651998203, 'fraction_numerical': 0.02028139500074839, 'mean_word_length': 4.752475247524752, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '82348', 'license': 'MIT', 'n_tokens_mistral': 5428, 'n_tokens_neox': 5154, 'n_words': 881, 'path': 'titanic-machine-learning-from-disaster/titanic-v2.ipynb', 'repo_name': 'rishabhiitbhu/kaggle', 'size': '13362'}