antitheft159 commited on
Commit
a3cbb60
1 Parent(s): e6eb67c

Upload credit_card_fraud_prevention_159.py

Browse files
Files changed (1) hide show
  1. credit_card_fraud_prevention_159.py +85 -0
credit_card_fraud_prevention_159.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Credit Card Fraud Prevention.159
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
+ def model_train_test(model,X_train,y_train,X_test,y_test):
70
+ model.fit(X_train,y_train)
71
+ prediction = model.predict(X_test)
72
+ print('Accuracy = {}'.format(accuracy_score(y_test,prediction)))
73
+ print(classification_report(y_test,prediction))
74
+ matrix = confusion_matrix(y_test,prediction)
75
+ dis = ConfusionMatrixDisplay(matrix)
76
+ dis.plot()
77
+ plt.show()
78
+
79
+ rf_model = RandomForestClassifier()
80
+
81
+ model_train_test(rf_model,X_train,y_train,X_test,y_test)
82
+
83
+ Decision_tree = DecisionTreeClassifier()
84
+
85
+ model_train_test(Decision_tree,X_train,y_train,X_test,y_test)