Skip to content

Instantly share code, notes, and snippets.

@limzunyuan
Created January 29, 2016 17:43
Show Gist options
  • Save limzunyuan/93ef0df573f78016319b to your computer and use it in GitHub Desktop.
Save limzunyuan/93ef0df573f78016319b to your computer and use it in GitHub Desktop.
% This function returns the attribute with the highest information gain
% in the attributes vector.
function [best_attribute] = choose_best_decision_attribute(examples, ...
attributes, binary_targets)
% Create a zero-ed row vector with same dimensions as attributes.
gain = zeros(size(attributes, 1), 1);
% Calculate information gain for each attribute and store in gain.
% Delegates calculation of information gain to another function.
for attr = 1 : length(attributes)
gain(attr) = information_gain(examples, attributes(attr), ...
binary_targets);
end
% best_idx returns the column number which has the highest info gain.
% ~ returns the value of the information gain, but we are not concerned
% about the value for this function; we just want the highest
% information gain.
[~, best_idx] = max(gain);
% Returns the attribute (number) that has the highest info gain.
best_attribute = attributes(best_idx);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment