-
Notifications
You must be signed in to change notification settings - Fork 4
/
02_2_BinaryClassification.py
209 lines (156 loc) · 4.97 KB
/
02_2_BinaryClassification.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#%% [markdown]
# ## Binary classification using DL
# Let us use make_moons which generate dataset for
# binary classification in two interleaving moon
# (swirl or two moons pattern)
#%%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
pd.set_option("display.max_rows", 13)
pd.set_option('display.max_columns', 11)
pd.set_option("display.latex.repr", False)
from matplotlib.pyplot import rcParams
rcParams['font.size'] = 14
rcParams['lines.linewidth'] = 2
rcParams['figure.figsize'] = (8.4, 5.6)
rcParams['axes.titlepad'] = 14
rcParams['savefig.pad_inches'] = 0.15
#%%
from sklearn.datasets import make_moons
#%% [markdown]
# ## 1. Load Data
#%%
X, y = make_moons(n_samples=1000, noise=0.1, random_state = 6)
#%%
X.shape
#%%
df = pd.DataFrame(dict(x=X[:,0], y=X[:,1], label=y))
df.head()
#%%
plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)
plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)
plt.legend(['0', '1'])
plt.title('Non linearly separable data')
#%% [markdown]
# ## 2. Split the data set
#%%
from sklearn.model_selection import train_test_split
#%%
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.22, random_state=42)
#%% [markdown]
# ## 3. Define model
#%%
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD, Adam
#%%
model = Sequential()
#%% [markdown]
# Let us use Logistic Regression
# 2 inputs, 1 output node using sigmoid
# ![Model](assets/02_2_1.png)
#%%
model.add(Dense(1, input_dim=2, activation='sigmoid'))
#%% [markdown]
# ## 4. Compile the Model
# Optimizer Adam - this is the algorithm that performs the actual learning.
# binary_crossentropy - cost function
#%%
model.compile(Adam(lr=0.05), 'binary_crossentropy', metrics=['accuracy'])
#%% [markdown]
# ## 5. Fit the model
#%%
model.fit(X_train, y_train, epochs=200, verbose=0)
#%%
results = model.evaluate(X_test, y_test)
#%% [markdown]
# ### Model Accuracy
#%%
print("The Accuracy score on the Test set is:\t",
"{:0.3f}".format(results[1]))
#%% [markdown]
# Let us see the boundary identified by the logistic expression
#%%
def plot_decision_boundary(model, X, y):
amin, bmin = X.min(axis=0) - 0.1
amax, bmax = X.max(axis=0) + 0.1
hticks = np.linspace(amin, amax, 101)
vticks = np.linspace(bmin, bmax, 101)
aa, bb = np.meshgrid(hticks, vticks)
ab = np.c_[aa.ravel(), bb.ravel()]
c = model.predict(ab)
cc = c.reshape(aa.shape)
plt.figure(figsize=(12, 8))
plt.contourf(aa, bb, cc, cmap='bwr', alpha=0.2)
plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)
plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)
plt.legend(['0', '1'])
plot_decision_boundary(model, X, y)
plt.title("Decision Boundary for Logistic Regression")
#%% [markdown]
# As you can see in the figure, since a shallow model like logistic regression is not able to draw curved boundaries, the best it can do is align the boundary so that most of the blue dots fall in the blue region and most of the red crosses fall in the red region.
#%% [markdown]
# ## Deep model
# ![](assets/02_2_2.png)
# 3 layers.
# Input node - 2
# First hidden layer - 4 nodes (output ReLU)
# Second hidden layer - 2 nodes (output ReLU)
# Output - 1 node (sigmoid)
#%%
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(Adam(lr=0.05), 'binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, verbose=0)
#%% [markdown]
# predict_classes() to return actual predicted classes instead of predicted probability of each classes
#%%
y_train_pred = model.predict_classes(X_train)
y_test_pred = model.predict_classes(X_test)
#%%
y_test[:3]
#%%
y_test_pred[:3]
#%%
y_train_prob = model.predict(X_train)
y_test_prob = model.predict(X_test)
#%%
y_train_prob[:3]
#%% [markdown]
# ### Accuracy
#%%
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
#%%
acc = accuracy_score(y_train, y_train_pred)
print("Accuracy (Train set):\t{:0.3f}".format(acc))
acc = accuracy_score(y_test, y_test_pred)
print("Accuracy (Test set):\t{:0.3f}".format(acc))
#%% [markdown]
# Let's plot the decision boundary for the model:
#%%
plot_decision_boundary(model, X, y)
plt.title("Decision Boundary for Fully Connected")
#%% [markdown]
# Optimize with different activation
#%%
model = Sequential()
model.add(Dense(4, input_dim=2, activation='tanh'))
model.add(Dense(2, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(Adam(lr=0.05),
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, verbose=0)
plot_decision_boundary(model, X, y)
#%%
y_train_pred = model.predict_classes(X_train)
y_test_pred = model.predict_classes(X_test)
#%%
acc = accuracy_score(y_train, y_train_pred)
print("Accuracy (Train set):\t{:0.3f}".format(acc))
acc = accuracy_score(y_test, y_test_pred)
print("Accuracy (Test set):\t{:0.3f}".format(acc))