CS计算机代考程序代写 algorithm python Implementing the Perceptron Algorithm in “Just” Python¶

Implementing the Perceptron Algorithm in “Just” Python¶

0) Imports¶

No modification required. You should execute this code and are encouraged to explore it further, but it is recommended to not make any alterations here.
In [1]:
import matplotlib.pyplot as plt
%matplotlib inline

1) Loading the dataset¶

No modification required. You should execute this code and are encouraged to explore it further, but it is recommended to not make any alterations here.
In [2]:
X, y = [], []

with open(‘./dataset.csv’, ‘r’) as f:
next(f)
for line in f:
line = line.strip()
if line:
values = line.split(‘,’)
else:
continue
X.append([float(i) for i in values[:2]])
y.append(int(values[-1]))

print(len(X), len(y))

100 100
In [3]:
import random

random.seed(123)

idx = list(range(len(X)))
random.shuffle(idx)

X_train = [X[i] for i in idx[:80]]
y_train = [y[i] for i in idx[:80]]
X_test = [X[i] for i in idx[80:]]
y_test = [y[i] for i in idx[80:]]
In [4]:
plt.scatter([i[0] for idx, i in enumerate(X_train) if y_train[idx] == 0],
[i[1] for idx, i in enumerate(X_train) if y_train[idx] == 0],
label=’class 0′, marker=’o’)

plt.scatter([i[0] for idx, i in enumerate(X_train) if y_train[idx] == 1],
[i[1] for idx, i in enumerate(X_train) if y_train[idx] == 1],
label=’class 1′, marker=’s’)

plt.title(‘Training set’)
plt.xlabel(‘feature 1’)
plt.ylabel(‘feature 2’)
plt.xlim([0.0, 7])
plt.ylim([-0.8, 0.8])
plt.legend()
plt.show()

Defining the Perceptron model¶

Below, you need to complete the code of the Perceptron model class based on the framework I provided.
In [6]:
class Perceptron():
def __init__(self, num_features):
self.num_features = num_features
self.weights = [0.0 for i in range(num_features)] #
self.bias = 0. #

def forward(self, x):
linear = sum([x[j]*self.weights[j] for j in range(self.num_features)]) + self.bias #
prediction = 0 #
if linear > 0.:
prediction = 1
return prediction

def backward(self, x, y):
# to compute the prediction error
predictions = self.forward(x)
errors = y – predictions
return errors

def train(self, x, y, epochs):
for e in range(epochs):
for i in range(len(y)):
# to update the weights and bias
errors = self.backward(x[i], y[i])
temp = [errors * x[i][j] for j in range(self.num_features)]
self.weights = [(self.weights[j] + temp[j]) for j in range(self.num_features)]
self.bias += errors

def evaluate(self, x, y):
# to compute the prediction accuracy
predictions = [self.forward(x[i]) for i in range(len(x))]
accuracy = 0
for i in range(len(predictions)):
if y[i] == predictions[i]:
accuracy += 1
accuracy /= len(y)

return accuracy

Training the Perceptron¶

Here, you are asked to train the perceptron for 5 epochs and print out the Perceptron weight parameters and the value of the bias unit after training.
• The code should be relatively similar to the code you’ve seen in the lecture.
In [8]:
ppn = Perceptron(num_features=2)

#
ppn.train(X_train, y_train, epochs=1)

print(‘Model parameters:\n\n’)
print(‘ Weights: %s\n’ % ppn.weights)
print(‘ Bias: %s\n’ % ppn.bias)

Model parameters:

Weights: [1.299999999999999, -0.9000000000000002]

Bias: -3.0

Evaluating the model¶

Compute the prediction accuracy (in percent) for both the training set and the test set.
In [9]:
#

train_acc = ppn.evaluate(X_train, y_train)
print(‘Train set accuracy: %.2f%%’ % (train_acc*100))

Train set accuracy: 100.00%
In [10]:
#

test_acc = ppn.evaluate(X_test, y_test)
print(‘Test set accuracy: %.2f%%’ % (test_acc*100))

Test set accuracy: 100.00%

Decision Boundary¶

Make 2 scatterplots: 1 for the training dataset and 1 for the test dataset. Draw the respective decision boundaries of the perceptron in each plot.
In [12]:
##########################
### 2D Decision Boundary
##########################

#

w, b = ppn.weights, ppn.bias

x_min = 0
y_min = ( (-(w[0] * x_min) – b) / w[1] )

x_max = 6
y_max = ( (-(w[0] * x_max) – b) / w[1] )

fig, ax = plt.subplots(1, 2, sharex=True, figsize=(7, 3))

ax[0].plot([x_min, x_max], [y_min, y_max])
ax[1].plot([x_min, x_max], [y_min, y_max])

ax[0].scatter([i[0] for idx, i in enumerate(X_train) if y_train[idx] == 0],
[i[1] for idx, i in enumerate(X_train) if y_train[idx] == 0],
label=’class 0′, marker=’o’)

ax[0].scatter([i[0] for idx, i in enumerate(X_train) if y_train[idx] == 1],
[i[1] for idx, i in enumerate(X_train) if y_train[idx] == 1],
label=’class 1′, marker=’s’)

ax[1].scatter([i[0] for idx, i in enumerate(X_test) if y_test[idx] == 0],
[i[1] for idx, i in enumerate(X_test) if y_test[idx] == 0],
label=’class 0′, marker=’o’)

ax[1].scatter([i[0] for idx, i in enumerate(X_test) if y_test[idx] == 1],
[i[1] for idx, i in enumerate(X_test) if y_test[idx] == 1],
label=’class 1′, marker=’s’)

ax[0].legend(loc=’upper right’)
ax[1].legend(loc=’upper right’)
plt.show()