Regularized logistic regression code in matlab

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

    Finally got it, after rewriting it again like for the 4th time, this is the correct code:

    function [J, grad] = costFunctionReg(theta, X, y, lambda)
    J = 0;
    grad = zeros(size(theta));
    
    temp_theta = [];
    
    for jj = 2:length(theta)
    
        temp_theta(jj) = theta(jj)^2;
    end
    
    theta_reg = lambda/(2*m)*sum(temp_theta);
    
    temp_sum =[];
    
    for ii =1:m
    
       temp_sum(ii) = -y(ii)*log(sigmoid(theta'*X(ii,:)'))-(1-y(ii))*log(1-sigmoid(theta'*X(ii,:)'));
    
    end
    
    tempo = sum(temp_sum);
    
    J = (1/m)*tempo+theta_reg;
    
    %regulatization
    %theta 0
    
    reg_theta0 = 0;
    
    for i=1:m
        reg_theta0(i) = ((sigmoid(theta'*X(i,:)'))-y(i))*X(i,1)
    end
    
    theta_temp(1) = (1/m)*sum(reg_theta0)
    
    grad(1) = theta_temp
    
    sum_thetas = []
    thetas_sum = []
    
    for j = 2:size(theta)
        for i = 1:m
    
            sum_thetas(i) = ((sigmoid(theta'*X(i,:)'))-y(i))*X(i,j)
        end
    
        thetas_sum(j) = (1/m)*sum(sum_thetas)+((lambda/m)*theta(j))
        sum_thetas = []
    end
    
    for z=2:size(theta)
        grad(z) = thetas_sum(z)
    end
    
    
    % =============================================================
    
    end
    

    If its helps anyone, or anyone has any comments on how can I do it better. :)

提交回复
热议问题