Regularized logistic regression code in matlab

前端 未结 4 2024
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 06:43

I\'m trying my hand at regularized LR, simple with this formulas in matlab:

The cost function:

J(theta) = 1/m*sum((-y_i)*log(h(x_i)-(1-y_i)*log(1-h(x_i))         


        
4条回答
  •  离开以前
    2021-01-31 06:46

    Here is an answer that eliminates the loops

    m = length(y); % number of training examples
    
    predictions = sigmoid(X*theta);
    reg_term = (lambda/(2*m)) * sum(theta(2:end).^2);
    calcErrors = -y.*log(predictions) - (1 -y).*log(1-predictions);
    J = (1/m)*sum(calcErrors)+reg_term;
    
    % prepend a 0 column to our reg_term matrix so we can use simple matrix addition
    reg_term = [0 (lambda*theta(2:end)/m)'];
    grad = sum(X.*(predictions - y)) / m + reg_term;
    

提交回复
热议问题