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/learningCurve.m
864 views
1
function [error_train, error_val] = ...
2
learningCurve(X, y, Xval, yval, lambda)
3
%LEARNINGCURVE Generates the train and cross validation set errors needed
4
%to plot a learning curve
5
% [error_train, error_val] = ...
6
% LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and
7
% cross validation set errors for a learning curve. In particular,
8
% it returns two vectors of the same length - error_train and
9
% error_val. Then, error_train(i) contains the training error for
10
% i examples (and similarly for error_val(i)).
11
%
12
% In this function, you will compute the train and test errors for
13
% dataset sizes from 1 up to m. In practice, when working with larger
14
% datasets, you might want to do this in larger intervals.
15
%
16
17
% Number of training examples
18
m = size(X, 1);
19
20
% You need to return these values correctly
21
error_train = zeros(m, 1);
22
error_val = zeros(m, 1);
23
24
% ====================== YOUR CODE HERE ======================
25
% Instructions: Fill in this function to return training errors in
26
% error_train and the cross validation errors in error_val.
27
% i.e., error_train(i) and
28
% error_val(i) should give you the errors
29
% obtained after training on i examples.
30
%
31
% Note: You should evaluate the training error on the first i training
32
% examples (i.e., X(1:i, :) and y(1:i)).
33
%
34
% For the cross-validation error, you should instead evaluate on
35
% the _entire_ cross validation set (Xval and yval).
36
%
37
% Note: If you are using your cost function (linearRegCostFunction)
38
% to compute the training and cross validation error, you should
39
% call the function with the lambda argument set to 0.
40
% Do note that you will still need to use lambda when running
41
% the training to obtain the theta parameters.
42
%
43
% Hint: You can loop over the examples with the following:
44
%
45
% for i = 1:m
46
% % Compute train/cross validation errors using training examples
47
% % X(1:i, :) and y(1:i), storing the result in
48
% % error_train(i) and error_val(i)
49
% ....
50
%
51
% end
52
%
53
%theta = trainLinearReg(X, y, 0);
54
% Computing error_train and error_val
55
for i = 1:m
56
theta = trainLinearReg(X(1:i,:), y(1:i), lambda);
57
error_train(i) = linearRegCostFunction(X(1:i,:),y(1:i),theta,0);
58
error_val(i) = linearRegCostFunction(Xval, yval, theta, 0);
59
end;
60
%error_val = linearRegCostFunction(Xval, yval, theta, 0);
61
% ---------------------- Sample Solution ----------------------
62
63
64
65
66
67
68
69
% -------------------------------------------------------------
70
71
% =========================================================================
72
73
end
74
75