Real-Time Analytics Lectures for SGH students

Laboratory 5 - Neural networks from scratch

import numpy as np
import pandas as pd

how AND logic gate work ??

test

child = Perceptron()
child.fit(X,y)

print(child.errors_)
print(child.w_)

child.predict(np.array([0,1]))

child.predict(np.array([0,0],[0,2],[3,4]))


class Perceptron(object):
    
    def __init__(self, eta=0.01, n_iter=10):
        self.eta = eta
        self.n_iter = n_iter
    
    def fit(self, X, y):
        self.w_ = np.zeros(1+X.shape[1])
        self.errors_ = []
        
        for _ in range(self.n_iter):
            errors = 0
            for xi, target in zip(X,y):
                
                update = self.eta*(target - self.predict(xi))
                print(update)
                self.w_[1:] += update*xi
                self.w_[0] += update
                print(self.w_[1:], self.w_[0])
                
                errors += int(update != 0.0)
                
            self.errors_.append(errors)
            print(self.errors_)
        return self
    
    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]
    
    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, -1)
# run code

a = Perceptron()

X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = [-1,-1,-1,1]


a.fit(X,y)
a.w_

import joblib
joblib.dump(a, 'model.pkl')

ADeline model

class Adaline():
    '''Klasyfikator  - ADAptacyjny LIniowy NEuron'''
    def __init__(self, eta=0.01, n_iter=10):
        self.eta = eta
        self.n_iter = n_iter

    def fit(self, X,y):
        self.w_ = np.zeros(1+X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            net_input = self.net_input(X)
            output = self.activation(X)
            errors = (y-output)
            self.w_[1:] += self.eta * X.T.dot(errors)
            self.w_[0] += self.eta * errors.sum()
            cost = (errors**2).sum() / 2.0
            self.cost_.append(cost)
        return self

    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def activation(self, X):
        return self.net_input(X)

    def predict(self, X):
        return np.where(self.activation(X) >= 0.0, 1, -1) 


ada = Adaline()
ada.fit(X,y)

simple flask app

from flask import Flask
from flask import request
from flask import jsonify

app = Flask(__name__)

@app.route('/')
def hello():
    return "<b>hello world</b>"

if __name__ == '__main__':
    app.run()