-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.py
More file actions
258 lines (208 loc) · 7.44 KB
/
Copy pathcode.py
File metadata and controls
258 lines (208 loc) · 7.44 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# This is the auxiliary code for the 3F8 coursework. Some parts are missing and
# should be completed by the student. These are Marked with XXX
# We load the data
import numpy as np
X = np.loadtxt('X.txt')
y = np.loadtxt('y.txt')
# We randomly permute the data
permutation = np.random.permutation(X.shape[ 0 ])
X = X[ permutation, : ]
y = y[ permutation ]
# We plot the data
import matplotlib.pyplot as plt
##
# Function that plots the points in 2D together with their labels
#
# Inputs:
#
# X: 2d array with the input features
# y: 1d array with the class labels (0 or 1)
#
# Output: 2D matrices with the x and y coordinates of the points shown in the plot
#
def plot_data_internal(X, y):
x_min, x_max = X[ :, 0 ].min() - .5, X[ :, 0 ].max() + .5
y_min, y_max = X[ :, 1 ].min() - .5, X[ :, 1 ].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
plt.figure()
plt.xlim(xx.min(None), xx.max(None))
plt.ylim(yy.min(None), yy.max(None))
ax = plt.gca()
ax.plot(X[y == 0, 0], X[y == 0, 1], 'ro', label = 'Class 1')
ax.plot(X[y == 1, 0], X[y == 1, 1], 'bo', label = 'Class 2')
plt.xlabel('X1')
plt.ylabel('X2')
plt.title('Plot data')
plt.legend(loc = 'upper left', scatterpoints = 1, numpoints = 1)
return xx, yy
##
# Function that plots the data without returning anything by calling "plot_data_internal".
#
# Input:
#
# X: 2d array with the input features
# y: 1d array with the class labels (0 or 1)
#
# Output: Nothing.
#
def plot_data(X, y):
xx, yy = plot_data_internal(X, y)
plt.show()
plot_data(X, y)
# We split the data into train and test sets
n_train = 800
X_train = X[ 0 : n_train, : ]
X_test = X[ n_train :, : ]
y_train = y[ 0 : n_train ]
y_test = y[ n_train : ]
# The logistic function
def logistic(x): return 1.0 / (1.0 + np.exp(-x))
##
# Function that makes predictions with a logistic classifier
#
# Input:
#
# X_tile: matrix of input features (with a constant 1 appended to the left)
# for which to make predictions
# w: vector of model parameters
#
# Output: The predictions of the logistic classifier
#
def predict(X_tilde, w): return logistic(np.dot(X_tilde, w))
##
# Function that computes the average loglikelihood of the logistic classifier on some data.
#
# Input:
#
# X_tile: matrix of input features (with a constant 1 appended to the left)
# for which to make predictions
# y: vector of binary output labels
# w: vector of model parameters
#
# Output: The average loglikelihood
#
def compute_average_ll(X_tilde, y, w):
output_prob = predict(X_tilde, w)
return np.mean(y * np.log(output_prob) + (1 - y) * np.log(1.0 - output_prob))
##
# Function that expands a matrix of input features by adding a column equal to 1.
#
# Input:
#
# X: matrix of input features.
#
# Output: Matrix x_tilde with one additional constant column equal to 1 added.
#
def get_x_tilde(X): return np.concatenate((np.ones((X.shape[ 0 ], 1 )), X), 1)
##
# Function that finds the model parameters by optimising the likelihood using gradient descent
#
# Input:
#
# X_tile_train: matrix of training input features (with a constant 1 appended to the left)
# y_train: vector of training binary output labels
# X_tile_test: matrix of test input features (with a constant 1 appended to the left)
# y_test: vector of test binary output labels
# alpha: step_size_parameter for the gradient based optimisation
# n_steps: the number of steps of gradient based optimisation
#
# Output:
#
# 1 - Vector of model parameters w
# 2 - Vector with average log-likelihood values obtained on the training set
# 3 - Vector with average log-likelihood values obtained on the test set
#
def fit_w(X_tilde_train, y_train, X_tilde_test, y_test, n_steps, alpha):
w = np.random.randn(X_tilde_train.shape[ 1 ])
ll_train = np.zeros(n_steps)
ll_test = np.zeros(n_steps)
for i in range(n_steps):
sigmoid_value = predict(X_tilde_train, w)
w = # XXX Gradient-based update rule for w. To be completed by the student
ll_train[ i ] = compute_average_ll(X_tilde_train, y_train, w)
ll_test[ i ] = compute_average_ll(X_tilde_test, y_test, w)
print(ll_train[ i ], ll_test[ i ])
return w, ll_train, ll_test
# We train the classifier
alpha = # XXX Learning rate for gradient-based optimisation. To be completed by the student
n_steps = # XXX Number of steps of gradient-based optimisation. To be completed by the student
X_tilde_train = get_x_tilde(X_train)
X_tilde_test = get_x_tilde(X_test)
w, ll_train, ll_test = fit_w(X_tilde_train, y_train, X_tilde_test, y_test, n_steps, alpha)
##
# Function that plots the average log-likelihood returned by "fit_w"
#
# Input:
#
# ll: vector with log-likelihood values
#
# Output: Nothing
#
def plot_ll(ll):
plt.figure()
ax = plt.gca()
plt.xlim(0, len(ll) + 2)
plt.ylim(min(ll) - 0.1, max(ll) + 0.1)
ax.plot(np.arange(1, len(ll) + 1), ll, 'r-')
plt.xlabel('Steps')
plt.ylabel('Average log-likelihood')
plt.title('Plot Average Log-likelihood Curve')
plt.show()
# We plot the training and test log likelihoods
plot_ll(ll_train)
plot_ll(ll_test)
##
# Function that plots the predictive probabilities of the logistic classifier
#
# Input:
#
# X: 2d array with the input features for the data (without adding a constant column with ones at the beginning)
# y: 1d array with the class labels (0 or 1) for the data
# w: parameter vector
# map_inputs: function that expands the original 2D inputs using basis functions.
#
# Output: Nothing.
#
def plot_predictive_distribution(X, y, w, map_inputs = lambda x : x):
xx, yy = plot_data_internal(X, y)
ax = plt.gca()
X_tilde = get_x_tilde(map_inputs(np.concatenate((xx.ravel().reshape((-1, 1)), yy.ravel().reshape((-1, 1))), 1)))
Z = predict(X_tilde, w)
Z = Z.reshape(xx.shape)
cs2 = ax.contour(xx, yy, Z, cmap = 'RdBu', linewidths = 2)
plt.clabel(cs2, fmt = '%2.1f', colors = 'k', fontsize = 14)
plt.show()
# We plot the predictive distribution
plot_predictive_distribution(X, y, w)
##
# Function that replaces initial input features by evaluating Gaussian basis functions
# on a grid of points
#
# Inputs:
#
# l: hyper-parameter for the width of the Gaussian basis functions
# Z: location of the Gaussian basis functions
# X: points at which to evaluate the basis functions
#
# Output: Feature matrix with the evaluations of the Gaussian basis functions.
#
def evaluate_basis_functions(l, X, Z):
X2 = np.sum(X**2, 1)
Z2 = np.sum(Z**2, 1)
ones_Z = np.ones(Z.shape[ 0 ])
ones_X = np.ones(X.shape[ 0 ])
r2 = np.outer(X2, ones_Z) - 2 * np.dot(X, Z.T) + np.outer(ones_X, Z2)
return np.exp(-0.5 / l**2 * r2)
# We expand the data
l = # XXX Width of the Gaussian basis funcction. To be completed by the student
X_tilde_train = get_x_tilde(evaluate_basis_functions(l, X_train, X_train))
X_tilde_test = get_x_tilde(evaluate_basis_functions(l, X_test, X_train))
# We train the new classifier on the feature expanded inputs
alpha = # XXX Learning rate for gradient-based optimisation with basis functions. To be completed by the student
n_steps = # XXX Number of steps of gradient-based optimisation with basis functions. To be completed by the student
w, ll_train, ll_test = fit_w(X_tilde_train, y_train, X_tilde_test, y_test, n_steps, alpha)
# We plot the training and test log likelihoods
plot_ll(ll_train)
plot_ll(ll_test)
# We plot the predictive distribution
plot_predictive_distribution(X, y, w, lambda x : evaluate_basis_functions(l, x, X_train))