Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 7/Programming Assignment - 6/ex6/dataset3Params.m
863 views
1
function [C, sigma] = dataset3Params(X, y, Xval, yval)
2
%DATASET3PARAMS returns your choice of C and sigma for Part 3 of the exercise
3
%where you select the optimal (C, sigma) learning parameters to use for SVM
4
%with RBF kernel
5
% [C, sigma] = DATASET3PARAMS(X, y, Xval, yval) returns your choice of C and
6
% sigma. You should complete this function to return the optimal C and
7
% sigma based on a cross-validation set.
8
%
9
10
% You need to return the following variables correctly.
11
C = 1;
12
sigma = 0.3;
13
14
% ====================== YOUR CODE HERE ======================
15
% Instructions: Fill in this function to return the optimal C and sigma
16
% learning parameters found using the cross validation set.
17
% You can use svmPredict to predict the labels on the cross
18
% validation set. For example,
19
% predictions = svmPredict(model, Xval);
20
% will return the predictions on the cross validation set.
21
%
22
% Note: You can compute the prediction error using
23
% mean(double(predictions ~= yval))
24
%
25
26
param = [0.01 0.03 0.1 0.3 1 3 10 30]
27
error = realmax();
28
for i=1:size(param,2)
29
for j=1:size(param,2)
30
C_i = param(i);
31
sigma_j = param(j);
32
33
model = svmTrain(X,y,C_i, @(x1, x2) gaussianKernel(x1, x2, sigma_j));
34
predictions = svmPredict(model,Xval);
35
temp = mean(double(predictions ~= yval));
36
37
if temp < error
38
error = temp;
39
C = C_i;
40
sigma = sigma_j;
41
end
42
end
43
end
44
45
% =========================================================================
46
47
end
48