Advertisement
Guest User

TP SVM

a guest
Nov 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.54 KB | None | 0 0
  1. close all
  2. clear all
  3. c = [1; 0.2; 0.5]; % coéfficients du canal
  4. %c = [0.4048; 0.8164; 0.4084];
  5. M = length(c);
  6. sigv = 0.01; % variance bruit additif pour .001 1e-3 pour 0.01 1e-30
  7. N = 10000; % nombre de données
  8. d = sign(randn(1, N)); % séquence binaire générée
  9. dbuffer = zeros(M, 1); % Buffer
  10. y = zeros(1, N);
  11. for n = 1 : N,
  12. dbuffer = [d(n); dbuffer(1 : M - 1)];
  13. y(n) = c' * dbuffer + sqrt(sigv) * randn; % entrée récepteur
  14. end
  15.  
  16. P = 3;
  17. ybuffer = zeros(P, 1);
  18. y2 = zeros(P, N);
  19. for n = 1 : N,
  20. ybuffer = [y(n); ybuffer(1 : P - 1)];
  21. y2(:,n) = ybuffer;
  22. %y(n) = wopt' ? ybuffer;
  23. if (n <= (N/2)) && (mod(n, 5) == 0),
  24. if d(n) == 1,
  25. plot3(ybuffer(1), ybuffer(2), ybuffer(3), 'O');
  26. hold on;
  27. else
  28. plot3(ybuffer(1), ybuffer(2), ybuffer(3), 'X');
  29. hold on;
  30. end
  31. pause(0.0001);
  32. end
  33. end
  34. disp('ok pour affichage des motifs pour les deux classes')
  35.  
  36. trainset = transpose(y2(:,1:N/2));
  37. trainsetclass = transpose(d(1:N/2));
  38. testset = transpose(y2(:,N/2+1:N));
  39. testsetclass = transpose(d(N/2+1:N));
  40.  
  41. %% CV partition
  42. c = cvpartition(trainsetclass, 'k', 5);
  43.  
  44. %% Feature selection
  45. opts = statset('display', 'iter');
  46. fun = @(train_data, train_labels, test_data, test_labels) ...
  47. sum(predict(fitcsvm(train_data, train_labels, 'KernelFunction', 'rbf'), test_data) ~= test_labels);
  48.  
  49. [fs, history] = sequentialfs(fun, trainset, trainsetclass, 'cv', c, 'options', opts, 'nfeatures', 3);
  50.  
  51. %% Best hyperparameters
  52.  
  53. BF = trainset(:, fs);
  54.  
  55. mdl = fitcsvm(BF, trainsetclass, 'KernelFunction', 'rbf');
  56.  
  57. %% Ploting
  58.  
  59. %Gather support vectors from ClassificationSVM struct
  60. sv = mdl.SupportVectors;
  61. %set step size for finer sampling
  62. dd = 0.05;
  63. %generate grid for predictions at finer sample rate
  64. [x, y, z] = meshgrid(min(trainset(:,1)):dd:max(trainset(:,1)),...
  65. min(trainset(:,2)):dd:max(trainset(:,2)),...
  66. min(trainset(:,3)):dd:max(trainset(:,3)));
  67. xGrid = [x(:),y(:),z(:)];
  68. %get scores, f
  69. [ ~ , f] = predict(mdl,xGrid);
  70. %reshape to same grid size as the input
  71. f = reshape(f(:,2), size(x));
  72. % Assume class labels are 1 and 0 and convert to logical
  73. t = logical(trainsetclass);
  74. %plot data points, color by class label
  75. figure
  76. plot3(trainset(t, 1), trainset(t, 2), trainset(t, 3), 'b.');
  77. hold on
  78. plot3(trainset(~t, 1), trainset(~t, 2), trainset(~t, 3), 'r.');
  79. hold on
  80. % load unscaled support vectors for plotting
  81. plot3(sv(:, 1), sv(:, 2), sv(:, 3), 'go');
  82. %plot decision surface
  83. [faces,verts,~] = isosurface(x, y, z, f, 0, x);
  84. patch('Vertices', verts, 'Faces', faces, 'FaceColor','k','edgecolor', 'none', 'FaceAlpha', 0.2);
  85. grid on
  86. box on
  87. view(3)
  88. hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement