-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple-blobs.py
87 lines (70 loc) · 3.13 KB
/
simple-blobs.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
# simple-blobs.py
# Defines a network that can find separate data from two blobs of data from
# different classes
#
# Imports
from sklearn.datasets import make_blobs
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="2"
# Helper functions
# plot the data on a figure
def plot_data(pl, X, y):
# plot class where y==0
pl.plot(X[y==0, 0], X[y==0,1], 'ob', alpha=0.5)
# plot class where y==1
pl.plot(X[y==1, 0], X[y==1,1], 'xr', alpha=0.5)
pl.legend(['0', '1'])
return pl
# Common function that draws the decision boundaries
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()]
# make prediction with the model and reshape the output so contourf can plot it
c = model.predict(ab)
Z = c.reshape(aa.shape)
plt.figure(figsize=(12, 8))
# plot the contour
plt.contourf(aa, bb, Z, cmap='bwr', alpha=0.2)
# plot the moons of data
plot_data(plt, X, y)
return plt
# Generate some data blobs. Data will be either 0 or 1 when 2 is number of centers.
# X is a [number of samples, 2] sized array. X[sample] contains its x,y position of the sample in the space
# ex: X[1] = [1.342, -2.3], X[2] = [-4.342, 2.12]
# y is a [number of samples] sized array. y[sample] contains the class index (ie. 0 or 1 when there are 2 centers)
# ex: y[1] = 0 , y[1] = 1
X, y = make_blobs(n_samples=1000, centers=2, random_state=42)
pl = plot_data(plt, X, y)
pl.show()
# Split the data into Training and Test sets
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)
# Create the keras model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
# Simple Sequential model
model = Sequential()
# Add a Dense Fully Connected Layer with 1 neuron. Using input_shape = (2,) says the input will
# be arrays of the form (*,2). The first dimension will be an unspecified
# number of batches (rows) of data. The second dimension is 2 which are the X, Y positions of each data element.
# The sigmoid activation function is used to return 0 or 1, signifying the data
# cluster the position is predicted to belong to.
model.add(Dense(1, input_shape=(2,), activation="sigmoid"))
# Compile the model. Minimize crossentopy for a binary. Maximize for accuracy
model.compile(Adam(lr=0.05), 'binary_crossentropy', metrics=['accuracy'])
# Fit the model with the data from make_blobs. Make 100 cycles through the data.
# Set verbose to 0 to supress progress messages
model.fit(X_train, y_train, epochs=100, verbose=1)
# Get loss and accuracy on test data
eval_result = model.evaluate(X_test, y_test)
# Print test accuracy
print("\n\nTest loss:", eval_result[0], "Test accuracy:", eval_result[1])
# Plot the decision boundary
plot_decision_boundary(model, X, y).show()