Compute log loss for logistic regression from scratch

0 votes

compute log-loss

def logloss(y_true,y_pred):
    '''In this function, we will compute log loss '''
    log_loss = (-((y_true * np.log10(y_pred)) + (1-y_true) * np.log10(1-y_pred)).mean())
    return log_loss

Computing logistic regression

def train(X_train,y_train,X_test,y_test,epochs,alpha,eta0):
 w,b = initialize_weights(X_train[0])
    train_loss = []
    test_loss = []
    for e in range(epochs):
        for x,y in zip(X_train,y_train):
            dw = gradient_dw(x,w,y,b,alpha,N)
            db = gradient_db(x,y,w,b)
            w = w + (eta0 * dw)
            b = b + (eta0 * db)
        train_pred = []
        for i in X_train:
            y_pred = sigmoid(np.dot(w.T, i) + b)
            train_pred.append(y_pred)
        train_loss.append(logloss(y_train, train_pred))
        
        test_pred = []
        for j in X_test:
            y_pred_test = sigmoid(np.dot(w.T, j) + b)
            test_pred.append(y_pred_test)
        test_loss.append(logloss(y_test, test_pred))
    return w,b
alpha=0.0001
eta0=0.0001
epochs=50
N = len(X_train)
w,b = train(X_train,y_train,X_test,y_test,epochs,alpha,eta0)

Error that I am getting

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-112-9a34879eb072> in <module>
      3 epochs=50
      4 N = len(X_train)
----> 5 w,b = train(X_train,y_train,X_test,y_test,epochs,alpha,eta0)

<ipython-input-110-db0e3d88382d> in train(X_train, y_train, X_test, y_test, epochs, alpha, eta0)
     30             y_pred = sigmoid(np.dot(w.T, i) + b)
     31             train_pred.append(y_pred)
---> 32         train_loss.append(logloss(y_train, train_pred))
     33 
     34         test_pred = []

<ipython-input-108-f272288a384c> in logloss(y_true, y_pred)
      1 def logloss(y_true,y_pred):
      2     '''In this function, we will compute log loss '''
----> 3     log_loss = (-((y_true * np.log10(y_pred)) + (1-y_true) * np.log10(1-y_pred)).mean())
      4     return log_loss

TypeError: unsupported operand type(s) for -: 'int' and 'list'

I have mentioned the full code just the codes for which I am getting error.I am confused whether to make changes to logloss or make changes to logistic regression code i.e def train(). How to rectify this error?

Apr 7, 2022 in Machine Learning by Nandini
• 5,480 points
1,104 views

1 answer to this question.

0 votes

You have made  train_pred as python list. When logloss function is used, you calculate(1- train_pred), which means integer minus python list.
This is the reason you get an error.

TypeError: unsupported operand type(s) for -: 'int' and 'list'

Hope this helps.

Elevate Your Expertise with Our Machine Learning Certification Program!

answered Apr 11, 2022 by Dev
• 6,000 points

Related Questions In Machine Learning

0 votes
1 answer

Can we use Normal Equation for Logistic Regression ?

Well not likely,  only one discriminative method ...READ MORE

answered Feb 24, 2022 in Machine Learning by Nandini
• 5,480 points
741 views
0 votes
1 answer
0 votes
1 answer

Different types of Logistic Regression

There are three main types of logistic ...READ MORE

answered May 13, 2019 in Machine Learning by Nikhil
11,483 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
0 votes
1 answer

Can we use Normal Equation for Logistic Regression ?

Unfortunately, only one discriminative method in classification ...READ MORE

answered Mar 30, 2022 in Machine Learning by Dev
• 6,000 points
729 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP