# ---
# 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
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/JSJeong-me/KOSA-Big-Data_Vision/blob/main/Model/03-NeuralNetwork.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="uUEoMlbU2oME"
import tensorflow as tf
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

# + id="xJjZs6Ui3Zoq"
from keras.models import Sequential
from keras.layers import Dense

# + id="x5wKw-2V2oML"
df = pd.read_csv("./credit_cards_dataset.csv")

# + id="3kzSO6jj2oMO"


# + colab={"base_uri": "https://localhost:8080/"} id="xrIZPJ8K2oMS" outputId="fd968bc3-96b6-4d02-a979-c3c1061829f4"
df.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 226} id="HRmndxef2oMU" outputId="416d4848-b27f-4c67-98d9-29be61b60f6d"
df.tail()

# + id="EDJwdLm72oMW"
# X = df.drop('Y_Value',axis =1).values
# y = df['Y_Value'].values
X = df.drop('default.payment.next.month',axis =1).values

# + colab={"base_uri": "https://localhost:8080/"} id="I22GCKi12oMZ" outputId="bc78d1d7-d80a-4ce8-ce11-d7ca604bc39d"
X.shape

# + id="NfLf_JjF2oMb"
y = df['default.payment.next.month'].values

# + id="a1T8Sl4h2oMi"
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size=0.3, random_state=42)

# + colab={"base_uri": "https://localhost:8080/"} id="3O7si-v72oMj" outputId="c519fdb0-49ce-410c-f609-5fdb68b21fd8"
y_test.T

# + colab={"base_uri": "https://localhost:8080/"} id="6t7ce3dp2oMl" outputId="07e568be-4ce5-4511-ea21-70b9343400b7"
X_test.shape

# + id="tgMxuii_2oMn"
from sklearn.preprocessing import StandardScaler
X_scaler = StandardScaler().fit(X_train)

# + id="JAXU2lAn2oMq"
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)

# + id="mRUcCmTI2oMs"
y_train_categorical = tf.keras.utils.to_categorical(y_train)
y_test_categorical = tf.keras.utils.to_categorical(y_test)

# + id="ne6jVqTZ2oMu"
from keras.models import Sequential

#instantiate
model = Sequential()

# + id="t08VRz8E2oMv"
from keras.layers import Dense

number_inputs = 24
number_units = 60

model.add(Dense(units = number_units, activation ='relu', input_dim=number_inputs))
model.add(Dense(units = 70, activation ='relu')) #second hidden layer
model.add(Dense(units = 60, activation ='relu')) #second hidden layer
#model.add(Dense(units = 50, activation ='relu')) #second hidden layer
#model.add(Dense(units = 40, activation ='relu')) #second hidden layer
#model.add(Dense(units = 30, activation ='relu')) #second hidden layer
#model.add(Dense(units = 20, activation ='relu')) #second hidden layer
model.add(Dense(units = 10, activation ='relu')) #second hidden layer

# + id="7I8QNMJJ2oMx"
number_classes =2 ## yes or no
model.add(Dense(units = number_classes, activation = 'sigmoid'))

# + colab={"base_uri": "https://localhost:8080/"} id="mKdnDBV_2oMy" outputId="2db81340-cab5-4d6d-f2fa-93a636c504c4"
model.summary()

# + id="0RdRvGpN2oMz"
#compile the model
model.compile(optimizer = 'adam' ,
             loss = 'binary_crossentropy',
             metrics =['accuracy'])

# + id="ap_InvEB2oM0"
#train the model

model.fit(X_train_scaled, y_train_categorical, epochs=50,shuffle = True,verbose =2)

# + id="H6zypaDr2oM0"
#model.save("neuralnetwork.h5")

# + colab={"base_uri": "https://localhost:8080/"} id="MceafpKl2oM1" outputId="8bcd9a54-c57a-4389-a55c-aa9a4e5c666c"
#quantify the model
model_loss, model_accuracy = model.evaluate(X_test_scaled,y_test_categorical,verbose =2)
print( model_loss )
print (model_accuracy)

# + [markdown] id="x9kabdOC2oM2"
# F1, Precision Recall, and Confusion Matrix

# + id="ppvIcD842oM5"
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import recall_score
from sklearn.metrics import classification_report

# + id="u1Rk3XTy2oM7"
y_prediction = model.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="chitKzPw2oM7" outputId="62d3ded7-837c-49db-c370-f3e6af519edf"
y_prediction.reshape(-1,1)

# + colab={"base_uri": "https://localhost:8080/"} id="DEV97JHi7DO1" outputId="a3f3549a-e6a3-483a-bab0-defae6e26bae"
scores = model.evaluate(X_test, y_prediction)

# + id="hnSwq7fb2oM8"
#print("Recall score:"+ str(recall_score(y_test, y_prediction, average# != 'binary')))

# + id="V1wfH3Lm2oM9"
#print(classification_report(y_test, y_prediction,
                            target_names=["default", "non_default"]))

# + id="kJUinAgy2oM-"
import itertools
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix


# + id="BFw7Hv6X2oM_"
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="red" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, y_prediction)
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['Non_defualt', 'Default'],
                      title='Confusion matrix, without normalization')

# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['Non_defualt', 'Default'], normalize=True,
                      title='Normalized confusion matrix')

plt.show()

# + id="h4cC15de2oNB"


StarcoderdataJupyterScriptsDedupFiltered