ML logistic regression - assignment 2

# set up the env

import pytest
import ipytest
import unittest
import numpy as np

ipytest.autoconfig()

10. ML logistic regression - assignment 2#

10.1. Logistic Regression from scratch#

class MyOwnLogisticRegression:
    def __init__(self, learning_rate=0.001, n_iters=1000):
        self.lr = learning_rate
        self.n_iters = n_iters
        self.weights = None
        self.bias = None

    def fit(self, X, y):
        n_samples, n_features = X.shape

        # init parameters
        self.weights = np.zeros(n_features)
        self.bias = 0

        # gradient descent
        for _ in range(self.n_iters):
            # approximate y with linear combination of weights and x, plus bias
            linear_model = np.dot(X, self.___) + self.___
            # apply sigmoid function
            y_predicted = self._sigmoid(linear_model)

            # compute gradients
            dw = (1 / n_samples) * np.dot(X.T, (___ - ___))
            db = (1 / n_samples) * np.sum(___ - ___)
            # update parameters
            self.weights -= self.lr * ___
            self.bias -= self.lr * ___

    def predict(self, X):
        linear_model = np.dot(X, self.___) + self.___
        y_predicted = self._sigmoid(linear_model)
        y_predicted_cls = [1 if i > 0.5 else 0 for i in y_predicted]
        return np.array(y_predicted_cls)

    def _sigmoid(self, x):
        return 1 / (1 + np.___(___))
# Set an example
X_train = np.array([[1, 2], [2, 3], [3, 4], [4, 5],[2, 2], [4, 6], [0.5, 2], [4, 5.5]])
y_train = np.array([0, 0, 0, 1, 0, 1, 0, 1])

# Use MyOwnLogisticRegression mode
model = MyOwnLogisticRegression()

model.fit(X_train, y_train)

# Create a test dataset
X_test = np.array([[1, 2], [2, 3]])

# Use predict
predictions = model.predict(X_test)
print(predictions)
[0 1]
%%ipytest -qq

class Test_MyOwnLogisticRegression:
    def test_fit(self):
        # Test if the fit method works properly
        model = MyOwnLogisticRegression()
        model.fit(X_train, y_train)
        assert model.weights is not None
        assert model.bias is not None

    def test_predict(self):
        # Test if the predict method works properly
        model = MyOwnLogisticRegression()
        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)
        assert len(y_pred) == len(X_test)

    def test_sigmoid(self):
        # Test if the sigmoid function works properly
        model = MyOwnLogisticRegression()
        assert model._sigmoid(0) == 0.5
        assert model._sigmoid(10) == pytest.approx(0.9999546021312976)

    def test_model_performance(self):
        # Test the performance of the model on the training data
        model = MyOwnLogisticRegression()
        model.fit(X_train, y_train)
        y_pred_train = model.predict(X_train)
        accuracy = np.mean(y_pred_train == y_train)
        assert accuracy >= 0.5  # Since no cross-validation is performed, accuracy should be at least 0.5

    def test_initialization(self):
        # Test if the initialization parameters are effective
        model = MyOwnLogisticRegression(learning_rate=0.01, n_iters=2000)
        assert model.lr == 0.01
        assert model.n_iters == 2000
.....                                                                                        [100%]