sepzhu
2018-04-02 02:40
采纳率: 0%
浏览 1.7k

matlab逻辑回归代价函数报错

代价函数:

function [jVal,gradient]=costFunc(theta)
%load data
fid=fopen('D:\graduate design\data\track2\training.txt');
temp=textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f%f',1000000);
temp=cell2mat(temp);
X=temp(:,[8 9 7]);
y=temp(:,1);
X=X';
m=length(y);
hypothesis=sigmoid(theta'*X);
jVal=-1/m(sum(y.*log(sigmoid(theta'*X))+(1-y).*log(1-sigmoid(theta'*X))));
gradient=zeros(size(theta));
for j=1:size(theta)
gradient(j)=1/m*sum(hypothesis-y)*X(:,j);
end

    梯度下降:
    function [optTheta,functionVal,exitFlag]=gradientDescent()
options=optimset('GradObj','on','MaxIter',100);
initialTheta=zeros(3,1);
[optTheta,functionVal,exitFlag]=fminunc(@costFunc,initialTheta,options);

end

报错信息:

下标索引必须为正整数类型或逻辑类型。

出错 costFunc (line 11)
jVal=-1/m(sum(y.*log(sigmoid(theta'*X))+(1-y).*log(1-sigmoid(theta'*X))));

出错 fminunc (line 301)
[f,GRAD] = feval(funfcn{3},x,varargin{:});

出错 gradientDescent (line 4)
[optTheta,functionVal,exitFlag]=fminunc(@costFunc,initialTheta,options);

原因:
Failure in initial objective function evaluation. FMINUNC cannot continue.

2条回答 默认 最新

相关推荐 更多相似问题