# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

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

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
# Importing Liberaries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pandas import read_csv, set_option
from pandas.plotting import scatter_matrix
# Loading Data
fraud_data = pd.read_csv('/kaggle/input/creditcardfraud/creditcard.csv')
# Viewing Raw Data
set_option('display.width', 100)
fraud_data.head()
# Dimension of data
fraud_data.shape
# Data Type
fraud_data.info()
"""
> Our observations are as 

> NaN values do not present in the data set. Because of the Non-Null Count and number of rows in the dataset match.

> There are 29 Input Variables and 1 Output Variable (Class)

> The data type of all the input variables is float64 whereas the data type of out variable (Class) is int64
"""
# CHecking Null values
fraud_data.isnull().sum()
# Summarizing data
set_option('precision', 5)
fraud_data.describe()

# Response Variable Analysis
class_names = {0:'Not Fraud', 1:'Fraud'}
rvs = fraud_data.Class.value_counts().rename(index = class_names)
print(rvs)
"""
***Splitting data into training and testing data***
"""
from sklearn.model_selection import train_test_split

y= fraud_data["Class"]
X = fraud_data.loc[:, fraud_data.columns != 'Class']
X_train,X_test,y_train,y_test = train_test_split(X,y, test_size=1/6, random_state=42)
"""
# Data Modelling
"""
"""
***Logistic Regression***
"""
#Import Library for Accuracy Score
from sklearn.metrics import accuracy_score

#Import Library for Logistic Regression
from sklearn.linear_model import LogisticRegression

#Initialize the Logistic Regression Classifier
logisreg = LogisticRegression()

#Train the model using Training Dataset
logisreg.fit(X_train, y_train)

# Prediction using test data
y_pred = logisreg.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_logisreg = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of Logistic Regression model : ', acc_logisreg )
"""
***Linear Discriminent Analysis***
"""
#Import Library for Linear Discriminant Analysis
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

#Initialize the Linear Discriminant Analysis Classifier
model = LinearDiscriminantAnalysis()

#Train the model using Training Dataset
model.fit(X_train, y_train)

# Prediction using test data
y_pred = model.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_lda = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of Linear Discriminant Analysis Classifier: ', acc_lda )
"""
***Gaussian Naive Bayes***
"""
#Import Library for Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB

#Initialize the Gaussian Naive Bayes Classifier
model = GaussianNB()

#Train the model using Training Dataset
model.fit(X_train, y_train)

# Prediction using test data
y_pred = model.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_ganb = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of Gaussian Naive Bayes : ', acc_ganb )
"""
***Decision Tree***
"""
#Import Library for Decision Tree Classifier
from sklearn.tree import DecisionTreeClassifier

#Initialize the Decision Tree Classifier
model = DecisionTreeClassifier()

#Train the model using Training Dataset
model.fit(X_train, y_train)

# Prediction using test data
y_pred = model.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_dtree = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of  Decision Tree Classifier : ', acc_dtree )
"""
***Random Forest***
"""
#Import Library for Random Forest
from sklearn.ensemble import RandomForestClassifier

#Initialize the Random Forest
model = RandomForestClassifier()

#Train the model using Training Dataset
model.fit(X_train, y_train)

# Prediction using test data
y_pred = model.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_rf = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of  Random Forest : ', acc_rf )
"""
***Support Vector Machine***
"""
#Import Library for Support Vector Machine
from sklearn import svm

#Initialize the Support Vector Classifier
model = svm.SVC()

#Train the model using Training Dataset
model.fit(X_train, y_train)

# Prediction using test data
y_pred = model.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_svc = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of Support Vector Classifier: ', acc_svc )
"""
***KNN***
"""
#Import Library for K Nearest Neighbour Model
from sklearn.neighbors import KNeighborsClassifier

#Initialize the K Nearest Neighbour Model with Default Value of K=5
model = KNeighborsClassifier()

#Train the model using Training Dataset
model.fit(X_train, y_train)

# Prediction using test data
y_pred = model.predict(X_test)

# Calculate Model accuracy by comparing y_test and y_pred
acc_knn = round( accuracy_score(y_test, y_pred) * 100, 2 )
print( 'Accuracy of KNN Classifier: ', acc_knn )
"""
# Model Selection
"""
models = pd.DataFrame({
    'Model': ['Logistic Regression', 'Linear Discriminant Analysis','Naive Bayes', 'Decision Tree', 'Random Forest', 'Support Vector Machines', 
              'K - Nearest Neighbors'],
    'Score': [acc_logisreg, acc_lda, acc_ganb, acc_dtree, acc_rf, acc_svc, acc_knn]})

models.sort_values(by='Score', ascending=False)

"""
>The Best model for Predicting in **Random Forest Model** with **99.96% Accuracy**
"""
"""
# Confusion Matrix
This is a binary classification problem (Fraud or No-Fraud). Some of the commonly used terms are:
* True positives (TP)
        Predicted positive and are actually positive.
* False positives (FP)
        Predicted positive and are actually negative.
* True negatives (TN)
        Predicted negative and are actually negative.
* False negatives (FN)
        Predicted negative and are actually positive.
"""
"""
![image.png](attachment:c4985768-6fd7-4dec-a7ac-0f8dd50b3a88.png)
"""
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
sns.heatmap(cm, annot = True);
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(model, X_test, y_test);
"""
* In this case, overall accuracy is strong, but the confusion metrics tell a different story. 
* Despite the high accuracy level, 36 out of 164 instances of fraud are missed and incorrectly predicted as nonfraud. 
* The false-negative rate is substantial. 
* The intention of a fraud detection model is to minimize these false negatives.
"""
"""
**Dont Forgot Upvote!!!!!!**
"""