<jupyter_start><jupyter_text>x1, x2, x3, y1,  3,  5,  12,  4,  4,  0.           1 .           1            0            (satır, sütun) => (5, 1) => (5,)               => (1, 5)w = weightsb = biasz = w1.x1 + w2.x2 + w3.x3 + bsigmoid = 1 / 1 + exp(-z)y_hat =   1 / 1 + exp(- (w1.x1 + w2.x2 + w3.x3 + b))0, 1y_hat >= 0.5: y_hat = 1y_hat < 0.5: y_hat = 0    X (nx, m)Wb<jupyter_code>class binClass():
    
    def __init__(self, num_iter=10000, learning_rate=5e-5):
        self.num_iters = num_iter
        self.learning_rate = learning_rate
        
    def fit(self, X_train, y_train):
        X_train = X_train.T.astype(np.float128)
        y_train = y_train.values.reshape((1,-1)).astype(np.float128)
        
        W, b = self.initParams(X_train.shape[0])
        costs = []
        
        for i in range(self.num_iters):
            grads, cost = self.prop(X_train, y_train, W, b)
            
            dW = grads['dW']
            db = grads['db']
            
            W, b = self.update(W, b, dW, db, self.learning_rate)
            
            if i%1000 == 0:
                costs.append(cost)
                print(f'{i}th iteration, cost: {cost}')

        params = {'W' : W, 
                  'b' : b}
        
        grads = {'dW': dW,
                 'db': db}
        
        self.params = params
        
        return params, grads, cost
    
    def predict(self, X_test):
        X_test = X_test.T.astype(np.float128)
        
        W = self.params['W']
        b = self.params['b']
        
        A = self.forwProp(X_test, W, b)
        preds = np.zeros((1, X_test.shape[1]))
        
        for i in range(A.shape[1]):
            if A[0, i] < 0.5:
                preds[0, i] = 0
            if A[0 ,i] >= .5:
                preds[0, i] = 1
        
        preds = np.squeeze(preds)
        
        return preds
        
    def initParams(self, dim):
        W = np.zeros((dim, 1))
        b = 0
        
        return W, b
    
    def sigmoid(self, Z):
        Z = 1 / (1 + np.exp(-Z))
        
        return (Z)
    
    def forwProp(self, X_train, W, b):
        A = self.sigmoid(np.dot(W.T, X_train) + b)
        
        return A
    
    def calcCost(self, m, Y, A):
        loss = -(np.sum(np.multiply(Y, np.log(A)) + np.multiply((1-Y), np.log(1-A)))) / m
        
        return loss
    
    def calcGrads(self, m, X_train, y_train, A):
        dW = np.dot(X_train, (A-y_train).T) / m
        db = np.sum(A-y_train) / m
        
        return dW, db
    
    def update(self, W, b, dW, db, learning_rate):
        W = W - (learning_rate * dW)
        b = b - (learning_rate * db)
        
        return W, b
    
    def prop(self, X_train, y_train, W, b):
        
        A = self.forwProp(X_train, W, b)
        
        cost = self.calcCost(X_train.shape[0], y_train, A)
        dW, db = self.calcGrads(X_train.shape[0], X_train, y_train, A)
        
        grad = {'dW' : dW,
                'db' : db}
        
        return grad, cost
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score, confusion_matrix
df = pd.read_csv('../data/BinaryClassification/nba_logreg.csv')
df.head()
df.dropna(axis=0, how='any', inplace=True)
X = df.drop(columns=['Name', 'TARGET_5Yrs'])
y = df['TARGET_5Yrs']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)
clf = binClass(num_iter=50000, learning_rate=8e-7)
clf.fit(X_train, y_train)
preds = clf.predict(X_test)
accuracy_score(y_test, preds)
confusion_matrix(y_test, preds)
from sklearn.linear_model import LogisticRegression
clf_sk = LogisticRegression(max_iter=50000)
clf_sk.fit(X_train, y_train)
preds_sk = clf_sk.predict(X_test)
accuracy_score(y_test, preds_sk)
confusion_matrix(y_test, preds_sk)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13637503133617448, 'fraction_numerical': 0.025821007771371272, 'mean_word_length': 1.835820895522388, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '653806', 'license': 'MIT', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1529, 'n_words': 326, 'path': 'logistic_regression_from_scratch/LogRess.ipynb', 'repo_name': 'makermotion/youtube_mm', 'size': '3989'}