Matlab使用mnrfit建立了逻辑回归,但是我需要用L2正则化来实现逻辑回归.我完全不知道如何继续.我发现了一些很好的论文和网站参考文献,但是不确定如何实现优化所需的梯度下降算法.
在Matlab中是否有一个容易获得的示例代码.我找到了一些库和软件包,但它们都是更大的软件包的一部分,并且调用了许多复杂的函数,只要经过跟踪就会丢失.
最佳答案 这是用于逻辑回归的简单梯度下降的带注释的代码片段.要引入正则化,您需要更新成本和梯度方程.在此代码中,theta是参数,X是类预测变量,y是类标签,alpha是学习率
我希望这有帮助 :)
function [theta,J_store] = logistic_gradientDescent(theta, X, y,alpha,numIterations)
% Initialize some useful values
m = length(y); % number of training examples
n = size(X,2); %number of features
J_store = 0;
%J_store = zeros(numIterations,1);
for iter=1:numIterations
%predicts the class labels using the current weights (theta)
Z = X*theta;
h = sigmoid(Z);
%This is the normal cost function equation
J = (1/m).*sum(-y.*log(h) - (1-y).*log(1-h));
%J_store(iter) = J;
%This is the equation to obtain the given the current weights, without regularisation
grad = [(1/m) .* sum(repmat((h - y),1,n).*X)]';
theta = theta - alpha.*grad;
end
end