Path: blob/master/Week 7/Programming Assignment - 6/ex6/dataset3Params.m
863 views
function [C, sigma] = dataset3Params(X, y, Xval, yval)1%DATASET3PARAMS returns your choice of C and sigma for Part 3 of the exercise2%where you select the optimal (C, sigma) learning parameters to use for SVM3%with RBF kernel4% [C, sigma] = DATASET3PARAMS(X, y, Xval, yval) returns your choice of C and5% sigma. You should complete this function to return the optimal C and6% sigma based on a cross-validation set.7%89% You need to return the following variables correctly.10C = 1;11sigma = 0.3;1213% ====================== YOUR CODE HERE ======================14% Instructions: Fill in this function to return the optimal C and sigma15% learning parameters found using the cross validation set.16% You can use svmPredict to predict the labels on the cross17% validation set. For example,18% predictions = svmPredict(model, Xval);19% will return the predictions on the cross validation set.20%21% Note: You can compute the prediction error using22% mean(double(predictions ~= yval))23%2425param = [0.01 0.03 0.1 0.3 1 3 10 30]26error = realmax();27for i=1:size(param,2)28for j=1:size(param,2)29C_i = param(i);30sigma_j = param(j);3132model = svmTrain(X,y,C_i, @(x1, x2) gaussianKernel(x1, x2, sigma_j));33predictions = svmPredict(model,Xval);34temp = mean(double(predictions ~= yval));3536if temp < error37error = temp;38C = C_i;39sigma = sigma_j;40end41end42end4344% =========================================================================4546end4748