Upload 2 files
Browse files
Credit_Card_Transactions.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
credit_card_transactions.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Credit Card Transactions
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1u6Uvg6spSXdnjrvtQi8OjhJOGywYvsNG
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import seaborn as sns
|
| 14 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 15 |
+
from sklearn.model_selection import train_test_split
|
| 16 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 17 |
+
from sklearn.model_selection import GridSearchCV
|
| 18 |
+
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, ConfusionMatrixDisplay
|
| 19 |
+
from sklearn.ensemble import GradientBoostingClassifier
|
| 20 |
+
|
| 21 |
+
df = pd.read_csv('creditcard.csv')
|
| 22 |
+
|
| 23 |
+
df.head()
|
| 24 |
+
|
| 25 |
+
df.shape
|
| 26 |
+
|
| 27 |
+
df.columns
|
| 28 |
+
|
| 29 |
+
df.info()
|
| 30 |
+
|
| 31 |
+
df.describe()
|
| 32 |
+
|
| 33 |
+
df.isnull().sum()
|
| 34 |
+
|
| 35 |
+
df.duplicated().sum()
|
| 36 |
+
|
| 37 |
+
df.drop_duplicates(inplace=True)
|
| 38 |
+
|
| 39 |
+
df.shape
|
| 40 |
+
|
| 41 |
+
df['Class'].unique()
|
| 42 |
+
|
| 43 |
+
df['Class'].value_counts()
|
| 44 |
+
|
| 45 |
+
fraud = df[df['Class'] == 1]
|
| 46 |
+
normal = df[df['Class'] == 0]
|
| 47 |
+
normal_percentage = len(normal)/(len(fraud)+len(normal))
|
| 48 |
+
fraud_percentage = len(fraud)/(len(fraud)+len(normal))
|
| 49 |
+
print('Percentage of fraud transactions = ', round(fraud_percentage * 100, 3))
|
| 50 |
+
print('Percentage of normal transactions = ', round(normal_percentage * 100, 3))
|
| 51 |
+
|
| 52 |
+
plt.figure(figsize=(9,7))
|
| 53 |
+
sns.countplot(data=df,x='Class',palette=['blue', 'red'])
|
| 54 |
+
plt.title("Number of Normal and Fraud Transactions");
|
| 55 |
+
|
| 56 |
+
plt.figure(figsize=(8,6))
|
| 57 |
+
sns.FacetGrid(df, hue="Class", height=6,palette=['blue','red']).map(plt.scatter, "Time", "Amount").add_legend()
|
| 58 |
+
plt.show()
|
| 59 |
+
|
| 60 |
+
plt.figure(figsize=(10,7))
|
| 61 |
+
sns.heatmap(data=df.corr(),cmap='mako')
|
| 62 |
+
plt.show()
|
| 63 |
+
|
| 64 |
+
X = df.drop('Class',axis=1)
|
| 65 |
+
y = df['Class']
|
| 66 |
+
|
| 67 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def model_train_test(model,X_train,y_train,X_test,y_test):
|
| 72 |
+
model.fit(X_train,y_train)
|
| 73 |
+
prediction = model.predict(X_test)
|
| 74 |
+
print('Accuracy = {}'.format(accuracy_score(y_test,prediction)))
|
| 75 |
+
print(classification_report(y_test,prediction))
|
| 76 |
+
matrix = confusion_matrix(y_test,prediction)
|
| 77 |
+
dis = ConfusionMatrixDisplay(matrix)
|
| 78 |
+
dis.plot()
|
| 79 |
+
plt.show()
|
| 80 |
+
|
| 81 |
+
rf_model = RandomForestClassifier()
|
| 82 |
+
|
| 83 |
+
model_train_test(rf_model,X_train,y_train,X_test,y_test)
|
| 84 |
+
|
| 85 |
+
Decision_tree = DecisionTreeClassifier()
|
| 86 |
+
|
| 87 |
+
model_train_test(Decision_tree,X_train,y_train,X_test,y_test)
|