Advertisement
makispaiktis

ML - Bagging Outputs

Oct 10th, 2022 (edited)
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.89 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Data
  5. N = 10^3;
  6. x = 1 : N;
  7. e = exp(1);
  8.  
  9. % Bagging Rounds + Original Data
  10. MAX_ROUND = 5;
  11. rounds_list = 0 : MAX_ROUND;
  12. un_sim_list = zeros(1, MAX_ROUND+1);
  13. un_exp_list = zeros(1, MAX_ROUND+1);
  14.  
  15. for i = 1 : length(rounds_list)
  16.     round = rounds_list(i);
  17.     if round == 0
  18.         un_sim_list(round+1) = N;
  19.         un_exp_list(round+1) = N;
  20.     else
  21.         x = BAGGING(x);
  22.         un_sim = length(unique(x));
  23.         un_exp = N * (1 - 1/e)^round;
  24.         un_sim_list(round+1) = un_sim;
  25.         un_exp_list(round+1) = un_exp;
  26.     end
  27. end
  28. un_sim_list
  29. un_exp_list
  30.  
  31. % Plots
  32. figure();
  33. scatter(rounds_list, un_sim_list);
  34. hold on
  35. scatter(rounds_list, un_exp_list);
  36. legend("Simulation Results", "Theoretical Results");
  37.  
  38.  
  39. % Auxiliary Functions
  40. function y = BAGGING(x)
  41.     LEN = length(x);
  42.     for i = 1 : LEN
  43.         y(i) = x(randi([1 LEN]));
  44.     end
  45. end
  46.  
  47.  
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement