<jupyter_start><jupyter_text>Classifier<jupyter_code>class Classification:
    def __init__(self, eta = 0.1, it = 10, opt='N'):
        self.eta = eta
        self.it = it
        self.nc = True
        self.c = False
        self.ch = []
        self.t = 1e-9
        self.ta = 0.99
        self.theta=[]
        self.opt = opt
        
        
    def sig(x):
        return 1/(1+np.exp(x))
        
    
    def J(self,x,y,theta):
        t = np.dot(x,theta)
        z = Classification.sig(t)
        return (-y * np.log(z) - (1 - y) * np.log(1 - z)).mean()
    
    def GD(x,y,theta,eta):
        t = np.dot(x,theta)
        z = Classification.sig(t)
        grad = np.dot(x.T, (z - y)) / y.shape[0]
        theta = theta + eta*grad
        return theta
    
    def N(x,y,theta):
        t = np.dot(x,theta)
        z = Classification.sig(t)
        w = np.diag(z*(1-z))
        h = x.T.dot(w).dot(x)
        grad = np.dot(np.linalg.inv(h),np.dot(x.T,(y-z)))
        theta =  theta - grad
        return theta
    
    def fit(self,x,y,xt,yt):
        i = np.ones((x.shape[0], 1))
        x = np.concatenate((i,x), axis=1)
        self.theta = np.zeros(x.shape[1])
        
        eta = self.eta
        itc=0
        while self.nc:
            Jc = self.J(x,y,self.theta)
            self.ch.append(Jc)
            if np.isnan(Jc):
                print("Divergent")
                break
            itc+=1
            print(f"{itc}")
            if self.opt == 'N':
                theta = Classification.N(x,y,self.theta)
            elif self.opt =='GD': 
                theta = Classification.GD(x,y,self.theta,self.eta)
                
            
            if itc>1:
                conv = self.CC(self.ch[-1],Jc,self.t,xt,yt,itc)
                
                if conv:
                    print(f"Cost: {Jc}, Iteration: {itc}, Theta: {theta}")
            self.theta = theta
            self.ch.append(Jc)
            
        
            
    def predict(self,x):
        i = np.ones((x.shape[0], 1))
        x = np.concatenate((i,x), axis=1)
        return Classification.sig(np.dot(x,self.theta))
    
    def CC(self,pj,j,t,xt,yt,itc):
        cc = np.abs(pj - j)
        if self.opt=='N':
            if (np.any(cc < t)):
                self.c = True
                self.nc = False
            if (self.opt == 'N'):
                if (np.isnan(j)):
                    print("Divergent")
                    self.c = True
                    self.nc = False
        else:
            yp = Classification.predict(self,xt)
            for i in range(yp.shape[0]):
                if yp[i]>0.5:
                    yp[i]=1
                else:
                    yp[i]=0
            if accuracy_score(y_test,yp) > self.ta:
                self.c = True
                self.nc = False
            elif itc >= self.it:
                self.c = True
                self.nc = False
                print(accuracy_score(y_test,yp))
        return self
        
        
    
    def getw(self):
        return self.theta
    def geth(self):
        return self.ch<jupyter_output><empty_output><jupyter_text>Preparing the data<jupyter_code>p = pd.read_csv("penguins.csv")
p.head()
p = p[['bill_length_mm','bill_depth_mm','species']].dropna()
p=p[0:274]
mapping = {'Adelie':0,'Gentoo':1}
p=p.replace({'species':mapping})
p=p.values
y = p[:,2]
x = p[:,0:2]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
for i in range(X_train.shape[0]):
    if y_train[i] == 1 :
        c1 = 'r'
        
    else:
        c1 = 'b'
        
    plt.scatter(X_train[i,0],X_train[i,1],marker='.',c=c1)
plt.xlabel("Bill Length")
plt.ylabel("Bill Depth")
c = Classification()<jupyter_output><empty_output><jupyter_text>Newton's Method<jupyter_code>c.__init__()
c.fit(X_train,y_train,X_test,y_test)
x1 = np.linspace(30,55,100)
w = c.getw()
y1 = -(w[1]/w[2])*x1 - (w[0]/w[2])
plt.scatter(x1,y1,marker='.')
for i in range(X_test.shape[0]):
    if y_test[i] == 1 :
        c1 = 'r'
        
    else:
        c1 = 'b'
        
    plt.scatter(X_test[i,0],X_test[i,1],marker='.',c=c1)
plt.xlabel("Bill Length")
plt.ylabel("Bill Depth")

plt.plot(c.geth())
plt.ylabel("Loss Function")
plt.xlabel("# of Iterations")<jupyter_output><empty_output><jupyter_text>Gradient Descent<jupyter_code>c.__init__(eta=0.1,opt='GD',it=100)
c.fit(X_train,y_train,X_test,y_test)
x1 = np.linspace(30,55,100)
w = c.getw()
y1 = -(w[1]/w[2])*x1 - (w[0]/w[2])
plt.scatter(x1,y1,marker='.')
for i in range(X_test.shape[0]):
    if y_test[i] == 1 :
        c1 = 'r'
        
    else:
        c1 = 'b'
        
    plt.scatter(X_test[i,0],X_test[i,1],marker='.',c=c1)
plt.xlabel("Bill Length")
plt.ylabel("Bill Depth")

c.__init__(eta=0.01,opt='GD',it=100)
c.fit(X_train,y_train,X_test,y_test)
x1 = np.linspace(30,55,100)
w = c.getw()
y1 = -(w[1]/w[2])*x1 - (w[0]/w[2])
plt.scatter(x1,y1,marker='.')
for i in range(X_test.shape[0]):
    if y_test[i] == 1 :
        c1 = 'r'
        
    else:
        c1 = 'b'
        
    plt.scatter(X_test[i,0],X_test[i,1],marker='.',c=c1)
plt.xlabel("Bill Length")
plt.ylabel("Bill Depth")

plt.plot(c.geth())
plt.ylabel("Loss Function")
plt.xlabel("# of Iterations")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17206935544666416, 'fraction_numerical': 0.024877497173011683, 'mean_word_length': 1.8273841236014918, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 21, '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': '102887', 'license': 'MIT', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2078, 'n_words': 334, 'path': 'P6a.ipynb', 'repo_name': 'ANadali/PR-HW3', 'size': '5306'}