You are on page 1of 4

OR gate

Sample Program:

from sklearn.linear_model import Perceptron

import numpy as np

# Define the input data (X) and corresponding target labels (y) for OR gate

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

[0, 1],

[1, 0],

[1, 1]])
y = np.array([0, 1, 1, 1]) # OR gate truth table

# Create and train the perceptron model

perceptron = Perceptron(random_state=42)

perceptron.fit(X, y)

# Make predictions

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

predictions = perceptron.predict(test_data)

# Display the predictions

for i in range(len(test_data)):

print(f"Input: {test_data[i]}, Predicted Output: {predictions[i]}")


AND Gate

from sklearn.linear_model import Perceptron

import numpy as np

# Define the input data (X) and corresponding target labels (y) for OR gate

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

[1, 0],

[1, 1]])

y = np.array([0, 1, 1, 1]) # OR gate truth table

# Create and train the perceptron model

perceptron = Perceptron(random_state=42)

perceptron.fit(X, y)

# Make predictions

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

predictions = perceptron.predict(test_data)

# Display the predictions

for i in range(len(test_data)):

print(f"Input: {test_data[i]}, Predicted Output: {predictions[i]}")

Chapter 5: Sentiment analysis with the perceptron algorithm


https://github.com/luisguiserrano/manning/blob/master/Chapter_5_Perceptron_Algorithm/Coding_per
ceptron_algorithm.ipynb

You might also like