示例#1
0
        private StreamReader trainSr = null; // StreamReader for reading training data file.

        #endregion Fields

        #region Constructors

        public DecisionTree(string trainingFile)
        {
            trainSr = new StreamReader(trainingFile);
            // parse the first line to specify each name of attributes.
            SetAttributeNames();
            rootNode = new Node();
            // set attribute values by using the rest of lines of training data.
            SetAttributeValues();
            trainSr.Close();
        }
示例#2
0
        /* Returns true if splitting this node is possible.
           Returns false if it's not possible. */
        public void SplitNode()
        {
            classLabel = ClassLabel();	// majority voting

            // If tuples in this node are all of the same class
            if (CheckTuplesAreSameClass() == true)
            {
                return;
            }
            // If attribute_list is empty
            if (CheckRemainingAttrs() == false)
            {
                return;
            }

            int attrIdx = splitAttrIdx = SelectAttribute();
            int attrValCnt = Globals.attrs[attrIdx].values.Count;
            List<Tuple>[] tupleLists = new List<Tuple>[attrValCnt];
            for (int i=0; i<attrValCnt; ++i)
                tupleLists[i] = new List<Tuple>();

            foreach (Tuple t in tuples)
            {
                int attrVal = t.values[attrIdx];
                tupleLists[attrVal-1].Add(t);
            }
            for (int i=0; i<tupleLists.Length; ++i)
            {
                if (tupleLists[i].Count != 0)
                {
                    Node node = new Node(this.availableAttrs);
                    node.DisableAttr(attrIdx);
                    node.SetTupleList(tupleLists[i]);
                    node.splitAttrVal = (i+1);
                    childNodes.Add(node);
                }
            }
            tuples.Clear();

            foreach (Node childNode in childNodes)
                childNode.SplitNode();
        }