0% found this document useful (0 votes)
17 views1 page

Import Numpy As NP

This document contains a Python implementation of a Perceptron class for binary classification tasks. It includes methods for training the perceptron on logical gates (AND, OR, XOR) and predicting outputs based on input data. The perceptron adjusts its weights based on the error between predicted and actual outputs during training.

Uploaded by

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

Import Numpy As NP

This document contains a Python implementation of a Perceptron class for binary classification tasks. It includes methods for training the perceptron on logical gates (AND, OR, XOR) and predicting outputs based on input data. The perceptron adjusts its weights based on the error between predicted and actual outputs during training.

Uploaded by

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

import numpy as np

def step_function(x):
return 1 if x >= 0 else 0

class Perceptron:
def __init__(self, input_size, learning_rate=0.1, epochs=100):
self.weights = np.zeros(input_size + 1)
self.learning_rate = learning_rate
self.epochs = epochs

def predict(self, x):


x_with_bias = np.insert(x, 0, 1)
linear_output = np.dot(self.weights, x_with_bias)
return step_function(linear_output)

def train(self, X, y):


for _ in range(self.epochs):
for i in range(len(X)):
x_with_bias = np.insert(X[i], 0, 1)
y_pred = step_function(np.dot(self.weights, x_with_bias))
error = y[i] - y_pred
self.weights += self.learning_rate * error * x_with_bias

def train_gate(gate_name, X, y):


print(f"Training {gate_name} gate...")
perceptron = Perceptron(input_size=2)
perceptron.train(X, y)
print(f"Weights for {gate_name} gate: {perceptron.weights}")
for x, target in zip(X, y):
pred = perceptron.predict(x)
print(f"Input: {x}, Predicted: {pred}, Target: {target}")
print()
return perceptron

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])


y_and = np.array([0, 0, 0, 1])
y_or = np.array([0, 1, 1, 1])
y_xor = np.array([0, 1, 1, 0])

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy