<jupyter_start><jupyter_text>0. Imports<jupyter_code>import pandas as pd
import numpy as np

from sklearn import preprocessing

import matplotlib.pyplot as plt
class AdalineSGD(object):
    
    def __init__(self, rychlost_ucenia=0.01, pocet_iteracii = 100, shuffle = True, random_state=None):
       
        self.rychlost_ucenia = rychlost_ucenia
        self.pocet_iteracii = pocet_iteracii
        self.w_initialized = False
        self.shuffle = shuffle
        if random_state:
            seed(random_state)
        
    def fit(self,X,y):
        
        self._initialize_weights(X.shape[1])
        self.cost_=[]
        
        for i in range (self.pocet_iteracii):
            if self.shuffle:
                X,y = self._shuffle(X,y)
            cost =[]
            
            for xi,target in zip(X,y):
                cost.append(self._update_weights(xi,target))
            avg_cost = sum(cost)/len(y)
            self.cost_.append(avg_cost)
        return self
    
    def partial_fit(self,X,y):
        if not self.w_initialized:
            self._initialize_weights(X.shape[1])
        if y.ravel().shape[0]>1:
             for xi,target in zip(X,y):
                self._update_weights(xi,target)
        else:
            self._update_weights(X,y)
        return self

    def _shuffle(self,X,y):
        r =np.random.permutation(len(y))
        return X[r],y[r]
    
    def _initialize_weights(self,m):
        self.w_ =np.zeros(1+m)
        self.w_initialized = True
        
    def _update_weights(self,xi,target):
        
        output = self.net_input(xi)
        error = (target-output)  
        self.w_[1:] += self.rychlost_ucenia *xi.dot(error)
        self.w_[0] += self.rychlost_ucenia *error
        
        cost = 0.5*error**2
        return cost
        
    def net_input(self,X):
        return np.dot(X,self.w_[1:])+self.w_[0]
    
    def linearna_aktivacia(self,X):
        return self.net_input(X)
    
    def predikcia(self,X):
        return np.where(self.linearna_aktivacia(X)>=0.0,1,-1) 
# dataframe (add own path)
data = pd.read_csv('/content/drive/MyDrive/Škola/NS/cvicenie_5/kosatec_data.csv', header = None)
data
maticaX_trenovacie = data.iloc[0:80,[0,2]].values

maticaX_testovacie = data.iloc[80:100,[0,2]].values

y = np.where((data.iloc[80:100,4]) == 'kosatec_sibirsky',-1,1)
x_stdTrenovacie = preprocessing.scale(maticaX_trenovacie)
x_stdTestovacie = preprocessing.scale(maticaX_testovacie)
# pomiesanie
np.random.shuffle(maticaX_trenovacie)
np.random.shuffle(maticaX_testovacie)
# pomiesanie
np.random.shuffle(x_stdTrenovacie)
np.random.shuffle(x_stdTestovacie)
# create object
adalineSGD = AdalineSGD()
adalineSGD.partial_fit(maticaX_trenovacie,y)
# train (testing)
adalineSGD.fit(maticaX_testovacie,y)

adalineSGD.rychlost_ucenia = 0.01
plt.plot(adalineSGD.cost_)
plt.title("rychlost ucenia 0.01")
adalineSGD.rychlost_ucenia = 0.0005
plt.plot(adalineSGD.cost_)
plt.title("rychlost ucenia 0.0005")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11811811811811812, 'fraction_numerical': 0.02002002002002002, 'mean_word_length': 2.7428214731585516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '652613', 'license': 'MIT', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1133, 'n_words': 165, 'path': 'cvicenie_5/Untitled0.ipynb', 'repo_name': 'Ludovit-Laca/NS-cvicenia', 'size': '2997'}