diff --git a/Lamina_Zafrullah_Segun b/Lamina_Zafrullah_Segun new file mode 100644 index 0000000..cfec91e --- /dev/null +++ b/Lamina_Zafrullah_Segun @@ -0,0 +1,68 @@ +import numpy as np +import matplotlib.pyplot as plt +%matplotlib inline +import seaborn as sns +from sklearn import datasets + +BC = datasets.load_breast_cancer() +X, y = BC.data, BC.target + +class LogisticRegression: + def __init__(self, lr=0.0001, num_iter=300000, fit_intercept=True, verbose=False): + self.lr = lr + self.num_iter = num_iter + self.fit_intercept = fit_intercept + self.verbose = verbose + + def __add_intercept(self, X): + intercept = np.ones((X.shape[0], 1)) + return np.concatenate((intercept, X), axis=1) + + def __sigmoid(self, z): + return 1 / (1 + np.exp(-z)) + def __loss(self, h, y): + return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() + + def fit(self, X, y): + if self.fit_intercept: + X = self.__add_intercept(X) + + # weights initialization + self.theta = np.zeros(X.shape[1]) + + for i in range(self.num_iter): + z = np.dot(X, self.theta) + h = self.__sigmoid(z) + gradient = np.dot(X.T, (y - h)) + self.theta += self.lr * gradient + + z = np.dot(X, self.theta) + h = self.__sigmoid(z) + loss = self.__loss(h, y) + + if(self.verbose ==True and i % 10000 == 0): + print(f'loss: {loss} \t') + + def predict_prob(self, X): + if self.fit_intercept: + X = self.__add_intercept(X) + + return self.__sigmoid(np.dot(X, self.theta)) + + def predict(self, X): + return self.predict_prob(X).round() + + BC = datasets.load_breast_cancer() + X, y = BC.data, BC.target + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3, random_state=1234) + +def accuracy(y_true, y_pred): + accuracy = np.sum(y_true == y_pred) / len(y_true) + return accuracy + +regressor = LogisticRegression(lr=0.00001, num_iter=3000000) +regressor.fit(X_train, y_train) +predictions = regressor.predict(X_test) + +print ('Logistic Regression Accuracy:', accuracy(y_test, predictions))