You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset3Params.m 1.6 kB

8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. % You need to return the following variables correctly.
  10. C = 1;
  11. sigma = 0.3;
  12. % ====================== YOUR CODE HERE ======================
  13. % Instructions: Fill in this function to return the optimal C and sigma
  14. % learning parameters found using the cross validation set.
  15. % You can use svmPredict to predict the labels on the cross
  16. % validation set. For example,
  17. % predictions = svmPredict(model, Xval);
  18. % will return the predictions on the cross validation set.
  19. %
  20. % Note: You can compute the prediction error using
  21. % mean(double(predictions ~= yval))
  22. %
  23. min = 999;
  24. CA = [0.01; 0.03; 0.1; 0.3; 1; 3; 10; 30];
  25. sigmaA = [0.01; 0.03; 0.1; 0.3; 1; 3; 10; 30];
  26. for i = 1 : length(CA)
  27. for j = 1 : length(sigmaA)
  28. model = svmTrain(X, y, CA(i), @(x1, x2) gaussianKernel(x1, x2, sigmaA(j)));
  29. predictions = svmPredict(model, Xval);
  30. tempMin = mean(double(predictions ~= yval));
  31. if tempMin < min
  32. min = tempMin;
  33. C = CA(i);
  34. sigma = sigmaA(j);
  35. end
  36. end
  37. end
  38. % =========================================================================
  39. end

机器学习

Contributors (1)