# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Day 1

https://github.com/c17hawke/Perceptron_implimentation_FSDS

# !pip install joblib

# +
import os
import pandas as pd
import matplotlib.pyplot as plt
import joblib
import numpy as np

plt.style.use('fivethirtyeight')


# -

class Perceptron:
    def __init__(self, eta: float=None, epochs: int=None):
        self.weights = np.random.randn(3) * 1e-4
        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 epoch in range(self.epochs):
            print("--"*10)
            print(f"for epoch >> {epoch+1}")
            print("--"*10)
            
            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}")
            
            self.weights = self.weights + self.eta * np.dot(x_with_bias.T, self.error)
            print(f"updated weights afetr epoch: {epoch+1}/{self.epochs}: \n{self.weights}")
            print(f"##"*10)
            
    def predict(self,x):
        x_with_bias = np.c_[x, -np.ones((len(x), 1))]
        z = self._z_outcome(x_with_bias, self.weights)
        return self.activation_function(z)


# +
OR = {
    'x1':[0,0,1,1],
    'x2':[0,1,0,1],
    'y':[0,1,1,1]
}

df_OR = pd.DataFrame(OR)

df_OR


# -

def prepare_data(df, target_col='y'):
    x = df.drop(target_col, axis=1)
    y = df[target_col]
    
    return x,y


# +
x, y = prepare_data(df_OR)

ETA = 0.1
EPOCHS = 10

model_or = Perceptron(eta=ETA, epochs=EPOCHS)

model_or.fit(x,y)
# -

model_or.predict(x=[[1,0]])

# +
AND = {
    'x1':[0,0,1,1],
    'x2':[0,1,0,1],
    'y':[0,0,0,1]
}

df_AND = pd.DataFrame(AND)

df_AND

x, y = prepare_data(df_AND)

ETA = 0.1
EPOCHS = 10

model_and = Perceptron(eta=ETA, epochs=EPOCHS)

model_and.fit(x,y)

# +
XOR = {
    'x1':[0,0,1,1],
    'x2':[0,1,0,1],
    'y':[0,1,1,0]
}

df_XOR = pd.DataFrame(XOR)

df_XOR

x, y = prepare_data(df_XOR)

ETA = 0.1
EPOCHS = 10

model_xor = Perceptron(eta=ETA, epochs=EPOCHS)

model_xor.fit(x,y)

# +
NAND = {
    'x1':[0,0,1,1],
    'x2':[0,1,0,1],
    'y':[1,0,0,0]
}

df_NAND = pd.DataFrame(NAND)

df_NAND

x, y = prepare_data(df_NAND)

ETA = 0.1
EPOCHS = 10

model_nand = Perceptron(eta=ETA, epochs=EPOCHS)

model_nand.fit(x,y)
# -



StarcoderdataJupyterScriptsDedupFiltered