Click here to Skip to main content
15,896,730 members
Articles / Programming Languages / C#
Article

ID3 Decision Tree Algorithm in C#

Rate me:
Please Sign up or sign in to vote.
3.05/5 (44 votes)
21 Oct 20031 min read 308.5K   20.2K   58   56
Sample of ID3 Decision Tree Algorithm in C#

Image 1

Introduction

The algorithm ID3 (Quinlan) uses the method top-down induction of decision trees. Given a set of classified examples a decision tree is induced, biased by the information gain measure, which heuristically leads to small trees. The examples are given in attribute-value representation. The set of possible classes is finite. Only tests, that split the set of instances of the underlying example languages depending on the value of a single attribute are supported.

Details

Depending on whether the attributes are nominal or numerical, the tests either

  • have a successor for each possible attribute value, or
  • split according to a comparison of an attribute value to a constant, or depending on if an attribute value belongs to a certain interval or not.

The algorithm starts with the complete set of examples, a set of possible tests and the root node as the actual node. As long as the examples propagated to a node do not all belong to the same class and there are tests left,

  • a test with highest information gain is chosen,
  • the corresponding set of successors is created for the actual node,
  • each example is propagated to the successor given by the chosen test,
  • ID3 is called recursively for all successors.

How it works

The core of sample is builded with 3 classes (Attribute, TreeNode and DecisionTreeID3).

  • TreeNode - are the nodes of the decision tree;
  • Attribute - is the class with have a name e any possible values;
  • DecisionTreeID3 - is the class what get a data samples and generate a decision tree.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer iddi
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: How you defined new attributes in main? Pin
pol_chu2-Oct-10 19:16
pol_chu2-Oct-10 19:16 
GeneralAn error report [modified] Pin
pol_chu9-Sep-10 22:44
pol_chu9-Sep-10 22:44 
Generalerror Pin
lqht25-Apr-10 8:05
lqht25-Apr-10 8:05 
GeneralError Pin
strepto125-Apr-10 6:21
strepto125-Apr-10 6:21 
Generalhi Pin
rodalyn15-Mar-10 18:15
rodalyn15-Mar-10 18:15 
Generalhelp:id3 decision tree Pin
std_112-Mar-10 1:34
std_112-Mar-10 1:34 
GeneralRe: help:id3 decision tree Pin
Sama Ramba15-Oct-10 22:36
Sama Ramba15-Oct-10 22:36 
GeneralRe: help:id3 decision tree Pin
leavetrace24-Oct-10 23:08
leavetrace24-Oct-10 23:08 
Alslam Alikum ;
I am sorry for late in answer your email to me
this is the java code
package guiID3;<br />
<br />
<br />
import java.awt.Cursor;<br />
import java.awt.FlowLayout;<br />
import java.awt.GridBagConstraints;<br />
import java.awt.GridBagLayout;<br />
import java.awt.GridLayout;<br />
import java.awt.Insets;<br />
import java.awt.Point;<br />
import javax.swing.JEditorPane;<br />
import javax.swing.border.SoftBevelBorder;<br />
import javax.swing.border.BevelBorder;<br />
import javax.swing.JTextPane;<br />
import javax.swing.BoxLayout;<br />
import java.awt.event.ActionEvent;<br />
import java.awt.event.ActionListener;<br />
import java.awt.event.MouseAdapter;<br />
import java.awt.event.MouseEvent;<br />
import java.io.IOException;<br />
<br />
import javax.swing.BorderFactory;<br />
import javax.swing.ImageIcon;<br />
import javax.swing.JButton;<br />
import javax.swing.JDialog;<br />
import javax.swing.JFrame;<br />
import javax.swing.JLabel;<br />
import javax.swing.JPanel;<br />
import javax.swing.WindowConstants;<br />
<br />
<br />
public class AboutGUIId3 extends javax.swing.JDialog {<br />
    <br />
    /**<br />
     *<br />
     */<br />
    private static final long serialVersionUID = -1893243061264575311L;<br />
    <br />
    {<br />
        //Set Look & Feel<br />
        try {<br />
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");<br />
        } catch(Exception e) {<br />
            e.printStackTrace();<br />
        }<br />
    }<br />
    <br />
    private JLabel jLabel1;<br />
    private JLabel jLabel2;<br />
    private JPanel jPanel1;<br />
    private JButton jButton2;<br />
    private JLabel jLabel4;<br />
    private JTextPane jTextPane3;<br />
    private JTextPane jTextPane2;<br />
    private JTextPane jTextPane1;<br />
    private JLabel jLabel3;<br />
    private JPanel jPanel2;<br />
    private JLabel jLabel5;<br />
    private JButton jButton1;<br />
    private JFrame parent;<br />
    <br />
    /**<br />
     * Auto-generated main method to display this JDialog<br />
     */<br />
        /*public static void main(String[] args) {<br />
                JFrame frame = new JFrame();<br />
                AboutAntMinerJDialog inst = new AboutAntMinerJDialog(frame);<br />
                inst.setVisible(true);<br />
        }*/<br />
    <br />
    public AboutGUIId3(JDialog frame, JFrame parent) {<br />
        super(frame, "About GUI ID3", true);<br />
        this.parent = parent;<br />
        initGUI();<br />
    }<br />
    <br />
    private void initGUI() {<br />
        try {<br />
            GridBagLayout thisLayout = new GridBagLayout();<br />
            thisLayout.columnWeights = new double[] {0.1};<br />
            thisLayout.columnWidths = new int[] {7};<br />
            thisLayout.rowWeights = new double[] {0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1};<br />
            thisLayout.rowHeights = new int[] {25,10,20,8,180,11,52,2,7};<br />
            this.getContentPane().setLayout(thisLayout);<br />
            this.setModal(true);<br />
            this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);<br />
            <br />
            Point p = parent.getLocationOnScreen();<br />
            this.setLocation(new Double(p.getX() + 30).intValue(), new Double(p.getY() + 50).intValue());<br />
            <br />
            this.getContentPane().setForeground(new java.awt.Color(255,255,255));<br />
            this.getContentPane().setBackground(new java.awt.Color(255,255,255));<br />
            {<br />
                jLabel2 = new JLabel();<br />
                this.getContentPane().add(<br />
                        jLabel2,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        1,<br />
                        1,<br />
                        1,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.NORTHWEST,<br />
                        GridBagConstraints.NONE,<br />
                        new Insets(0, 11, 0, 0),<br />
                        0,<br />
                        0));<br />
                jLabel2.setText("Version 1");<br />
            }<br />
            {<br />
                jLabel1 = new JLabel();<br />
                this.getContentPane().add(<br />
                        jLabel1,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        0,<br />
                        1,<br />
                        1,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.SOUTHWEST,<br />
                        GridBagConstraints.NONE,<br />
                        new Insets(0, 11, 0, 0),<br />
                        0,<br />
                        0));<br />
                GridLayout jLabel1Layout = new GridLayout(3, 1);<br />
                jLabel1Layout.setRows(3);<br />
                jLabel1Layout.setVgap(33);<br />
                jLabel1.setLayout(jLabel1Layout);<br />
                jLabel1.setText("GUI ID3");<br />
                jLabel1.setFont(new java.awt.Font("Dialog",1,14));<br />
            }<br />
            {<br />
                jPanel1 = new JPanel();<br />
                FlowLayout jPanel1Layout = new FlowLayout();<br />
                jPanel1Layout.setAlignment(FlowLayout.RIGHT);<br />
                jPanel1Layout.setHgap(15);<br />
                jPanel1Layout.setVgap(9);<br />
                jPanel1.setLayout(jPanel1Layout);<br />
                this.getContentPane().add(<br />
                        jPanel1,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        8,<br />
                        1,<br />
                        2,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.CENTER,<br />
                        GridBagConstraints.BOTH,<br />
                        new Insets(7, 0, 0, 0),<br />
                        0,<br />
                        0));<br />
                {<br />
                    jButton1 = new JButton();<br />
                    jPanel1.add(jButton1);<br />
                    jButton1.setText("Ok");<br />
                    jButton1.setPreferredSize(new java.awt.Dimension(65, 23));<br />
                    jButton1.setLocation(new java.awt.Point(0, 7));<br />
                    jButton1.setBounds(246, 6, 56, 24);<br />
                    jButton1.setMaximumSize(new java.awt.Dimension(65, 23));<br />
                    jButton1.setMinimumSize(new java.awt.Dimension(65, 23));<br />
                    jButton1.addActionListener(new ActionListener() {<br />
                        public void actionPerformed(ActionEvent evt) {<br />
                            setVisible(false);<br />
                        }<br />
                    });<br />
                }<br />
            }<br />
            {<br />
                jLabel5 = new JLabel();<br />
                this.getContentPane().add(<br />
                        jLabel5,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        6,<br />
                        1,<br />
                        1,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.NORTH,<br />
                        GridBagConstraints.NONE,<br />
                        new Insets(0, 0, 0, 0),<br />
                        0,<br />
                        0));<br />
                jLabel5.setIcon(new ImageIcon(getClass().getClassLoader().getResource("qoraan1.JPG")));<br />
                jLabel5.setToolTipText("State University of Santa Catarina, campus Joinville.");<br />
            }<br />
            {<br />
                jButton2 = new JButton();<br />
                this.getContentPane().add(<br />
                        jButton2,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        6,<br />
                        1,<br />
                        1,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.PAGE_END,<br />
                        GridBagConstraints.NONE,<br />
                        new Insets(0, 0, 0, 0),<br />
                        0,<br />
                        0));<br />
                jButton2.setText("<html><a href=\"http://www.joinville.udesc.br\">http://www.joinville.udesc.br</a></html>");<br />
                jButton2.setBackground(new java.awt.Color(255,255,255));<br />
                jButton2.setContentAreaFilled(false);<br />
                jButton2.setBorderPainted(false);<br />
                jButton2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));<br />
                jButton2.setFont(new java.awt.Font("Arial",0,11));<br />
                jButton2.setMaximumSize(new java.awt.Dimension(50, 9));<br />
                jButton2.setMinimumSize(new java.awt.Dimension(180, 14));<br />
                jButton2.setToolTipText("Website of the State University of Santa Catarina, campus Joinville. Available in Portuguese only.");<br />
                jButton2.addMouseListener(new MouseAdapter() {<br />
                    public void mouseEntered(MouseEvent evt) {<br />
                        setCursor(Cursor<br />
                                .getPredefinedCursor(Cursor.HAND_CURSOR));<br />
                    }<br />
                    public void mouseExited(MouseEvent evt) {<br />
                        setCursor(Cursor<br />
                                .getPredefinedCursor(Cursor.DEFAULT_CURSOR));<br />
                    }<br />
                });<br />
                jButton2.addActionListener(new ActionListener() {<br />
                    public void actionPerformed(ActionEvent evt) {<br />
                        String[] cmd = new String[4];<br />
                        cmd[0] = "cmd.exe";<br />
                        cmd[1] = "/C";<br />
                        cmd[2] = "start";<br />
                        cmd[3] = "http://www.joinville.udesc.br";<br />
                        try {<br />
                            Runtime.getRuntime().exec(cmd);<br />
                        } catch (IOException e) {<br />
                            e.printStackTrace();<br />
                        }<br />
                    }<br />
                });<br />
            }<br />
            {<br />
                jPanel2 = new JPanel();<br />
                GridBagLayout jPanel2Layout = new GridBagLayout();<br />
                jPanel2Layout.columnWeights = new double[] {0.1};<br />
                jPanel2Layout.columnWidths = new int[] {7};<br />
                jPanel2Layout.rowWeights = new double[] {0.1,0.1,0.1,0.1};<br />
                jPanel2Layout.rowHeights = new int[] {7,7,7,7};<br />
                this.getContentPane().add(<br />
                        jPanel2,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        4,<br />
                        1,<br />
                        1,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.CENTER,<br />
                        GridBagConstraints.BOTH,<br />
                        new Insets(0, 11, 0, 11),<br />
                        0,<br />
                        0));<br />
                <br />
                jPanel2.setMinimumSize(new java.awt.Dimension(530, 10));<br />
                jPanel2.setPreferredSize(new java.awt.Dimension(530, 10));<br />
                jPanel2.setSize(540, 54);<br />
                jPanel2.setBackground(new java.awt.Color(240,240,240));<br />
                jPanel2.setLayout(jPanel2Layout);<br />
                {<br />
                    jLabel4 = new JLabel();<br />
                    jPanel2.add(jLabel4, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));<br />
                    jLabel4.setText("Copyright Notice");<br />
                    jLabel4.setFont(new java.awt.Font("Tahoma",1,11));<br />
                }<br />
                {<br />
                    jTextPane1 = new JTextPane();<br />
                    jPanel2.add(jTextPane1, new GridBagConstraints(<br />
                            0,<br />
                            1,<br />
                            1,<br />
                            1,<br />
                            0.0,<br />
                            0.0,<br />
                            GridBagConstraints.CENTER,<br />
                            GridBagConstraints.BOTH,<br />
                            new Insets(0, 11, 0, 11),<br />
                            0,<br />
                            0));<br />
                    jTextPane1.setText("This software is freely available for research and teaching. It is not meant for commercial purposes. If you publish any material involving the use of this software, please quote the original paper that describes the algorithm in detail, as suggested below:");<br />
                    jTextPane1.setOpaque(false);<br />
                    jTextPane1.setEditable(false);<br />
                    jTextPane1.setFocusable(false);<br />
                    jTextPane1.setLocation(new java.awt.Point(555, 0));<br />
                }<br />
                {<br />
                    jTextPane2 = new JTextPane();<br />
                    jPanel2.add(jTextPane2, new GridBagConstraints(<br />
                            0,<br />
                            2,<br />
                            1,<br />
                            1,<br />
                            0.0,<br />
                            0.0,<br />
                            GridBagConstraints.CENTER,<br />
                            GridBagConstraints.BOTH,<br />
                            new Insets(0, 11, 0, 11),<br />
                            0,<br />
                            0));<br />
                    jTextPane2.setText("Parpinelli, R.S., Lopes, H.S., Freitas, A.A. \"Data mining with an ant colony optimization algorithm\". IEEE Transactions on Evolutionary Computation, special issue on Ant Colony Algorithms, v. 6, n. 4, p. 321-332, August, 2002.");<br />
                    jTextPane2.setOpaque(false);<br />
                    jTextPane2.setEditable(false);<br />
                    jTextPane2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));<br />
                }<br />
                {<br />
                    jTextPane3 = new JTextPane();<br />
                    jPanel2.add(jTextPane3, new GridBagConstraints(<br />
                            0,<br />
                            3,<br />
                            1,<br />
                            1,<br />
                            0.0,<br />
                            0.0,<br />
                            GridBagConstraints.CENTER,<br />
                            GridBagConstraints.BOTH,<br />
                            new Insets(0, 11, 0, 11),<br />
                            0,<br />
                            0));<br />
                    jTextPane3.setText("We do not take any responsibility for the results that you obtain with this software, we do not offer any guarantees, nor do we help with the maintenance of this program.");<br />
                    jTextPane3.setOpaque(false);<br />
                    jTextPane3.setEditable(false);<br />
                    jTextPane3.setFocusable(false);<br />
                }<br />
            }<br />
            {<br />
                jLabel3 = new JLabel();<br />
                this.getContentPane().add(<br />
                        jLabel3,<br />
                        new GridBagConstraints(<br />
                        0,<br />
                        2,<br />
                        1,<br />
                        1,<br />
                        0.0,<br />
                        0.0,<br />
                        GridBagConstraints.NORTHWEST,<br />
                        GridBagConstraints.NONE,<br />
                        new Insets(0, 11, 0, 0),<br />
                        0,<br />
                        0));<br />
                jLabel3.setText("Developed by Fernando Meyer under the orientation of M.Sc. Rafael Stubs Parpinelli.");<br />
            }<br />
            this.setSize(580, 386);<br />
        } catch (Exception e) {<br />
            e.printStackTrace();<br />
        }<br />
    }<br />
    <br />
    /**<br />
     * Auto-generated method for setting the popup menu for a component<br />
     */<br />
    private void setComponentPopupMenu(final java.awt.Component parent, final javax.swing.JPopupMenu menu) {<br />
        parent.addMouseListener(new java.awt.event.MouseAdapter() {<br />
            public void mousePressed(java.awt.event.MouseEvent e) {<br />
                if(e.isPopupTrigger())<br />
                    menu.show(parent, e.getX(), e.getY());<br />
            }<br />
            public void mouseReleased(java.awt.event.MouseEvent e) {<br />
                if(e.isPopupTrigger())<br />
                    menu.show(parent, e.getX(), e.getY());<br />
            }<br />
        });<br />
    }<br />
    <br />
}<br />
<br />

----------------class Attribute-----------
<br />
package guiID3;<br />
<br />
<br />
public class Attribute {<br />
    private String attributeName;<br />
    private String [] typesArray;<br />
    private int [] intTypesArray;<br />
    int x;<br />
    public Attribute(String [] typesArray){<br />
        this.typesArray = typesArray;<br />
        initializeIntTypesArray();<br />
    }<br />
    public Attribute(String name)<br />
		{<br />
			attributeName = name;<br />
		}<br />
<br />
<br />
 public void setAttributeAsInt(int x){<br />
        this.x=x;<br />
    } <br />
 public int getAttributeAsInt(){<br />
      return x;<br />
    }<br />
 <br />
    private void initializeIntTypesArray(){<br />
        intTypesArray = new int[typesArray.length];<br />
        for(int x=0; x < typesArray.length; x++){<br />
            intTypesArray[x] = x;<br />
        }<br />
    }<br />
    <br />
    public void setAttributeName(String name){<br />
        attributeName = name;<br />
    }<br />
    public String getAttributeName(){<br />
        return attributeName;<br />
    }<br />
    public String[] getTypes(){<br />
        return typesArray;<br />
    }<br />
    public int[] getIntTypesArray(){<br />
        return intTypesArray;<br />
    }<br />
    public int indexOf(int type){<br />
        for(int x=0; x < intTypesArray.length; x++)<br />
            if(intTypesArray[x] == type)<br />
                return x;<br />
        return -1;<br />
    }<br />
    <br />
    public int indexOf(String value){<br />
        for(int x=0; x < typesArray.length; x++){<br />
            if(value.compareTo(typesArray[x]) == 0){<br />
                return x;<br />
            }<br />
        }<br />
        return -1;<br />
    }<br />
}<br />
<br />
<br />
<br />

----------------
GeneralRe: help:id3 decision tree Pin
leavetrace24-Oct-10 23:13
leavetrace24-Oct-10 23:13 
GeneralRe: help:id3 decision tree Pin
leavetrace24-Oct-10 23:16
leavetrace24-Oct-10 23:16 
GeneralRe: help:id3 decision tree Pin
leavetrace24-Oct-10 23:18
leavetrace24-Oct-10 23:18 
GeneralMy vote of 1 Pin
Code Deamon20-Apr-09 4:27
Code Deamon20-Apr-09 4:27 
Questioncan not work with more than two class Pin
leavetrace1-Jan-09 18:50
leavetrace1-Jan-09 18:50 
GeneralI think there's a mistake, correct me if i'm wrong Pin
rock_me1-Jul-08 6:56
rock_me1-Jul-08 6:56 
QuestionDo u interested in association rule? Pin
alice_nhan27-Nov-07 4:39
alice_nhan27-Nov-07 4:39 
Answerfind duplicate item in childsample [modified] Pin
alice_nhan27-Nov-07 4:34
alice_nhan27-Nov-07 4:34 
GeneralRe: find duplicate item in childsample Pin
leavetrace12-Jan-09 11:36
leavetrace12-Jan-09 11:36 
GeneralTranslated Version (English) of this Algorithm Pin
kirankonathala20-Apr-07 10:16
kirankonathala20-Apr-07 10:16 
GeneralRe: Translated Version (English) of this Algorithm Pin
kroket30-Dec-07 0:40
kroket30-Dec-07 0:40 
GeneralRe: Translated Version (English) of this Algorithm Pin
jingweidu25-Mar-08 0:58
jingweidu25-Mar-08 0:58 
GeneralRe: Translated Version (English) of this Algorithm Pin
Vaasugi16-Aug-08 21:16
Vaasugi16-Aug-08 21:16 
GeneralRe: Translated Version (English) of this Algorithm Pin
wesemy19-Aug-08 3:51
wesemy19-Aug-08 3:51 
GeneralRe: Translated Version (English) of this Algorithm Pin
mikaal10018-Mar-09 6:49
mikaal10018-Mar-09 6:49 
GeneralRe: Translated Version (English) of this Algorithm Pin
u06050943-Apr-09 16:47
u06050943-Apr-09 16:47 
GeneralRe: Translated Version (English) of this Algorithm Pin
Code Deamon20-Apr-09 4:16
Code Deamon20-Apr-09 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.