ML Kli

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

06/05/2024, 18:44 ML 05

In [1]: import numpy as np

In [2]: class Perceptron:


def __init__ (self, num_inputs, activation_threshold):
self.weight = np.zeros(num_inputs)
self.bias = 0
self.activation_threshold = activation_threshold
def activate(self, x):
return 1 if x>= self.activation_threshold else 0
def predict(self, inputs):
weighted_sum = np.dot(inputs, self.weights) + self.bias
return self.activate(weighted_sum)

In [10]: # AND Gate


and_gate = Perceptron(2, activation_threshold=1.5)
and_gate.weights = np.array([1, 1])
and_gate.bias = -0.5

In [11]: # OR Gate
or_gate = Perceptron(2, activation_threshold=0.5)
or_gate.weights = np.array([1, 1])
or_gate.bias = -0.5

In [12]: # NOT Gate


not_gate = Perceptron(1, activation_threshold=0.5)
not_gate.weights = np.array([-1])
not_gate.bias = 0.5

In [13]: inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]

In [14]: print("AND Gate:")


for input_pair in inputs:
output = and_gate.predict(np.array(input_pair))
print(f"{input_pair} -> {output}")

AND Gate:
(0, 0) -> 0
(0, 1) -> 0
(1, 0) -> 0
(1, 1) -> 1

In [15]: print("OR Gate:")


for input_pair in inputs:
output = or_gate.predict(np.array(input_pair))
print(f"{input_pair} -> {output}")

OR Gate:
(0, 0) -> 0
(0, 1) -> 1
(1, 0) -> 1
(1, 1) -> 1

In [9]: print("NOT Gate:")


for input_value in [0, 1]:
output = not_gate.predict(np.array([input_value]))
print(f"{input_value} -> {output}")

file:///D:/DOWNLOAD/ML 05.html 1/2


06/05/2024, 18:44 ML 05

NOT Gate:
0 -> 1
1 -> 0

In [ ]:

file:///D:/DOWNLOAD/ML 05.html 2/2

You might also like