<jupyter_start><jupyter_text>Logistic RegressionThe code in this notebook will be an implementation of the logistic regression algorithm from scratch. I will be working on an example and the datasets available is a part of Andrew Ng's course on coursera in the Week 3 section. The txt file will also be available in the directory<jupyter_code>import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy.special import expit
from scipy import optimize
import math

df=pd.read_csv("ex2data1.txt",sep=',', engine='python')
x1 = df["exam1"].tolist()
x2 = df["exam2"].tolist()
x0 = np.array([1 for i in range(len(x1))])
y = df["status"].tolist()
x = np.matrix([x0,x1,x2])<jupyter_output><empty_output><jupyter_text>Under the hoodh$\theta$(x) = g($\theta$x)<jupyter_code>def gradDesc(x,y,alpha,iterations):
    yM = np.matrix(y)
    theta = np.matrix([0,1,7])
    m = len(y)
    
    for i in range(iterations):
        h = 1/(1+expit(-(theta*x)))
        theta = theta - (alpha/m)*((h - yM)*np.transpose(x))
    p1,p2,p3 = theta.item(0,0),theta.item(0,1),theta.item(0,2)
    return (p1,p2,p3,cost)
p0,p1,p2,cost = gradDesc(x,y,0.075,72000)

%matplotlib inline
acceptedX1 = [x1[i] for i in range(len(x1)) if y[i] == 1]
acceptedX2 = [x2[i] for i in range(len(x2)) if y[i] == 1]
rejectedX1 = [x1[i] for i in range(len(x1)) if y[i] == 0]
rejectedX2 = [x2[i] for i in range(len(x2)) if y[i] == 0]
plt.scatter(acceptedX1,acceptedX2, marker='*', color='g')
plt.scatter(rejectedX1,rejectedX2, marker='o', color='purple')
plt.xlim(30,100)
plt.ylim(30,100)
a = np.linspace(0,100,100)
b = -(p1*a + p0)/p2
plt.plot(a,b,'r')
plt.show()
print(p0,p1,p2)
def predict(x,y,p0,p1,p2):
    tempY = -(p1*x + p0)/p2
    if y-tempY>=0:
        return f"With scores {x} and {y} You will be accepted"
    else:
        return f"With scores {x} and {y} you will be rejected"
    
print(predict(45,86,p0,p1,p2))
print(predict(50,87,p0,p1,p2))
print(predict(85,46,p0,p1,p2))
print(predict(67,52,p0,p1,p2))<jupyter_output>With scores 45 and 86 You will be accepted
With scores 50 and 87 You will be accepted
With scores 85 and 46 You will be accepted
With scores 67 and 52 you will be rejected
{'fraction_non_alphanumeric': 0.15955983493810177, 'fraction_numerical': 0.062356717102246675, 'mean_word_length': 4.727034120734908, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '40604', 'license': 'MIT', 'n_tokens_mistral': 958, 'n_tokens_neox': 896, 'n_words': 236, 'path': 'Supervised/logistic-regression.ipynb', 'repo_name': 'akagam1/Machine-Learning', 'size': '2181'}