0% found this document useful (0 votes)
9 views2 pages

Ann 4

Uploaded by

Anushka Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Ann 4

Uploaded by

Anushka Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [2]: import numpy as np

import matplotlib.pyplot as plt

# Define the training data


x = np.array([[-2, 4],
[4, 1],
[1, 6],
[2, 4],
[6, 2],
[4, 7]])

y = np.array([-1, -1, -1, 1, 1, 1])

# Initialize the weights and bias


w = np.zeros(x.shape[1])
b = 0

def perceptron(X, y, w, b, learning_rate=0.1, num_epochs=100):


for epoch in range(num_epochs):
for i in range(X.shape[0]):
z = np.dot(X[i], w) + b
if z > 0:
y_pred = 1
else:
y_pred = -1
if y_pred != y[i]:
w = w + learning_rate * y[i] * X[i]
b = b + learning_rate * y[i]
return w, b

w, b = perceptron(x, y, w, b)

x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1


y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

z = np.sign(np.dot(np.c_[xx.ravel(), yy.ravel()], w) + b)
z = z.reshape(xx.shape)

plt.contourf(xx, yy, z, alpha=0.4)


plt.scatter(x[:, 0], x[:, 1], c=y, s=20, edgecolor='k')
plt.title('Perceptron Decision Region')
plt.xlabel('X1')
plt.ylabel('X2')
plt.show()
In [ ]:

You might also like