Java Implementation of decision tree ID3 algorithm (basically using all ID3 algorithms), id3java
Known: indicates the flu training dataset, which predefines two types. The ID3 algorithm is used to establish the flu attribute description decision tree for the flu training dataset.
|
No. |
Headache |
Muscle pain |
Body temperature |
Flu |
|
1 |
Yes (1) |
Yes (1) |
Normal (0) |
No (0) |
|
2 |
Yes (1) |
Yes (1) |
High (1) |
Yes (1) |
|
3 |
Yes (1) |
Yes (1) |
High (2) |
Yes (1) |
|
4 |
No (0) |
Yes (1) |
Normal (0) |
No (0) |
|
5 |
No (0) |
No (0) |
High (1) |
No (0) |
|
6 |
No (0) |
Yes (1) |
High (2) |
Yes (1) |
|
7 |
Yes (1) |
No (0) |
High (1) |
Yes (1) |
Principle Analysis:
Before dividing each non-leaf node in a decision tree, calculate the information gain produced by each attribute and select the attribute of the maximum information gain for Division. Because the greater the information gain, the stronger the ability to differentiate samples, the more representative.
Information entropy calculation:
Information Gain:
Calculation Result (the word on the draft is ugly ):
Certificate --------------------------------------------------------------------------------------------------------------------------------------------
**************************************** **************************************** *****************************
*************** ******************************
package ID3Tree;import java.util.Comparator;;@SuppressWarnings("rawtypes")public class Comparisons implements Comparator { public int compare(Object a, Object b) throws ClassCastException{ String str1 = (String)a; String str2 = (String)b; return str1.compareTo(str2); }}
Package ID3Tree; public class Entropy {// information Entropy public static double getEntropy (int x, int total) {if (x = 0) {return 0 ;} double x_pi = getShang (x, total); return-(x_pi * Logs (x_pi);} public static double Logs (double y) {return Math. log (y)/Math. log (2);} public static double getShang (int x, int total) {return x * Double. parseDouble ("1.0")/total ;}}
Package ID3Tree; public class TreeNode {// parent node TreeNode parent; // point to the attributes of the parent node String parentAttribute; String nodeName; String [] attributes; TreeNode [] childNodes ;}
Package ID3Tree; import java. util. *; public class UtilID3 {TreeNode root; private boolean [] flag; // training set private Object [] trainArrays; // node index private int nodeIndex; public static void main (String [] args) {// initialize the Object [] arrays = new Object [] {new String [] {"yes", "yes ", "Normal", "no"}, new String [] {"yes", "yes", "high", "yes"}, new String [] {"yes ", "Yes", "very high", "yes"}, new String [] {"no", "yes", "normal", "no "}, new String [] {"no", "no", "high", "No "}, New String [] {" no "," yes "," very high "," yes "}, new String [] {" yes "," no ", "high", "yes" }}; UtilID3 ID3Tree = new UtilID3 (); ID3Tree. create (arrays, 3);} // create public void create (Object [] arrays, int index) {this. trainArrays = arrays; initial (arrays, index); createDTree (arrays); printDTree (root);} // initialize public void initial (Object [] dataArray, int index) {this. nodeIndex = index; // data initialization this. flag = new boolean [(String []) DataArray [0]). length]; for (int I = 0; I <this. flag. length; I ++) {if (I = index) {this. flag [I] = true;} else {this. flag [I] = false ;}}// create a decision tree public void createDTree (Object [] arrays) {Object [] ob = getMaxGain (arrays); if (this. root = null) {this. root = new TreeNode (); root. parent = null; root. parentAttribute = null; root. attributes = getAttributes (Integer) ob [1]). intValue (); root. no DeName = getNodeName (Integer) ob [1]). intValue (); root. childNodes = new TreeNode [root. attributes. length]; insert (arrays, root) ;}// insert decision tree public void insert (Object [] arrays, TreeNode parentNode) {String [] attributes = parentNode. attributes; for (int I = 0; I <attributes. length; I ++) {Object [] Arrays = pickUpAndCreateArray (arrays, attributes [I], getNodeIndex (parentNode. nodeName); Object [] inf O = getMaxGain (Arrays); double gain = (Double) info [0]). doubleValue (); if (gain! = 0) {int index = (Integer) info [1]). intValue (); TreeNode currentNode = new TreeNode (); currentNode. parent = parentNode; currentNode. parentAttribute = attributes [I]; currentNode. attributes = getAttributes (index); currentNode. nodeName = getNodeName (index); currentNode. childNodes = new TreeNode [currentNode. attributes. length]; parentNode. childNodes [I] = currentNode; insert (Arrays, currentNode);} els E {TreeNode leafNode = new TreeNode (); leafNode. parent = parentNode; leafNode. parentAttribute = attributes [I]; leafNode. attributes = new String [0]; leafNode. nodeName = getLeafNodeName (Arrays); leafNode. childNodes = new TreeNode [0]; parentNode. childNodes [I] = leafNode ;}}// output public void printDTree (TreeNode node) {System. out. println (node. nodeName); TreeNode [] childs = node. childNodes; for (in T I = 0; I <childs. length; I ++) {if (childs [I]! = Null) {System. out. println ("If:" + childs [I]. parentAttribute); printDTree (childs [I]) ;}}// cut the array public Object [] pickUpAndCreateArray (Object [] arrays, String attribute, int index) {List <String []> list = new ArrayList <String []> (); for (int I = 0; I <arrays. length; I ++) {String [] strs = (String []) arrays [I]; if (strs [index]. equals (attribute) {list. add (strs) ;}} return list. toArray ();} // get the node name Public String getNodeName (int index) {String [] strs = new String [] {"headache", "muscle pain", "temperature", "flu "}; for (int I = 0; I <strs. length; I ++) {if (I = index) {return strs [I] ;}} return null ;} // retrieve the leaf node name public String getLeafNodeName (Object [] arrays) {if (arrays! = Null & arrays. length> 0) {String [] strs = (String []) arrays [0]; return strs [nodeIndex];} return null ;} // obtain the public int getNodeIndex (String name) {String [] strs = new String [] {"headache", "muscle pain", "body temperature ", "Influenza"}; for (int I = 0; I <strs. length; I ++) {if (name. equals (strs [I]) {return I ;}} return-1 ;}// obtain the maximum information gain: public Object [] getMaxGain (Object [] arrays) {Object [] result = new Object [2]; double Gain = 0; int index =-1; for (int I = 0; I <this. flag. length; I ++) {if (! This. flag [I]) {double value = gain (arrays, I); if (gain <value) {gain = value; index = I ;}}} result [0] = gain; result [1] = index; if (index! =-1) {this. flag [index] = true;} return result;} // obtain the attribute array public String [] getAttributes (int index) {@ SuppressWarnings ("unchecked ") treeSet <String> set = new TreeSet <String> (new Comparisons (); for (int I = 0; I <this. trainArrays. length; I ++) {String [] strs = (String []) this. trainArrays [I]; set. add (strs [index]);} String [] result = new String [set. size ()]; return set. toArray (result);} // calculation information gain public double gain (Object [] arrays, int index) {String [] playbils = getAttributes (this. nodeIndex); int [] counts = new int [playbils. length]; for (int I = 0; I <counts. length; I ++) {counts [I] = 0 ;}for (int I = 0; I <arrays. length; I ++) {String [] strs = (String []) arrays [I]; for (int j = 0; j <playbils. length; j ++) {if (strs [this. nodeIndex]. equals (playbils [j]) {counts [j] ++ ;}} double entropyS = 0; for (int I = 0; I <counts. length; I ++) {entropyS = entropyS + Entropy. getEntropy (counts [I], arrays. length);} String [] attributes = getAttributes (index); double total = 0; for (int I = 0; I <attributes. length; I ++) {total = total + entropy (arrays, index, attributes [I], arrays. length);} return entropyS-total;} public double entropy (Object [] arrays, int index, String attribute, int totals) {String [] playbils = getAttributes (this. nodeIndex); int [] counts = new int [playbils. length]; for (int I = 0; I <counts. length; I ++) {counts [I] = 0 ;}for (int I = 0; I <arrays. length; I ++) {String [] strs = (String []) arrays [I]; if (strs [index]. equals (attribute) {for (int k = 0; k <playbils. length; k ++) {if (strs [this. nodeIndex]. equals (playbils [k]) {counts [k] ++ ;}}} int total = 0; double entropy = 0; for (int I = 0; I <counts. length; I ++) {total = total + counts [I];} for (int I = 0; I <counts. length; I ++) {entropy = entropy + Entropy. getEntropy (counts [I], total);} return Entropy. getShang (total, totals) * entropy ;}}