<jupyter_start><jupyter_text>This notebook is Final submission for the hackathon https://www.machinehack.com/hackathons/insurance_churn_prediction_weekend_hackathon_2/overviewAlert: This notebook is an old notebook so I couldn't structure the preprocessing and model code.<jupyter_code>import pandas as pd
pd.set_option("display.max_columns", 200)
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras import optimizers
from keras.models import Sequential, Model
from keras.layers.convolutional import Conv1D, MaxPooling1D
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_log_error
from sklearn.model_selection import train_test_split

import keras
from keras.layers import LeakyReLU
train = pd.read_csv('Train.csv')
test = pd.read_csv('Test.csv')
train['labels'].value_counts()
pd.read_excel('sample_submission.xlsx').head(5)
train.info()
test.info()
X_train, X_test, y_train, y_test = train_test_split(train.drop('labels', axis=1), train['labels'],
                                                    test_size=0.30, stratify=train['labels'])
import tensorflow as tf
from keras.losses import binary_crossentropy, categorical_crossentropy
import keras.backend as K

def f1(y_true, y_pred):
    y_pred = K.round(y_pred)
    tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
    tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)
    fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)
    fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)

    p = tp / (tp + fp + K.epsilon())
    r = tp / (tp + fn + K.epsilon())

    f1 = 2*p*r / (p+r+K.epsilon())
    f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
    return K.mean(f1)

def f1_loss(y_true, y_pred):
    
    tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
    tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)
    fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)
    fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)

    p = tp / (tp + fp + K.epsilon())
    r = tp / (tp + fn + K.epsilon())

    f1 = 2*p*r / (p+r+K.epsilon())
    f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
    return 1 - K.mean(f1)

leaky_relu_alpha =0.02
model_mlp = Sequential()
model_mlp.add(Dense(60, input_dim=X_train.shape[1], activation='relu'))
# model_mlp.add(LeakyReLU(alpha=leaky_relu_alpha))
model_mlp.add(Dense(35, kernel_initializer='normal', activation='relu'))
# model_mlp.add(LeakyReLU(alpha=leaky_relu_alpha))
model_mlp.add(Dense(15, kernel_initializer='normal', activation='relu'))
# model_mlp.add(LeakyReLU(alpha=leaky_relu_alpha))
model_mlp.add(Dense(1, activation='sigmoid'))
model_mlp.compile(optimizer='adam', loss=f1_loss, metrics=['accuracy', f1])
model_mlp.summary()
callback = [keras.callbacks.EarlyStopping(monitor='val_f1', min_delta=0, patience=10,
                                        mode='auto', restore_best_weights=True),
          keras.callbacks.ReduceLROnPlateau(monitor='val_f1', factor=0.25, patience=6,
                                            min_lr=0.00001)]
mlp_history = model_mlp.fit(X_train, y_train,
                            validation_data=(X_test, y_test),
                            epochs=150, verbose=1, batch_size=128, callbacks=callback)
mlp_history = model_mlp.fit(train.drop('labels', axis=1), train['labels'],
                            validation_data=(X_test, y_test),
                            epochs=150, verbose=1, batch_size=128)
model_mlp.predict_classes(test).flatten()
pd.DataFrame({'labels':model_mlp.predict_classes(test).flatten()
             })['labels'].value_counts()
pd.DataFrame({
    'labels':model_mlp.predict_classes(test).flatten()
             }).to_excel('Submission_3.xlsx', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1406758448060075, 'fraction_numerical': 0.022027534418022528, 'mean_word_length': 4.001251564455569, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '416202', 'license': 'MIT', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1448, 'n_words': 264, 'path': 'MachineHack/Insurance_Churn_ParticipantsData/EDA_and_Submissions.ipynb', 'repo_name': 'lavanya-m-k/ml-hackathons', 'size': '3995'}