<jupyter_start><jupyter_text><jupyter_code># ! pip install -q tensorflow
import tensorflow as tf
print(tf.__version__)

import pandas as pd
import numpy as np
import seaborn as sbs

import re

# To plot within notebook
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

# Setting figure size
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
from tensorflow.keras import regularizers

# For normalizing data
import sklearn
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
scaler = MinMaxScaler(feature_range=(0, 1))

import warnings
warnings.filterwarnings('ignore')

from google.colab import files

uploaded1 = files.upload()
uploaded2 = files.upload()
import io

train = pd.read_csv(io.BytesIO(uploaded1['train.csv']))
test = pd.read_csv(io.BytesIO(uploaded2['test.csv']))

all = [train, test]

train.head()
test.head()
'''
df = df.fillna(value=0)
# df = df.drop(columns=['Name'])
# df = df.drop(columns=['Ticket'])
print(df.iloc[0, 3])

for idx in range(len(df)):
  if df.iloc[idx, 3] == 'male':
    df.iloc[idx, 3] = 0
  else:
    df.iloc[idx, 3] = 1

  if str(df.iloc[idx, 8])[0] == 'C':
    df.iloc[idx, 8] = str(df.iloc[idx, 8])[1:]

df.head()
'''
# Creating FamilySize Feature
for data in all:
    data['family_size'] = data['SibSp'] + data['Parch'] + 1

# Creating isAlone Feature
for data in all:
    data['is_alone'] = 0
    data.loc[data['family_size'] == 1, 'is_alone'] = 1

# NaN Value in Embarked Column
for data in all:
    data['Embarked'] = data['Embarked'].fillna('S')

# NaN Value in Fare Column
for data in all:
    data['Fare'] = data['Fare'].fillna(data['Fare'].median())
train['category_fare'] = pd.qcut(train['Fare'], 4)

# Missing Age Value Handling
for data in all:
    age_avg  = data['Age'].mean()
    age_std  = data['Age'].std()
    age_null = data['Age'].isnull().sum()

    random_list = np.random.randint(age_avg - age_std, age_avg + age_std , size = age_null)
    data['Age'][np.isnan(data['Age'])] = random_list
    data['Age'] = data['Age'].astype(int)

train['category_age'] = pd.cut(train['Age'], 5)




#Feature 7: Name
def get_title(name):
    title_search = re.search(' ([A-Za-z]+)\. ', name)
    if title_search:
        return title_search.group(1)
    return ""

for data in all:
    data['title'] = data['Name'].apply(get_title)

for data in all:
    data['title'] = data['title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'],'Rare')
    data['title'] = data['title'].replace('Mlle','Miss')
    data['title'] = data['title'].replace('Ms','Miss')
    data['title'] = data['title'].replace('Mme','Mrs')

train.head()
#Map Data
for data in all:

    #Mapping Sex
    sex_map = { 'female':0 , 'male':1 }
    data['Sex'] = data['Sex'].map(sex_map).astype(int)

    #Mapping Title
    title_map = {'Mr':1, 'Miss':2, 'Mrs':3, 'Master':4, 'Rare':5}
    data['title'] = data['title'].map(title_map)
    data['title'] = data['title'].fillna(0)

    #Mapping Embarked
    embark_map = {'S':0, 'C':1, 'Q':2}
    data['Embarked'] = data['Embarked'].map(embark_map).astype(int)

    #Mapping Fare
    data.loc[ data['Fare'] <= 7.91, 'Fare']                            = 0
    data.loc[(data['Fare'] > 7.91) & (data['Fare'] <= 14.454), 'Fare'] = 1
    data.loc[(data['Fare'] > 14.454) & (data['Fare'] <= 31), 'Fare']   = 2
    data.loc[ data['Fare'] > 31, 'Fare']                               = 3
    data['Fare'] = data['Fare'].astype(int)

    #Mapping Age
    data.loc[ data['Age'] <= 16, 'Age']                       = 0
    data.loc[(data['Age'] > 16) & (data['Age'] <= 32), 'Age'] = 1
    data.loc[(data['Age'] > 32) & (data['Age'] <= 48), 'Age'] = 2
    data.loc[(data['Age'] > 48) & (data['Age'] <= 64), 'Age'] = 3
    data.loc[ data['Age'] > 64, 'Age']                        = 4

    #Mapping Age*Title
    data['Age*Title'] = data['Age'] * data['title']

#Feature Selection
#Create list of columns to drop
drop_elements = ["Name", "Ticket", "Cabin", "SibSp", "Parch", "family_size"]

#Drop columns from both data sets
train = train.drop(drop_elements, axis = 1)
train = train.drop(['PassengerId','category_fare', 'category_age'], axis = 1)
test = test.drop(drop_elements, axis = 1)

train.head()
X_train = train.drop(columns=['Survived'], axis=1)
Y_train = train['Survived']
X_test  = test.drop("PassengerId", axis=1).copy()
# X.head()
# Y.head()

# X_norm = scaler.fit_transform(X)
# X_norm[1][2]

X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_train, Y_train, test_size=0.25)
x_val, x_test, y_val, y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)
X_train
# X_test

# Data Plot
# df = sbs.load_dataset('data')
# print(sbs.pairplot(data=data))
sbs.pairplot(data=data)
plt.show()

# Data Correlation
# -- 1st Tech
plt.matshow(data.corr())
plt.show()

# -- 2nd Tech
'''
f = plt.figure(figsize=(7, 7))
plt.matshow(data.corr(), fignum=f.number)
plt.xticks(range(data.shape[1]), data.columns, fontsize=14, rotation=45)
plt.yticks(range(data.shape[1]), data.columns, fontsize=14)
cb = plt.colorbar()
cb.ax.tick_params(labelsize=14)
plt.title('Correlation Matrix', fontsize=16);
plt.show()
'''

# -- 3rd Tech
'''
corr = data.corr()
f = plt.figure(figsize=(8,8))
corr.style.background_gradient(cmap='coolwarm')
plt.matshow(corr, fignum=f.number)
plt.show(corr)
'''

# Decision Tree Model
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
Y_pred = decision_tree.predict(X_test)
accuracy = round(decision_tree.score(X_train, Y_train) * 100, 2)
print("Model Accuracy: ",accuracy)
model = Sequential()
model.add(Dense(48, activation='selu', kernel_initializer='lecun_normal', kernel_regularizer=regularizers.l2(0.01), input_shape=(8,)))
model.add(Dropout(0.25))
model.add(Dense(32, activation='selu', kernel_initializer='lecun_normal', kernel_regularizer=regularizers.l2(0.01)))
model.add(Dropout(0.25))
model.add(Dense(1, activation='sigmoid', kernel_regularizer=regularizers.l2(0.01)))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
hist = model.fit(X_train, Y_train, batch_size=32, epochs=100, validation_data=(x_val, y_val))

# print(model.summary())
model.evaluate(x_test, y_test)[1]
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper right')
plt.show()
# Create and Fit the LSTM network - Not Working
x_train, y_train = np.array(X_train), np.array(Y_train)

x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))

model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=100, batch_size=16, verbose=2)
Y = model.predict(X_test, batch_size=32)
Y_dt = Y_pred

Y[:20]
#Y_dt[:20]
Y = model.predict(X_test, batch_size=32)
Y_dt = Y_pred

out = pd.DataFrame(index=range(len(X)), columns=['PassengerID', 'Survived'])

for idx in range(len(X)):
  out.iloc[idx, 0] = X.iloc[idx, 0]
  if idx < 20:
    print(out.iloc[idx, 0], Y[idx])
  if Y[idx] > 0.5:
    out.iloc[idx, 1] = 1
  else:
    out.iloc[idx, 1] = 0

out.head(20)
Y[:20]
from google.colab import drive
drive.mount('drive')
out.to_csv('titanic_NN_3D_Selu_L2-1-Reg_result.csv', index=False)
!cp titanic_NN_3D_Selu_L2-1-Reg_result.csv "drive/My Drive/"
print(len(out))<jupyter_output>418
{'fraction_non_alphanumeric': 0.18749190728991325, 'fraction_numerical': 0.025896672277612328, 'mean_word_length': 4.847085541256623, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '504804', 'license': 'Apache-2.0', 'n_tokens_mistral': 3163, 'n_tokens_neox': 2950, 'n_words': 594, 'path': 'Titanic.ipynb', 'repo_name': 'porcelainruler/Kaggle', 'size': '7723'}