-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
117 lines (83 loc) · 2.14 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# %%
import pandas as pd
# %%
dataset=pd.read_csv("cleansed_dataset.csv")
dataset.head()
# %%
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
# %%
print(list(dataset.columns))
# %%
x=dataset.drop('Class',axis=1)
x
# %%
y=dataset['Class']
y
# %%
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)
# %%
log_model=LogisticRegression(max_iter=1000)
# %%
log_model.fit(x_train,y_train)
# %%
y_pred=log_model.predict(x_test)
y_pred
# %%
x_train_pred=log_model.predict(x_train)
x_train_pred
# %%
accuracy_score(y_pred,y_test) #for test data
# %%
accuracy_score(x_train_pred,y_train)
# %%
conf_matrix=confusion_matrix(y_test,y_pred)
conf_matrix
# %%
import matplotlib.pyplot as plt
import seaborn as sns
# %%
plt.figure(figsize=(6,6))
sns.heatmap(conf_matrix,annot=True,fmt='d', cmap='Blues',xticklabels=['Non_fraud_predicted','Fraud_predicted'],yticklabels=['Actual_Non_Fraud','Actual_Fraud'])
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show
# %%
print(classification_report(y_test,y_pred))
# %%
from sklearn.ensemble import RandomForestClassifier
# %%
RF_model=RandomForestClassifier(n_estimators=100,random_state=42)
# %%
RF_model.fit(x_train,y_train)
# %%
y_pred_rf=RF_model.predict(x_test)
y_pred_rf
# %%
x_train_pred_RF=RF_model.predict(x_train)
# %%
accuracy_score(y_pred_rf,y_test)
# %%
accuracy_score(x_train_pred_RF,y_train)
# %%
conf_matrix_RF=confusion_matrix(y_test,y_pred_rf)
conf_matrix_RF
# %%
plt.figure(figsize=(6,6))
sns.heatmap(conf_matrix_RF,annot=True,fmt='d', cmap='Blues',xticklabels=['Non_fraud_predicted','Fraud_predicted'],yticklabels=['Actual_Non_Fraud','Actual_Fraud'])
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show
# %%
from sklearn.model_selection import cross_val_score,KFold
# %%
kf=KFold(n_splits=5,shuffle=True,random_state=42)
Cross_Scores=cross_val_score(RF_model,x,y,cv=kf,scoring='accuracy')
Cross_Scores
# %%
Cross_Scores.mean()
# %%
print(classification_report(y_test,y_pred_rf))