@@ -0,0 +1,1616 @@
+import numpy as np%0A# Setting the random seed, feel free to change it and see different solutions.%0Anp.random.seed(42)%0A%0Adef stepFunction(t):%0A    if t %3E= 0:%0A        return 1%0A    return 0%0A%0Adef prediction(X, W, b):%0A    return stepFunction((np.matmul(X,W)+b)%5B0%5D)%0A%0A# The function should receive as inputs the data X, the labels y,%0A# the weights W (as an array), and the bias b,%0A# update the weights and bias W, b, according to the perceptron algorithm,%0A# and return W and b.%0Adef perceptronStep(X, y, W, b, learn_rate = 0.01):%0A    # Fill in code%0A    for i, x in enumerate(X):%0A        p = prediction(x, W, b)%0A        if y%5Bi%5D != p:%0A            delta = -1 if p == 1 else 1%0A            for j in range(len(W)):%0A                W%5Bj%5D%5B0%5D += delta * learn_rate * x%5Bj%5D%0A            b += delta * learn_rate%0A%0A    return W, b%0A    %0A# This function runs the perceptron algorithm repeatedly on the dataset,%0A# and returns a few of the boundary lines obtained in the iterations,%0A# for plotting purposes.%0A# Feel free to play with the learning rate and the num_epochs,%0A# and see your results plotted below.%0Adef trainPerceptronAlgorithm(X, y, learn_rate = 0.01, num_epochs = 25):%0A    x_min, x_max = min(X.T%5B0%5D), max(X.T%5B0%5D)%0A    y_min, y_max = min(X.T%5B1%5D), max(X.T%5B1%5D)%0A    W = np.array(np.random.rand(2,1))%0A    b = np.random.rand(1)%5B0%5D + x_max%0A    # These are the solution lines that get plotted below.%0A    boundary_lines = %5B%5D%0A    for i in range(num_epochs):%0A        # In each epoch, we apply the perceptron step.%0A        W, b = perceptronStep(X, y, W, b, learn_rate)%0A        boundary_lines.append((-W%5B0%5D/W%5B1%5D, -b/W%5B1%5D))%0A    return boundary_lines%0A

Create perceptron.py