Logistic regression code not working,

Hey, so I was building the logistic regression model (vectorized implementation) and this is the code I have written, I don’t know why it isn’t working, could you please help me with this and point out the mistake in the code,it would be of great help.The dataset I have used is here dataset.

Here is the link for the same question I asked on StackExchange (it has a couple of other codes as well).
Thanks in Advance!

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#sigmoid function
def sigmoid(z):
    
    a=1/(1+np.exp(-z))
    
    return a

#predicted y value for a given w,b ans x
def y_out(x_test,w_ini,b_ini):
    m,n=x_test.shape
    a=sigmoid(np.dot(w_ini,np.transpose(x_test))+b_ini)
    a=a.reshape(m,1)
    return a

#loss of a single training example
def loss(x_train,y_train,w_ini,b_ini):
    l=0
    l=-(y*np.log(y_out(x_train,w_ini,b_ini))+(1-y)*np.log(1-y_out(x_train,w_ini,b_ini)))
    
    return l

#the cost function
def cost(x,y,w,b):
    J=0
    m,n=x.shape
    J=(1/m)*np.sum(loss(x,y,w,b))
    
    return J

#derivative terms
def gradient(x_train,y_train,w_ini,b_ini):
    
    
    m,n=x_train.shape
    dw=np.zeros(n)
    dw=dw.reshape(len(dw),1)
    db=0.
    
    dw=(1/m)*(np.dot(np.transpose(x_train),y_out(x_train,w_ini,b_ini)-y_train))
    db=(1/m)*(np.sum(y_out(x_train,w_ini,b_ini)-y_train))
    
    dw=dw.reshape(1,len(dw))
    return dw,db


#gradient descent
def optimize(x_train,y_train,w_ini,b_ini,alpha,itirations):
    
    m,n=x.shape
    w=w_ini
    b=b_ini
    for i in range(itirations):
        
        dw,db=gradient(x_train,y_train,w,b)
        w=w-alpha*(dw)
        b=b-alpha*(db)
        
    return w,b
    



dataset=pd.read_csv("ex2data1.csv")
x=dataset.iloc[:,:-1].values
y=dataset.iloc[:,-1].values
y=y.reshape(len(y),1)
m,n=x.shape

#initialization
w=np.zeros(n)
b=0.

#running the algoritm
w_out,b_out=optimize(x,y,w,b,1e-5,10000)

#parameters after running gradient descent
print(w_out,b_out)

Hi @Prathvik_G_S, when you open a question on stack exchange, I suggest you to go through the following thinking process:

  1. what do you mean by not working? You never explained.

  2. can you focus on the part of the code that is not working? For example, you have checked your sigmoid function, and you should have found that it’s working, at least if you have compared it with your assignment in MLS. Then ask your question about the smallest piece of code that’s not up to your expectation. It’s your responsibility to unit-test each of your function.

  3. if you unit-tested the code and they all look fine, then I suspect that your “not working” is not a coding issue, but maybe you tried to compare your implementation with sklearn, and found that they don’t behave in the same way, then I think this is the specific question you need to ask instead, and shows what you observed, whether you made sure the results had converged, what are the difference and the logic behind linking those observation to your conclusion that it is not working, and the code is less important here and you may choose to put it there for people’s reference, or on others’ request.

Finally, sklearn logistic regression offers a few different solvers so don’t be surprised that none of those implements optimization that works the same way as yours.

I suggest you to open a new question on stack exchange.

Thanks for the suggesion!!