-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotDecBoundaries.py
executable file
·171 lines (131 loc) · 7.25 KB
/
plotDecBoundaries.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
################################################
## EE559 HW Wk2, Prof. Jenkins, Spring 2018
## Created by Arindam Jati, TA
## Tested in Python 3.6.3, OSX El Captain
################################################
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial.distance import cdist
def plotDecBoundaries(training, label_train, sample_mean):
#Plot the decision boundaries and data points for minimum distance to
#class mean classifier
#
# training: traning data
# label_train: class lables correspond to training data
# sample_mean: mean vector for each class
#
# Total number of classes
nclass = max(np.unique(label_train))
# Set the feature range for ploting
max_x = np.ceil(max(training[:, 0])) + 1
min_x = np.floor(min(training[:, 0])) - 1
max_y = np.ceil(max(training[:, 1])) + 1
min_y = np.floor(min(training[:, 1])) - 1
xrange = (min_x, max_x)
yrange = (min_y, max_y)
# step size for how finely you want to visualize the decision boundary.
inc = 0.005
# generate grid coordinates. this will be the basis of the decision
# boundary visualization.
(x, y) = np.meshgrid(np.arange(xrange[0], xrange[1]+inc/100, inc), np.arange(yrange[0], yrange[1]+inc/100, inc))
# size of the (x, y) image, which will also be the size of the
# decision boundary image that is used as the plot background.
image_size = x.shape
xy = np.hstack( (x.reshape(x.shape[0]*x.shape[1], 1, order='F'), y.reshape(y.shape[0]*y.shape[1], 1, order='F')) ) # make (x,y) pairs as a bunch of row vectors.
# distance measure evaluations for each (x,y) pair.
dist_mat = cdist(xy, sample_mean)
pred_label = np.argmin(dist_mat, axis=1)
# reshape the idx (which contains the class label) into an image.
decisionmap = pred_label.reshape(image_size, order='F')
#show the image, give each coordinate a color according to its class label
plt.imshow(decisionmap, extent=[xrange[0], xrange[1], yrange[0], yrange[1]], origin='lower')
# plot the class training data.
plt.plot(training[label_train == 1, 0],training[label_train == 1, 1], 'rx')
plt.plot(training[label_train == 2, 0],training[label_train == 2, 1], 'go')
if nclass == 3:
plt.plot(training[label_train == 3, 0],training[label_train == 3, 1], 'b*')
# include legend for training data
if nclass == 3:
l = plt.legend(('Class 1', 'Class 2', 'Class 3'), loc=2)
else:
l = plt.legend(('Class 1', 'Class 2'), loc=2)
plt.gca().add_artist(l)
# plot the class mean vector.
m1, = plt.plot(sample_mean[0,0], sample_mean[0,1], 'rd', markersize=12, markerfacecolor='r', markeredgecolor='w')
m2, = plt.plot(sample_mean[1,0], sample_mean[1,1], 'gd', markersize=12, markerfacecolor='g', markeredgecolor='w')
if nclass == 3:
m3, = plt.plot(sample_mean[2,0], sample_mean[2,1], 'bd', markersize=12, markerfacecolor='b', markeredgecolor='w')
# include legend for class mean vector
if nclass == 3:
l1 = plt.legend([m1,m2,m3],['Class 1 Mean', 'Class 2 Mean', 'Class 3 Mean'], loc=4)
else:
l1 = plt.legend([m1,m2], ['Class 1 Mean', 'Class 2 Mean'], loc=4)
plt.gca().add_artist(l1)
plt.show()
##########################################################################################
# import numpy as np
# import matplotlib.pyplot as plt
# from scipy.spatial.distance import cdist
# def plotDecBoundaries(training, label_train, sample_mean, sample_mean_prime):
# #Plot the decision boundaries and data points for minimum distance to
# #class mean classifier
# #
# # training: traning data
# # label_train: class lables correspond to training data
# # sample_mean: mean vector for each class
# #
# # # Total number of classes
# nclass = max(np.unique(label_train))
# # Set the feature range for ploting
# max_x = np.ceil(max(training[:, 0])) + 1
# min_x = np.floor(min(training[:, 0])) - 1
# max_y = np.ceil(max(training[:, 1])) + 1
# min_y = np.floor(min(training[:, 1])) - 1
# xrange = (min_x, max_x)
# yrange = (min_y, max_y)
# # step size for how finely you want to visualize the decision boundary.
# inc = 0.005
# # generate grid coordinates. this will be the basis of the decision
# # boundary visualization.
# (x, y) = np.meshgrid(np.arange(xrange[0], xrange[1]+inc/100, inc), np.arange(yrange[0], yrange[1]+inc/100, inc))
# # size of the (x, y) image, which will also be the size of the
# # decision boundary image that is used as the plot background.
# image_size = x.shape
# xy = np.hstack( (x.reshape(x.shape[0]*x.shape[1], 1, order='F'), y.reshape(y.shape[0]*y.shape[1], 1, order='F')) ) # make (x,y) pairs as a bunch of row vectors.
# # distance measure evaluations for each (x,y) pair.
# dist_mat = cdist(xy, sample_mean)
# dist_mat_prime = cdist(xy, sample_mean_prime)
# condition = dist_mat < dist_mat_prime
# pred_label = np.array([np.where(i == 1)[0][0] if sum(i) == 1 else 4 for i in condition ])
# # pred_label = np.argmin(dist_mat, axis=1)
# # reshape the idx (which contains the class label) into an image.
# decisionmap = pred_label.reshape(image_size, order='F')
# #show the image, give each coordinate a color according to its class label
# plt.imshow(decisionmap, extent=[xrange[0], xrange[1], yrange[0], yrange[1]], origin='lower')
# # plot the class training data.
# plt.plot(training[label_train == 1, 0],training[label_train == 1, 1], 'rx')
# plt.plot(training[label_train == 2, 0],training[label_train == 2, 1], 'go')
# plt.plot(training[label_train == 4, 0],training[label_train == 4, 1], 'k+')
# if nclass == 3:
# plt.plot(training[label_train == 3, 0],training[label_train == 3, 1], 'b*')
# # include legend for training data
# if nclass = 3:
# l = plt.legend(('Class 1', 'Class 2', 'Class 3', 'Indeterminate'), loc=2)
# else:
# l = plt.legend(('Class 1', 'Class 2', 'Indeterminate'), loc=2)
# plt.gca().add_artist(l)
# # # plot the class mean vector.
# m1, = plt.plot(sample_mean[0,0], sample_mean[0,1], 'rd', markersize=10, markerfacecolor='r', markeredgecolor='w')
# m2, = plt.plot(sample_mean[1,0], sample_mean[1,1], 'gd', markersize=10, markerfacecolor='g', markeredgecolor='w')
# if nclass == 3:
# m3, = plt.plot(sample_mean[2,0], sample_mean[2,1], 'bd', markersize=10, markerfacecolor='b', markeredgecolor='w')
# m6, = plt.plot(sample_mean_prime[2,0], sample_mean_prime[2,1], 'bo', markersize=10, markerfacecolor='b', markeredgecolor='w')
# m4, = plt.plot(sample_mean_prime[0,0], sample_mean_prime[0,1], 'ro', markersize=10, markerfacecolor='r', markeredgecolor='w')
# m5, = plt.plot(sample_mean_prime[1,0], sample_mean_prime[1,1], 'go', markersize=10, markerfacecolor='g', markeredgecolor='w')
# # include legend for class mean vector
# if nclass == 3:
# l1 = plt.legend([m1, m2, m3, m4, m5, m6],['Class 1 Mean', 'Class 2 Mean', 'Class 3 Mean', 'Class 1 Prime Mean', 'Class 2 Prime Mean', 'Class 3 Prime Mean'], loc=4)
# else:
# l1 = plt.legend([m1, m2, m4, m5], ['Class 1 Mean', 'Class 2 Mean', 'Class 1 Prime Mean', 'Class 2 Prime Mean'], loc=4)
# plt.gca().add_artist(l1)
# plt.show()