text_plain_output_1.png
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score, log_loss, f1_score, classification_report, roc_curve, plot_roc_curve

from sklearn.metrics import confusion_matrix, precision_score, recall_score, precision_recall_curve, roc_auc_score

from sklearn.model_selection import KFold

from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedShuffleSplit

from sklearn.preprocessing import StandardScaler

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train_data = pd.read_csv('../input/titanic/train.csv')

train_data.shape

def getDatasetDetail(data):
    return pd.DataFrame({'Datatype': data.dtypes.astype(str), 'Non_Null_Count': data.count(axis=0).astype(int), 'Null_Count': data.isnull().sum().astype(int), 'Null_Percentage': round(data.isnull().sum() / len(data) * 100, 2), 'Unique_Values_Count': data.nunique().astype(int)}).sort_values(by='Null_Percentage', ascending=False)

X_train.shape

scaler = StandardScaler()
X_train_sc = scaler.fit_transform(X_train)
X_train_scaled = pd.DataFrame(X_train_sc, columns=X_train.columns)

X_test_sc = scaler.transform(X_test)
X_test_scaled = pd.DataFrame(X_test_sc, columns=X_test.columns)

def evaluate_model(actual, pred):
    acc_sc = round(accuracy_score(actual, pred) * 100, 2)
    prec_sc = round(precision_score(actual, pred) * 100, 2)
    rec_sc = round(recall_score(actual, pred) * 100, 2)
    confusion_m = confusion_matrix(actual, pred)
    TP = confusion_m[1, 1]
    TN = confusion_m[0, 0]
    FP = confusion_m[0, 1]
    FN = confusion_m[1, 0]
    Specificity = round(TN / float(TN + FP), 2)
    roc_score = round(recall_score(actual, pred) * 100, 2)
    f1_score = round(2 * (prec_sc * rec_sc / (prec_sc + rec_sc)), 2)
    return {'TP': TP, 'TN': TN, 'FP': FP, 'FN': FN, 'Recall': rec_sc, 'Precision': prec_sc, 'Specificity': Specificity, 'ROC/AUC Score': roc_score, 'F1-Score': f1_score, 'Accuracy': acc_sc}

model_lg = LogisticRegression(random_state=42)
model_lg.fit(X_train_scaled, y_train)
Y_train_pred = model_lg.predict(X_train_scaled)
Y_test_pred = model_lg.predict(X_test_scaled)

train_eval = pd.DataFrame([evaluate_model(y_train, Y_train_pred)])
test_eval = pd.DataFrame([evaluate_model(y_test, Y_test_pred)])
df_eval = pd.concat([train_eval, test_eval])
df_eval['data'] = ['train_data', 'test_data']
df_eval.set_index('data', inplace=True)

folds = KFold(n_splits=10, shuffle=True, random_state=4)
params = {'C': [0.01, 0.1, 1, 10, 100, 1000]}
model_cv = GridSearchCV(estimator=LogisticRegression(), param_grid=params, scoring='recall', cv=folds, verbose=1, return_train_score=True)
model_cv.fit(X_train_scaled, y_train)

cv_results = pd.DataFrame(model_cv.cv_results_)
cv_results