Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 6/Programming Assignment - 5/machine-learning-ex5/ex5/trainLinearReg.m
864 views
1
function [theta] = trainLinearReg(X, y, lambda)
2
%TRAINLINEARREG Trains linear regression given a dataset (X, y) and a
3
%regularization parameter lambda
4
% [theta] = TRAINLINEARREG (X, y, lambda) trains linear regression using
5
% the dataset (X, y) and regularization parameter lambda. Returns the
6
% trained parameters theta.
7
%
8
9
% Initialize Theta
10
initial_theta = zeros(size(X, 2), 1);
11
12
% Create "short hand" for the cost function to be minimized
13
costFunction = @(t) linearRegCostFunction(X, y, t, lambda);
14
15
% Now, costFunction is a function that takes in only one argument
16
options = optimset('MaxIter', 200, 'GradObj', 'on');
17
18
% Minimize using fmincg
19
theta = fmincg(costFunction, initial_theta, options);
20
21
end
22
23