Regularized logistic regression code in matlab

前端 未结 4 2031
伪装坚强ぢ
伪装坚强ぢ 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:48

    Vectorized:

    function [J, grad] = costFunctionReg(theta, X, y, lambda)
    
    hx = sigmoid(X * theta);
    m = length(X);
    
    J = (sum(-y' * log(hx) - (1 - y')*log(1 - hx)) / m) + lambda * sum(theta(2:end).^2) / (2*m);
    grad =((hx - y)' * X / m)' + lambda .* theta .* [0; ones(length(theta)-1, 1)] ./ m ;
    
    end
    

提交回复
热议问题