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

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from torch.utils.data import TensorDataset, DataLoader
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score
import torch.nn.functional as F
import torch
from torch import optim
import torch
import torch.nn as nn

# # Importing and visualizing dataset

#import the dataset
iris = pd.read_csv("creditcard.csv")
iris.head()

# # Seperating Examples and Labels

# +
X= iris.iloc[:, 1:29]
Y= iris.iloc[:, 30]
from sklearn.preprocessing import normalize
X = normalize(X)
y = np.array(Y)
y = y.astype(int)

X_train,X_test,Y_train,Y_test= train_test_split(X, y, test_size= 0.10, random_state= 1)
print ('X_train shape: ',X_train.shape)
print ('y_train shape: ',Y_train.shape)
print ('X_test shape: ',X_test.shape)
print ('y_test shape: ',Y_test.shape)
# -

# ## Using Dataloader to convert numpy arrays to Tensors 

# +

trainloader = DataLoader(TensorDataset(torch.from_numpy(X_train), torch.from_numpy(Y_train)),
                         batch_size=len(X_train), shuffle=True)
testloader = DataLoader(TensorDataset(torch.from_numpy(X_test), torch.from_numpy(Y_test)),
                         batch_size=len(X_test), shuffle=False)

dataloaders = {
    "train": trainloader,
    "validation": testloader
}


# -

# ## This class will define our model
# ### Using __init__ we will define numbers of nodes in our particular layer
# ### Using forward() we will define functionality of each layer

class Classifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(28, 340)
        self.fc2 = nn.Linear(340, 220)
        self.fc3 = nn.Linear(220, 200)
        self.fc4 = nn.Linear(200, 70)
        self.fc5 = nn.Linear(70, 10)
        self.fc6 = nn.Linear(10, 2)
        self.dropout = nn.Dropout(p=0.2)
        
    def forward(self, x):
        #x = x.view(x.shape[0], -1)
        #print(x)
        
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        
        x = F.relu(self.fc4(x))
        x = self.dropout(F.relu(self.fc5(x)))
        x = F.log_softmax(self.fc6(x), dim=1)
        
        #x = F.log_softmax(self.fc4(x))
        return x


# ## Model declaration, Type of loss and optimizer.
# ### We are using adam optimizer to optimize our network

model = Classifier()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)

# ## This block is showing summary off our model

model


# ## This function is predicting output of examples we will feed in.
# ### Will be useful in  calculating model accuracies.

def predict(model, inputs):
    output = model(inputs)
    return output.data.numpy().argmax(axis= 1)


# # Here we will perform forward and backward propagation.

# +
from torch.autograd import Variable

loss1=[]
train_acc=[]

Epoch=40

for epoch in range(Epoch):
    print('------------------------------------------------------------------------------------------')
    acc=0
    train_acc1=0
    
    for i, (features, labels) in enumerate(trainloader):
        #print(features.shape)

        features = Variable(features)

        labels = Variable(labels)

        

        optimizer.zero_grad()

        features=features.float()

        outputs = model(features)

        

        loss = criterion(outputs, labels.long())

        loss.backward()

        optimizer.step()

        if (i+1) % len(trainloader) == 0:
            Ypred = predict(model, torch.from_numpy(X_train).float())
            acc = np.mean(Y_train == Ypred)
#             train_acc1=train_accuracy/len(trainloader)

            train_acc1=acc/len(trainloader)
            train_acc.append(train_acc1)
            loss1.append(loss.data)

            print ('Epoch [%d/%d], Iter [%d] Loss: %.4f Training Accuracy: %.5f' %(epoch+1, 40, i+1, loss.data, train_acc1 ))

# -

# ## we will plot our accuracies and loss functions below.

np_loss=loss1[0].numpy()
for i in range(len(loss1)):
    np_loss=np.append(np_loss, loss1[i])
np_acc=0.02
for i in range(len(train_acc)):
    np_acc=np.append(np_acc, train_acc[i])

# # Training Accuracy
#     
#     

# %matplotlib inline
plt.plot(np_acc, color='blue')
plt.title("Training Accuracy")
plt.show()

# # Training Loss

# %matplotlib inline
plt.plot(np_loss, color='red', label='Trainig loss')
plt.title("Traininng Loss")
plt.legend()
plt.show()

# # Training loss and accuracy curves

# %matplotlib inline
plt.plot(np_loss, color='red', label='Trainig loss')
plt.plot(np_acc, color='blue', label='Training Accuracy')
plt.title("Loss/Accuracy curves")
plt.legend()
plt.show()

# # Model test accuracy

Ypred = predict(model, torch.from_numpy(X_test).float())
acc = np.mean(Y_test == Ypred)
print('Test accuracy: ', acc)

# # Precision/ Recall / F1 Scores using sklearn

from sklearn.metrics import classification_report
target_names = ['Class 0', 'Class 1']
print(classification_report(Y_test, Ypred, target_names=target_names))



























StarcoderdataJupyterScriptsDedupFiltered