Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

how do I call from function with parameters

$
0
0

How in Octave do I call functions with parameters

I need to return different values error_train and error_val every time I iterate through X and y. Now it does not go through the iteration correctly.J should take the place of error_train and error_val and return it back to the for loop in learningCurve function for loop

function [error_train, error_val] = ...    learningCurve(X, y, Xval, yval, lambda)m = size(X, 1);% You need to return these values correctlyerror_train = zeros(m, 1);error_val   = zeros(m, 1);for i = 1:mtheta = trainLinearReg(X(i,:), y(i), lambda)error_train(i) = linearRegCostFunction(X(i,:), y(i), theta, 0);error_val(i) = linearRegCostFunction(Xval, yval, theta, 0);endend----------------------------------------------------------------function [theta] = trainLinearReg(X, y, lambda)%   [theta] = TRAINLINEARREG (X, y, lambda) initial_theta = zeros(size(X, 2), 1); costFunction = @(t) linearRegCostFunction(X, y, t, lambda);options = optimset('MaxIter', 200, 'GradObj', 'on');% Minimize theta = fmincg(costFunction, initial_theta, options);----------------------------------------------------------------function [J, grad] = linearRegCostFunction(X, y, theta, lambda)h = X*theta;errorvector = h - y;error_sqr= (errorvector).^2;q = sum(error_sqr);Junreg = (1/(2*m))*q;theta_change = ((X')*errorvector)*(1/m);theta(1) = 0;costreg = (sum(theta.^2))*(lambda/(2*m));reggrad = theta*(lambda/m);J = Junreg + costreg;grad = theta_change + reggrad;

Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>