<jupyter_start><jupyter_text>Logistic regression implementation from scratch Logistic regression is the first of its kind classification technique that can easily classify into multiple categories and that too by usig the same linear model techniques. After getting the output using the linear model, we run it through a sigmoid function and get are class labels.<jupyter_code>%load_ext autoreload
%autoreload 2

%matplotlib inline
import numpy as np
import pandas as pd
import math
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression as LogR
from sklearn.metrics import r2_score
from scipy.special import expit as sigmoid<jupyter_output><empty_output><jupyter_text>Basic StructureAssume 0's to be weights for the linear model.for each iteration:        find the output of the linear model using existing weights    find the delta wrt the true values.    update the weights using a learning rate.     for predictions:    simply use sigmoid of the output using the linear model<jupyter_code>class LogisticRegression():
    def __init__(self, x, y, n_iter=1500, lr=0.01):
        self.w = np.zeros((x.shape[1], 1))
        self.lr = lr/x.shape[0]
        self.n_iter = n_iter
        self.x, self.y = x, y

    def fit(self):
            for i in range(self.n_iter):
                predictions = self.predict(self.x)
                delta = self.y - predictions
                self.w += (self.lr * (self.x.T @ delta))
        
    def predict(self, x):
            l = x @ self.w
            return np.round(sigmoid(l))
data = load_breast_cancer()
d = data.data
X_train = pd.DataFrame(d, columns=data.feature_names)
y_train = data.target[:, None]
X_train = (X_train - X_train.mean())/X_train.std()
lr = LogisticRegression(X_train, y_train)
lr.fit()
r2_score(y_train, lr.predict(X_train))
sklearn_lr = LogR()
sklearn_lr.fit(X_train, data.target)
sklearn_lr.score(X_train, data.target)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup