I just wrote about the establishment of the ID3 decision tree, which is predicted by the decision tree. The main use here is the XML traversal parsing, relatively simple.
For XML parsing, reference is made to:
http://blog.csdn.net/soszou/article/details/8049220
http://lavasoft.blog.51cto.com/62575/71669/
Ideas:
The data to be predicted first, such as "Sunny mild normal TRUE" becomes a map based on the feature table, making it easy to follow up, and the result is
Outlook Sunny
Temperature Windy
Humidity Normal
Windy true such a map
Then the variable XML file, starting from the child node of the root, if the node does not exist child nodes, it is the leaf node, then the direct output of text, is the category of its classification. If there are child nodes, the corresponding node is found according to value in map, and the node is used as the node parameter of the next iteration.
1 ImportJava.io.File;2 Importjava.io.IOException;3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 Importorg.dom4j.Document;9 Importorg.dom4j.DocumentException;Ten Importorg.dom4j.Element; One ImportOrg.dom4j.io.SAXReader; A - Public classPredict { - the Public StaticMap<string,string> Getmap (String data,arraylist<string>featurelist) { -Map<string,string> map=NewHashMap (); -String[] S=data.split (""); - for(inti=0;i<s.length;i++){ + Map.put (Featurelist.get (i), s[i]); - } + returnmap; A } at - Public Static intPredict (map<string,string>map,element e) { -List<element> childlist=e.elements (); - if(Childlist.size () ==0){ - System.out.println (E.gettext ()); - return1; in } -String Value=map.get (childlist.get (0). GetName ()); to for(Element next:childlist) { +String t=next.attributevalue ("value");//The property name here is value, so you can get the value of the property - if(T.compareto (value) ==0){ the predict (map,next); * } $ }Panax Notoginseng return1; - } the + A /** the * @paramargs + * @throwsdocumentexception - * @throwsIOException $ */ $ Public Static voidMain (string[] args)throwsdocumentexception, IOException { - //TODO auto-generated Method Stub -String xml= "C:/users/administrator/desktop/upload/dt1.xml"; theString file= "C:/users/administrator/desktop/upload/dt.txt"; -String data= "Sunny mild normal TRUE";WuyiArraylist<string> featurelist=utils.loadfeature (file); theMap<string,string> map=Getmap (data,featurelist); - WuSaxreader saxreader=NewSaxreader (); -Document Document =saxreader.read (NewFile (XML)); AboutElement root=document.getrootelement (); $ - predict (map,root); -System.out.println ("Finished"); - } A +}
Java implementation of ID3 decision tree prediction