a. Carry out a k-NN analysis on the test point (3.8, 5.2). What is the likely color of this test point when k = 4. b. Wi
Posted: Thu May 26, 2022 9:14 am
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, learning_rate=0.1):
self.weights = np.zeros((3,))
self.learning_rate =
learning_rate
self.training_error_history = []
self.test_error_history = []
def train(self, x_set, y_set):
# shuffle data for each epoch
train_data = np.hstack((x_set,
y_set.reshape((len(y_set), -1))))
np.random.shuffle(train_data)
x_set, y_set = train_data[:, 0:2],
train_data[:, 2]
for x, y in zip(x_set, y_set):
pred =
self.predict(x)
self.weights -=
self.learning_rate * \
(pred - y) * \
np.array([x[0], x[1], 1.0])
error_train2 = 0
for x, y in zip(x_set, y_set):
pred =
self.predict(x)
error_train2 += (pred -
y) ** 2
self.training_error_history.append(error_train2 / len(x_train))
print('Updated weights: {}, Training
MSE: {}'
.format(self.weights,
self.training_error_history[-1]))
def predict(self, x):
return 1 if np.dot(x,
self.weights[0:2]) + self.weights[2] >= 1 else 0
def evaluate(self, x_set, y_set):
test_error2 = 0
for i in range(len(x_set)):
test_error2 +=
(self.predict(x_set) - y_set) ** 2
self.test_error_history.append(test_error2 / len(x_set))
print('Testing MSE:
{}'.format(self.test_error_history[-1]))
def plot_error_histories(self):
plt.plot(self.training_error_history,
label='Training MSE')
plt.plot(self.test_error_history,
label='Test MSE')
plt.legend()
plt.xlabel('Training cycle')
plt.grid(True)
plt.show()
if __name__ == '__main__':
# x train data
x_train = np.array([[-10, 5],
[-10, 18],
[-9, 20],
[-5, 5],
[-3, 0],
[2, -3],
[5, -7],
[5, -8],
[5, -6],
[5, 0],
[4, 0],
[1, 0],
[1, 1],
[-2, 5],
[-3, 11],
[-6, 18],
[-10, 24]])
# y train data
y_train = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1])
# x test data
x_test = np.array([[-8, 4],
[-6, 12],
[-5, 6],
[-5, 10],
[0, 2],
[1, 0],
[6, -12],
[6, -14],
[-7, 18],
[-9, 30],
[-5, 8],
[-1, 14],
[1, 4],
[2, 0],
[2, 11],
[3, 0],
[6, -5]])
# y test data
y_test = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1])
p = Perceptron(learning_rate=0.001)
epochs = 80
for e in range(epochs):
p.train(x_train, y_train)
p.evaluate(x_test, y_test)
p.plot_error_histories()
# plot data and separating line
w1, w2, b = p.weights
class1 = np.vstack((x_train[:8, :], x_test[:8,
:]))
class2 = np.vstack((x_train[8:, :], x_test[8:,
:]))
plt.scatter(class1[:, 0], class1[:, 1], c='red',
label='Label 1')
plt.scatter(class2[:, 0], class2[:, 1], c='blue',
label='Label 2')
x = np.linspace(-12, 7, 100)
y = (1 - b) / w2 - w1 * x / w2
plt.grid(True)
plt.legend()
plt.xlabel('x1')
plt.ylabel('x2')
plt.plot(x, y, color='black')
plt.show()
a. Carry out a k-NN analysis on the test point (3.8, 5.2). What is the likely color of this test point when k = 4. b. With the Python program in the Appendix for a perceptron that has been made available in class earlier and which you can download from Canvas too try to train the perceptron on the dataset in the Appendix and let the perceptron predict the color of each entry. You may modify the code to suit the problem I. How should you split the data set into a training set and a test set? Comment on the decreasing error rates. II. III. Test your trained perceptron with the test points given in the test set in the Appendix.
Scatter: X 0 1 3 1 1 3 st 4 4 5 3 2 4 60 2 5 00 4 6 9 9 2 5 Y 10 8 8 1 7 8 6 7 7 3 3 4 5 5 4 colour Green Green Green Blue Blue Blue Green Green Red Red Blue Blue Red Blue Red 8 Red 3 Blue 8 Red 9 Red 5 Red 6 Green Green 60