<jupyter_start><jupyter_text>Perceptron Implemetation Data set or gate<jupyter_code>import os
import matplotlib.pyplot as plt
import joblib
import numpy as np
import pandas as pd
plt.style.use("fivethirtyeight")

class Perceptron:
    def __init__(self,eta,epochs):
        self.weights = np.random.randn(3) * 1e-4 # weights
        self.eta = eta # learning rate
        self.epochs = epochs # iterations
        
    def _z_outcome(self,inputs,weights):
        return np.dot(inputs,weights)
    
    def activation_function(self,z):
        return np.where(z>0,1,0)
        
        
    def fit(self,X,y):
        self.X = X
        self.y = y
        X_with_bias = np.c_[self.X,-np.ones((len(self.X),1))]
        print(f"x_with_bias : \n{X_with_bias}")
        
        for epoc in range(self.epochs):
            z = self._z_outcome(X_with_bias,self.weights)
            y_hat = self.activation_function(z)
                
            print(f"predicted value after forward pass: \n{y_hat}")
            
            self.error = self.y - y_hat
            print(f"Error : \n{self.error}")
            
            # backword propagation            
            self.weights = self.weights + self.eta * np.dot(X_with_bias.T, self.error)
            print(f"updated epochs after epoch {epoc +1 } >> {self.epochs}:\n{self.weights}")
    
    def predict(self,X):
        X_with_bias = np.c_[X,-np.ones((len(X),1))]
        z = self._z_outcome(X_with_bias,self.weights)
        y_hat = self.activation_function(z)
        return y_hat
        

def prep_data(df,target_col = "y"):
    X = df.drop(target_col,axis = 1)
    y = df[target_col]
    return X,y<jupyter_output><empty_output><jupyter_text>OR<jupyter_code>OR = {
    "A": [0,0,1,1],
    "B": [0,1,0,1],
    "y": [0,1,1,1]
}

df_OR = pd.DataFrame(OR)
df_OR

X,y = prep_data(df_OR)
ETA = 0.01
EPOCHS = 10
model_or = Perceptron(eta = ETA, epochs=EPOCHS)

model_or.fit(X,y)
model_or.predict(X)<jupyter_output>x_with_bias : 
[[ 0.  0. -1.]
 [ 0.  1. -1.]
 [ 1.  0. -1.]
 [ 1.  1. -1.]]
predicted value after forward pass: 
[1 1 1 1]
Error : 
0   -1
1    0
2    0
3    0
Name: y, dtype: int64
updated epochs after epoch 1 >> 10:
[7.35325501e-05 7.13490154e-05 9.99766552e-03]
predicted value after forward pass: 
[0 0 0 0]
Error : 
0    0
1    1
2    1
3    1
Name: y, dtype: int64
updated epochs after epoch 2 >> 10:
[ 0.02007353  0.02007135 -0.02000233]
predicted value after forward pass: 
[1 1 1 1]
Error : 
0   -1
1    0
2    0
3    0
Name: y, dtype: int64
updated epochs after epoch 3 >> 10:
[ 0.02007353  0.02007135 -0.01000233]
predicted value after forward pass: 
[1 1 1 1]
Error : 
0   -1
1    0
2    0
3    0
Name: y, dtype: int64
updated epochs after epoch 4 >> 10:
[ 2.00735326e-02  2.00713490e-02 -2.33448496e-06]
predicted value after forward pass: 
[1 1 1 1]
Error : 
0   -1
1    0
2    0
3    0
Name: y, dtype: int64
updated epochs after epoch 5 >> 10:
[0.02007353 0.02007135 0.00999767]
predic[...]<jupyter_text>AND<jupyter_code>AND = {
    "A": [0,0,1,1],
    "B": [0,1,0,1],
    "y": [0,0,0,1]
}

df_AND = pd.DataFrame(AND)
df_AND

X,y = prep_data(df_AND)
ETA = 0.01
EPOCHS = 10
model_or = Perceptron(eta = ETA, epochs=EPOCHS)

model_or.fit(X,y)
model_or.predict(X)<jupyter_output>x_with_bias : 
[[ 0.  0. -1.]
 [ 0.  1. -1.]
 [ 1.  0. -1.]
 [ 1.  1. -1.]]
predicted value after forward pass: 
[0 1 0 1]
Error : 
0    0
1   -1
2    0
3    0
Name: y, dtype: int64
updated epochs after epoch 1 >> 10:
[ 7.11624898e-06 -9.95644238e-03  1.00131207e-02]
predicted value after forward pass: 
[0 0 0 0]
Error : 
0    0
1    0
2    0
3    1
Name: y, dtype: int64
updated epochs after epoch 2 >> 10:
[1.00071162e-02 4.35576159e-05 1.31206511e-05]
predicted value after forward pass: 
[0 1 1 1]
Error : 
0    0
1   -1
2   -1
3    0
Name: y, dtype: int64
updated epochs after epoch 3 >> 10:
[ 7.11624898e-06 -9.95644238e-03  2.00131207e-02]
predicted value after forward pass: 
[0 0 0 0]
Error : 
0    0
1    0
2    0
3    1
Name: y, dtype: int64
updated epochs after epoch 4 >> 10:
[1.00071162e-02 4.35576159e-05 1.00131207e-02]
predicted value after forward pass: 
[0 0 0 1]
Error : 
0    0
1    0
2    0
3    0
Name: y, dtype: int64
updated epochs after epoch 5 >> 10:
[1.00071162e-02 4.35[...]<jupyter_text>XOR<jupyter_code>XOR = {
    "A": [0,0,1,1],
    "B": [0,1,0,1],
    "y": [0,1,1,0]
}

df_XOR = pd.DataFrame(XOR)
df_XOR

X,y = prep_data(df_XOR)
ETA = 0.01
EPOCHS = 10
model_or = Perceptron(eta = ETA, epochs=EPOCHS)

model_or.fit(X,y)
model_or.predict(X)<jupyter_output>x_with_bias : 
[[ 0.  0. -1.]
 [ 0.  1. -1.]
 [ 1.  0. -1.]
 [ 1.  1. -1.]]
predicted value after forward pass: 
[0 0 1 1]
Error : 
0    0
1    1
2    0
3   -1
Name: y, dtype: int64
updated epochs after epoch 1 >> 10:
[-0.00986156  0.00011289  0.00013145]
predicted value after forward pass: 
[0 0 0 0]
Error : 
0    0
1    1
2    1
3    0
Name: y, dtype: int64
updated epochs after epoch 2 >> 10:
[ 0.00013844  0.01011289 -0.01986855]
predicted value after forward pass: 
[1 1 1 1]
Error : 
0   -1
1    0
2    0
3   -1
Name: y, dtype: int64
updated epochs after epoch 3 >> 10:
[-0.00986156  0.00011289  0.00013145]
predicted value after forward pass: 
[0 0 0 0]
Error : 
0    0
1    1
2    1
3    0
Name: y, dtype: int64
updated epochs after epoch 4 >> 10:
[ 0.00013844  0.01011289 -0.01986855]
predicted value after forward pass: 
[1 1 1 1]
Error : 
0   -1
1    0
2    0
3   -1
Name: y, dtype: int64
updated epochs after epoch 5 >> 10:
[-0.00986156  0.00011289  0.00013145]
predicted value after fo[...]
{'fraction_non_alphanumeric': 0.1327449567723343, 'fraction_numerical': 0.14048991354466858, 'mean_word_length': 2.438390092879257, 'pattern_counts': {'":': 9, '<': 14, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '6069', 'license': 'MIT', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2394, 'n_words': 676, 'path': 'Perceptron/Perceptron.ipynb', 'repo_name': 'bhanu0925/DeepLearning', 'size': '5552'}