Search code examples
numpymathlogistic-regression

Can you please help me correct this valueerror: math domain error?


I'm trying to calculate the loss function in Logistic Regression but end up getting a math error in it. can you please help me rectify this error?

def loss(y,a):
    L = (-y*math.log(a)-(1-y)*math.log(1-a)).mean()
    return L

Solution

  • You are getting the error because you are trying to find the log of a negative number (i.e. a is becoming negative). From your equation, I infer y is the true value and a is the predicted value. And the predictions come for the eqution below:

                                                                enter link description here

    So there is no way a can be negative, so kindly check your prediction function definition.