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

# + _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" id="hfV503AtcBDp"
#Importing required libraries
import numpy as np 
import pandas as pd 
import io
import matplotlib.pyplot as plt

# + _cell_guid="79c7e3d0-c299-4dcb-8224-4455121ee9b0" _uuid="d629ff2d2480ee46fbb7e2d37f6b5fab8052498a" id="Y4rK9ffYcBEP" colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 258} outputId="263fbe28-d739-4713-afa6-013d39a94178"
# reading the csv file, and deleting 2 columns from the file, checking first few rows of the file
from google.colab import files
uploaded = files.upload()

data = pd.read_csv(io.BytesIO(uploaded['BuyComputer.csv']))

data.drop(columns=['User ID',],axis=1,inplace=True)
data.head()

# + colab={"base_uri": "https://localhost:8080/"} id="jEoi7Gm1AfmZ" outputId="8337fec4-0cb0-4090-ec58-91c2e55667b8"
from google.colab import drive
drive.mount('/content/drive')

# + _uuid="4cb45e28344e7e245ab398e9f4f5272ef21d2129" id="jwuPgU6_cBE8" colab={"base_uri": "https://localhost:8080/"} outputId="b8ce868f-254e-4683-a391-22eed47c179b"
#Declare label as last column in the source file

label = data.iloc[:,-1].values
print(label)

# + _uuid="2e7a145fa49435ad9578ec2827f76a70cc99f2e1" id="2lhBrOp8cBFX" colab={"base_uri": "https://localhost:8080/"} outputId="f0b00225-9be7-48ff-9b80-711c089c0d4c"
# Declaring X as all columns excluding last

X = data.iloc[:,:-1].values
print(X)

# + _uuid="dffb1f3e19e19964995ac827bf55108b5815ff67" id="t8nwbTn6cBFp"
# Splitting the data
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, label, test_size = 0.3, random_state = 137)

# + _uuid="7d4ed14782e114ae3282f20d3754121398e6d232" id="U4bUiVVFcBGD"
# Scaling the data
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# + id="3pkIY8gOwgAC"
# print(X_train)

# + _uuid="2ff7415e3e0e0673d59051cfe6154c63d3312a32" id="W5yGgzqbcBGc" colab={"base_uri": "https://localhost:8080/"} outputId="16eab247-966c-45d4-8d10-d1e148aa3bf7"
#Variables required to calculate sigmoid function
y_pred = []
print(X_train[0])
len_x = len(X_train[0])
w = []
b = 0.2
print(len_x)

# + _uuid="a228174207f4631be4f26a0cc05e379f3f58aa56" id="ZbqwTM0bcBGr" colab={"base_uri": "https://localhost:8080/"} outputId="1b3be8c0-09df-4bd5-c0ab-98f25bba3ab1"
entries = len(X_train[:,0])
entries

# + _uuid="5d4d6e47ee65c9c7404e60fcf8f05c11708546b3" id="vEV7Nn73cBG7" colab={"base_uri": "https://localhost:8080/"} outputId="190e86ff-a39f-4ea9-8993-b676632d876a"
for weights in range(len_x):
    w.append(0)
w


# + _uuid="18dbd2196d72527a82d30ab88ed2aa8d10bd01ce" id="_fAtpylNcBHM"
# Sigmoid function
def sigmoid(z): 
  return 1/(1 + np.exp(-z))


# + _uuid="daa0f87fdbf98591cb9f51b8dc7157dc399ca827" id="kfchkScTcBHd"
def predict(inputs):
  return sigmoid(np.dot(w, inputs) + b)


# + _uuid="4126f842d072ccd40019cc283b767a014e2ee074" id="K2ryTgglcBHt"
#Loss function
def loss_func(y,a):
  # Using the formula provided in the pdf
  # Here, a is htheta
  J = -(y * np.log(a) + (1-y) * np.log(1-a))
  return J


# + _uuid="fc0ceb65c69f4ee0c3f28e050744229dc90c621b" id="1KW3eDpmcBIA"
dw = []
db = 0
J = 0
alpha = 0.1
for x in range(len_x):
    dw.append(0)

# + _uuid="e4be38e9b500ae0c5a7134296a3055675c4fb2d8" id="ipqdFLP3cBIO"
#Repeating the process 3000 times
training_epochs = 3000
for epoch in range(training_epochs):
  for i in range(entries):
    sampl = X_train[i]
    a = predict(sampl)
    dz = a - Y_train[i]
    J += loss_func(Y_train[i], a)
    for j in range(len_x):
      dw[j] = dw[j] + (sampl[j] * dz)
    db += dz
  J = J/entries
  db = db/entries
  for x in range(len_x):
    dw[x] = dw[x] / entries
  for x in range(len_x):
    w[x] = w[x] - (alpha * dw[x])
  b = b - (alpha * db)
  J = 0

# + _uuid="5479ccb6073ed1ea310ef7de01b2935fc3ec400e" id="7Q585AdrcBIs" colab={"base_uri": "https://localhost:8080/"} outputId="6b820147-fca4-42f2-bf9c-137b12c299bf"
#Printing weight
print(w)

# + _uuid="a939c247b8a092f74c9843975612daa85c423621" id="rEiF-bNHcBJB" colab={"base_uri": "https://localhost:8080/"} outputId="1a5eb763-9370-4a61-e143-61bb940d3835"
#Printing bias
print(b)

# + _uuid="b7ae24169a21c7ac8ea0787f4a38a0de3e07a6b5" id="MPt5nUcpcBJR"
#Predicting the label
for i in range(len(Y_test)):
  y_pred.append(predict(X_test[i]))

# + _uuid="967ad1b72305ad792a5d50e4d8b8a07632f7b241" id="79HPPz7jcBJg"
#Printing actual and predicted values in a table
for i in range(len(y_pred)):
  if(y_pred[i] > 0.5):
    y_pred[i] = 1
  else:
    y_pred[i] = 0

# + _uuid="a59807150900082ab876ef0200c6c7f8f93e098c" id="sdZDj_iVcBJt" colab={"base_uri": "https://localhost:8080/"} outputId="d9e15d03-3bf1-4b51-ccb6-96fc4e4f5565"
# Calculating accuracy of prediction
correct = 0
for i in range(len(y_pred)):
  if(y_pred[i] == Y_test[i]):
    correct += 1

print('Correct prediction is ', (correct / len(y_pred)) * 100)

# + [markdown] id="x6nmajpzhAEn"
# #Using sklearn LogisticRegression model

# + _uuid="9aaade066015e04f20dd7eb1d37339be75ca3836" _kg_hide-output=true id="iG-BK4i9cBKH" colab={"base_uri": "https://localhost:8080/"} outputId="7ad15939-8a60-4d6a-9fa4-20b31aacb033"
# Fitting Logistic Regression to the Training set
from sklearn.linear_model import LogisticRegression
LR = LogisticRegression(random_state = 137)

#Fit
LR.fit(X_train, Y_train)

#predicting the test label with LR. Predict always takes X as input
y_predLR = LR.predict(X_test)

correctness = 0
for i in range(len(y_pred)):
  if(y_pred[i] == Y_test[i]):
    correctness += 1
print('Prediction : ', (correctness / len(y_pred)) * 100)

# + [markdown] id="Y8sYVBu-iSW-"
# **Exercise:**
#
# Try logistic regression on BuyComputer dataset and set Random state=Your_RollNumber (last 3 digit of ID, incase if you don't have ID)
#
# Done above

StarcoderdataJupyterScriptsDedupFiltered