Loading W Code...
7
Topics
Deep
Learning
Indian Context
Aadhaar fingerprint matching, Hindi OCR, UPI fraud detection
Concept Level: Beginner
# THE PERCEPTRON
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, learning_rate=0.1, n_iterations=100):
self.lr = learning_rate
self.n_iter = n_iterations
self.weights = None
self.bias = None
self.errors = []
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(self.n_iter):
errors = 0
for xi, yi in zip(X, y):
# Predict
linear_output = np.dot(xi, self.weights) + self.bias
y_pred = 1 if linear_output >= 0 else 0
# Update weights
update = self.lr * (yi - y_pred)
self.weights += update * xi
self.bias += update
errors += int(update != 0)
self.errors.append(errors)
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
return np.where(linear_output >= 0, 1, 0)
# AND Gate
print("="*60)
print("PERCEPTRON: LEARNING LOGIC GATES")
print("="*60)
X_and = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_and = np.array([0, 0, 0, 1]) # AND gate
p_and = Perceptron()
p_and.fit(X_and, y_and)
print("\nAND Gate:")
print(f"Weights: {p_and.weights}, Bias: {p_and.bias:.2f}")
print(f"Predictions: {p_and.predict(X_and)} (Expected: {y_and})")
# OR Gate
y_or = np.array([0, 1, 1, 1]) # OR gate
p_or = Perceptron()
p_or.fit(X_and, y_or)
print("\nOR Gate:")
print(f"Weights: {p_or.weights}, Bias: {p_or.bias:.2f}")
print(f"Predictions: {p_or.predict(X_and)} (Expected: {y_or})")
# XOR Gate (will fail!)
y_xor = np.array([0, 1, 1, 0]) # XOR gate
p_xor = Perceptron(n_iterations=100)
p_xor.fit(X_and, y_xor)
print("\nXOR Gate (Perceptron FAILS!):")
print(f"Predictions: {p_xor.predict(X_and)} (Expected: {y_xor})")
print(f"Final errors per epoch: {p_xor.errors[-1]}")
# Visualize AND gate decision boundary
plt.figure(figsize=(12, 4))
for idx, (gate, y, p, title) in enumerate([
('AND', y_and, p_and, 'AND Gate (Solvable)'),
('OR', y_or, p_or, 'OR Gate (Solvable)'),
('XOR', y_xor, p_xor, 'XOR Gate (NOT Solvable!)')
]):
plt.subplot(1, 3, idx+1)
plt.scatter(X_and[:, 0], X_and[:, 1], c=y, cmap='RdYlBu',
s=200, edgecolor='black', linewidth=2)
# Decision boundary
x1 = np.linspace(-0.5, 1.5, 100)
if p.weights[1] != 0:
x2 = -(p.weights[0] * x1 + p.bias) / p.weights[1]
plt.plot(x1, x2, 'k--', linewidth=2, label='Decision Boundary')
plt.xlim(-0.5, 1.5)
plt.ylim(-0.5, 1.5)
plt.xlabel('x₁')
plt.ylabel('x₂')
plt.title(title)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()