Browse files

Initial, can do nothing special...

  • Loading branch information...
0 parents commit 8d629a1270981d202c54952619d407d304f82a17 @jjedele committed Dec 10, 2011
Showing with 79 additions and 0 deletions.
  1. +1 −0 README
  2. +29 −0 decision_tree.m
  3. +49 −0 infogain.m
1 README
@@ -0,0 +1 @@
+Simple Decision Tree Creator for Octave/Matlab. Can do nothing special yet. Just call with decision_tree(X,Y) where X is an integer-encoded feature matrix and Y an integer-encoded class-vector.
29 decision_tree.m
@@ -0,0 +1,29 @@
+% Decision Tree Creator
+% Jeffrey Jedele, 2011
+
+function decision_tree(X, Y, recursion_depth)
+
+ if nargin != 3
+ recursion_depth = 0;
+ printf("Root Node\n")
+ endif;
+
+ if length(Y)<1
+ printf("no nodes\n", recursion_depth);
+ elseif length(unique(Y))==1
+ printf("leaf node for class %d\n", Y(1));
+ else
+
+ [col, igain] = infogain(X,Y);
+ printf("split on feature %d (info gain: %f)\n", col, igain);
+
+ features = unique(X(:,col));
+ for f=features'
+ printf("Depth: %d - Feature Branch: %d\n", recursion_depth, f);
+ pos = find(X(:,col)==f);
+ decision_tree(X(pos,:), Y(pos,:), recursion_depth+1);
+ endfor;
+
+ endif;
+
+endfunction
49 infogain.m
@@ -0,0 +1,49 @@
+% Information Gain Calculator
+% Jeffrey Jedele, 2011
+
+function [max_gain_feature, gain] = infogain(x,y)
+
+ max_gain_feature = 0;
+ info_gains = zeros(1, size(x,2));
+
+ % calculate H(y)
+ classes = unique(y);
+ hy = 0;
+ for c=classes'
+ py = sum(y==c)/size(y,1);
+ hy += py*log2(py);
+ endfor;
+ hy = -hy;
+
+ % iterate over all features (columns)
+ for col=1:size(x,2)
+
+ features = unique(x(:,col));
+
+ % calculate entropy
+ hyx = 0;
+ for f=features'
+
+ pf = sum(x(:,col)==f)/size(x,1);
+ yf = y(find(x(:,col)==f));
+
+ % calculate h for classes given feature f
+ yclasses = unique(yf);
+ hyf = 0;
+ for yc=yclasses'
+ pyf = sum(yf==yc)/size(yf,1);
+ hyf += pyf*log2(pyf);
+ endfor;
+ hyf = -hyf;
+
+ hyx += pf * hyf;
+
+ endfor;
+
+ info_gains(col) = hy - hyx;
+
+ endfor;
+
+ [gain, max_gain_feature] = max(info_gains);
+
+endfunction

0 comments on commit 8d629a1

Please sign in to comment.