/
SVM.py
165 lines (87 loc) · 2.59 KB
/
SVM.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import sklearn
# In[2]:
df = pd.read_csv('creditcard.csv', low_memory=False)
df.head()
x = df.iloc[:,:-1]
y = df['Class']
# x.head()
frauds = df.loc[df['Class'] == 1]
non_frauds = df.loc[df['Class'] == 0]
print("We have", len(frauds), "fraud data points and", len(non_frauds), "regular data points.")
# In[3]:
from sklearn.preprocessing import scale
x = scale(x)
# In[4]:
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y,
test_size=0.3,
random_state=42)
print("Size of training set: ", X_train.shape)
# In[5]:
from sklearn import svm
# In[6]:
from sklearn import svm
clf = svm.SVC()
clf.fit(X_train, y_train)
# In[7]:
predictions = clf.predict(X_test)
print("Size of training set: ", X_test.shape)
print(predictions.shape)
# In[8]:
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions))
# In[9]:
print(classification_report(y_test,predictions))
from sklearn.metrics import accuracy_score
accuracy_score(y_test, predictions)
# # after Oversampling
# In[10]:
from sklearn.decomposition import PCA
from sklearn.preprocessing import scale
import pandas_ml as pdml
import imblearn
# In[11]:
X2_train, X2_test, y2_train, y2_test = train_test_split(x, y, test_size=0.3, random_state=42)
print(X2_train.shape)
print(X2_test.shape)
# In[12]:
print(df.shape)
df2 = pdml.ModelFrame(X2_train, target=y2_train)
print(df2.shape)
sampler = df2.imbalance.over_sampling.SMOTE()
oversampled = df2.fit_sample(sampler)
print(oversampled.shape)
# print(oversampled.iloc[:,:-1])
y2_train = oversampled['Class']
del oversampled['Class']
X2_train = oversampled.iloc[:,:]
print(X2_train)
y2_train = y2_train.as_matrix()
# In[13]:
# data = scale(X2_train)
# pca = PCA(n_components=10)
# X2_train = pca.fit_transform(data)
# X2_train = scale(X2_train)
X2_train = X2_train.as_matrix()
type(X2_train)
# In[14]:
from sklearn import svm
clf = svm.SVC()
# In[15]:
clf.fit(X2_train, y2_train)
# In[16]:
predictions = clf.predict(X2_test)
# print("Size of training set: ", X2_test.shape)
print(predictions.shape)
# In[17]:
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y2_test,predictions))
print(classification_report(y2_test,predictions))
# In[18]:
from sklearn.metrics import accuracy_score
accuracy_score(y2_test, predictions)