# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="19LYAr64HuOw"
#@ IMPORTING THE LIBRARIES:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression


# + id="XqRfocAmIHrV"
#@ IMPLEMENTATION OF LOGISTIC REGRESSION:
class LogisticRegression():                                     # Implementation of Logistic Regression. 
  def __init__(self, learning_rate, iterations):                # Constructor Function. 
    self.learning_rate = learning_rate                          # Initializing the Learning rate. 
    self.iterations = iterations                                # Initializing the Iterations. 

  def fit(self, features, labels):                              # Function for Training. 
    self.features = features                                    # Initializing Features. 
    self.labels = labels                                        # Initializing Labels.
    self.num_train = features.shape[0]                          # Number of Training examples. 
    self.num_features = features.shape[1]                       # Number of Features. 
    self.W, self.b = np.zeros(self.num_features), 0             # Initializing Weights. 

    for i in range(self.iterations):
      self.update_weights()
    return self
  
  def update_weights(self):                                    # Function for updating weights. 
    A = 1 / (1 + np.exp(-(self.features.dot(self.W) + self.b)))
    pred = (A - self.labels.T)
    pred = np.reshape(pred, self.num_train)
    dW = np.dot(self.features.T, pred) / self.num_train
    db = np.sum(pred) / self.num_train                         # Average of the Predictions. 
    self.W = self.W - self.learning_rate * dW                  # Updating Weights. 
    self.b = self.b - self.learning_rate * db                  # Updating Weights. 
    return self
  
  def predict(self, X):                                        # Function for Predictions. 
    Z = 1 / (1 + np.exp(-(X.dot(self.W) + self.b)))
    Y = np.where(Z > 0.5, 1, 0)
    return Y


# + colab={"base_uri": "https://localhost:8080/"} id="aEZlOSt4thQU" outputId="40a0db7f-067c-45c3-f753-252216bdcb79"
#@ IMPLEMENTATION: 
def main():
  data = pd.read_csv(PATH)                                                  # Reading the Dataset. 
  X = data.iloc[:, :-1].values
  Y = data.iloc[:, -1:].values
  X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, 
                                                      random_state=11)
  model = LogisticRegression(learning_rate=0.01, iterations=1000)           # Initializing Logistic Regression. 
  model.fit(X_train, y_train)                                               # Training the Model. 
  Y_pred = model.predict(X_test)
  correctly_classified = 0
  count = 0
  for count in range(np.size(Y_pred)):
    if y_test[count] == Y_pred[count]:
      correctly_classified += 1
    count += 1
  print("Accuracy:", (correctly_classified / count) * 100)

if __name__ == "__main__":
  main()

StarcoderdataJupyterScriptsDedupFiltered