import numpy as np import pandas as pd import matplotlib.pyplot as plt from logisticRegression.logisticRegression import LogisticRegression from sklearn.datasets import load_breast_cancer from sklearn.preprocessing import MinMaxScaler from metrics import * import math scalar = MinMaxScaler() data = load_breast_cancer() X = pd.DataFrame(data['data']) y = pd.Series(data['target']) scalar.fit(X) X = scalar.transform(X) X = pd.DataFrame(X) # This scales data to the range 0-1 and is easier to train LR = LogisticRegression() LR.fit(X, y, n_iter=500, lr=5e-3) y_hat = LR.predict(X) print('Accuracy: ', accuracy(y_hat, y)) LR = LogisticRegression() LR.fit_autograd(X, y, n_iter=400, lr=8e-3) y_hat = LR.predict(X) print('Accuracy: ', accuracy(y_hat, y))
for i in range(3): xti = X.iloc[i * 190:min(570, (i + 1) * 190)] yti = y[i * 190:min(570, (i + 1) * 190)] xi1 = X.iloc[0:i * 190] xi2 = X.iloc[min(570, (i + 1) * 190):570] yi1 = y[0:i * 190] yi2 = y[min(570, (i + 1) * 190):570] xi = pd.concat([xi1, xi2]) yi = pd.concat([yi1, yi2]) xi.reset_index(drop=True, inplace=True) yi.reset_index(drop=True, inplace=True) xti.reset_index(drop=True, inplace=True) yti.reset_index(drop=True, inplace=True) LR = LogisticRegression() LR.fit(xi, yi, n_iter=600, lr=7e-03) y_hat = LR.predict(xti) acc_curr = accuracy(y_hat, yti) if (acc_curr > best_accuracy): best_accuracy = acc_curr best_LR = copy.deepcopy(LR) print(f'Accuracy Fold {i+1}: ', acc_curr) acc_ov += acc_curr print("Overall Accuracy:", acc_ov / 3) print("Decision boundary for features 0 and 1:") LR = LogisticRegression() LR.fit(X[[0, 1]], y, n_iter=500, lr=5e-3) LR.plot(np.array(X[[0, 1]]), np.array(y))
from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import KFold scaler = MinMaxScaler() np.random.seed(42) from sklearn.datasets import load_breast_cancer X = load_breast_cancer().data y = load_breast_cancer().target scalar = MinMaxScaler() scalar.fit(X) X = scalar.transform(X) print('\n---------Unregularised Normal Fit----------') LR = LogisticRegression(learningRate=0.1, maxIterations=1000) LR.fit(X, y) y_hat = LR.predict(X) print(accuracy(y_hat, y)) print('\n---------Unregularised Autograd Fit----------') LR = LogisticRegression(learningRate=0.1, maxIterations=1000) LR.fit_autograd(X, y) y_hat = LR.predict(X) print(accuracy(y_hat, y)) print('\n---------3 KFold----------') Kfolding = KFold(3, True, 1) avgAccuracy = 0 for trainData, testData in Kfolding.split(X): trainSetData, testSetData = X[trainData], X[testData] ytrainData, ytestData = y[trainData], y[testData]