Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. function [attribute,threshold] = ChooseAttribute(features,targets)
  2.  
  3. entrp_before=my_entropy(targets);%entropy before doing anything
  4.  
  5.  
  6. all_threasholds = []; % Holds the threashold values generated for each attribute.
  7. disp("EntrpArr "+entrp_before);
  8. leng=size(features,2);
  9.  
  10. num_of_candidate_threasholds=20;
  11. info_gain(1:leng,1:num_of_candidate_threasholds)=-1;
  12.  
  13.  
  14. %Go through every feature/attribute
  15. for i=1:leng
  16. threasholds = linspace(min(features(:,i))+30,max(features(:,i))-30,num_of_candidate_threasholds);
  17. threasholds= normrnd(mean(features(:,i)),std(features(:,i))*3,[1,num_of_candidate_threasholds]);
  18.  
  19. all_threasholds=[all_threasholds;threasholds];
  20.  
  21. left(1:length(features)) = -1;
  22. right(1:length(features)) = -1;
  23. %Split the array based on the different threashold values selected
  24. %and put them all in the left and right arrays.
  25. for x=1:length(threasholds)
  26. % split array into two based on less than or more than threashold.
  27.  
  28.  
  29. this_thsh=threasholds(x);
  30.  
  31. this_features= features(:,i);% Get the features for this attribute for this attribute
  32.  
  33. for z=1:size(features,1)
  34. if(this_features(z)>this_thsh)
  35. right(z) = targets(z);
  36. else
  37. left(z) = targets(z);
  38. end
  39. end
  40.  
  41. %remove initialization values
  42. right(right==-1)=[];
  43. left(left==-1)=[];
  44.  
  45. %Calculate the information gain for this attribute and add it to array.
  46. lftPrpr = length(left)/(length(left)+length(right));
  47. rhtPrpr = length(right)/(length(left)+length(right));
  48. info_gain(i,x) = entrp_before-(lftPrpr*my_entropy(left)+rhtPrpr*my_entropy(right));
  49. %disp("Entropy for this split: Left: "+my_entropy(left)+" Right: "+my_entropy(right));
  50. end
  51. disp("Complted attribute: "+i);
  52. end
  53.  
  54. %Work out best info gain in matrixx
  55. [max_gain_col,max_idx_col]= max(info_gain);%Max of each col
  56. [max_all,max_all_idx] = max(max_gain_col);%max of everything
  57.  
  58. out=[max_idx_col(max_all_idx),all_threasholds(max_idx_col(max_all_idx),max_all_idx)];
  59. attribute = max_idx_col(max_all_idx);
  60. threshold = all_threasholds(max_idx_col(max_all_idx),max_all_idx);
  61. disp("Max gain found: "+max_all+" on attribute: "+out(1)+" with threashold: "+out(2));
  62. return;
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement