コード例 #1
0
        ///// <summary>
        ///// if it's leaf, add childNodes
        ///// </summary>
        ///// <param name="root"></param>
        ///// <param name="data">current table;  not necessaryly the original table</param>
        ///// <param name="valToCheck"></param>
        ///// <returns></returns>
        //private static bool CheckIfIsLeaf_sealIfLeaf(DataTable data, TreeNode root, string valToCheck)
        //{
        //	var isLeaf = true;
        //	var allEndValues = new List<string>();

        //	// get all leaf values for the attribute in question
        //	for (var i = 0; i < data.Rows.Count; i++)
        //	{
        //		if (data.Rows[i][root.TableIndex].ToString().Equals(valToCheck))
        //		{
        //			allEndValues.Add(data.Rows[i][data.Columns.Count - 1].ToString());
        //		}
        //	}

        //	// check whether all elements of the list have the same value
        //	if (allEndValues.Count > 0 && allEndValues.Any(x => x != allEndValues[0]))
        //	{
        //		isLeaf = false;
        //	}

        //	// create leaf with value to display and edge to the leaf
        //	if (isLeaf)
        //	{
        //		root.ChildNodes.Add(new TreeNode(true, allEndValues[0], valToCheck));
        //	}

        //	return isLeaf;
        //}
        private static TreeNode __GetRootNodeFroClass_tblAssumeDwelt(DataTable data, string inEdge)
        {
            var lastIndex = data.Columns.Count - 1;

            return(new TreeNode(
                       data.Columns[data.Columns.Count - 1].ColumnName
                       , lastIndex
                       ,
                       new MyAttribute(
                           data.Columns[lastIndex].ToString()
                           ,
                           MyAttribute.GetDifferentAttributeNamesOfColumn(data, lastIndex)
                           ),
                       inEdge
                       ));
        }
コード例 #2
0
        private static TreeNode GetRootNode_tblAssumeDefiniteAttrsPurged(DataTable data, string inEdge)
        {
            ///if there is only one col: the class .
            ///
            if (data.Columns.Count == 1)
            {
                return(__GetRootNodeFroClass_tblAssumeDwelt(data, inEdge));
            }

            var attributes = new List <MyAttribute>();
            var highestInformationGainIndex = -1;
            var highestInformationGain      = double.MinValue;

            // Get all names, amount of attributes and attributes for every column
            for (var i = 0; i < data.Columns.Count - 1; i++)
            {
                var differentAttributenames = MyAttribute.GetDifferentAttributeNamesOfColumn(data, i);
                attributes.Add(new MyAttribute(data.Columns[i].ToString(), differentAttributenames));
            }

            // Calculate Entropy (S)
            var tableEntropy = _treed.trainSet._EntropyOfClassX.Entropy(data);

            for (var i = 0; i < attributes.Count; i++)
            {
                attributes[i].InformationGain = _treed._TrainSetX.GetGainRatioForCol(data, i, tableEntropy);

                if (attributes[i].InformationGain > highestInformationGain)
                {
                    highestInformationGain      = attributes[i].InformationGain;
                    highestInformationGainIndex = i;
                }
            }
            /// todo : gainRatio might be all nils
            ///
            if (highestInformationGain == 0)
            {
                return(__GetRootNodeFroClass_tblAssumeDwelt(data, inEdge));
            }

            return(new TreeNode(attributes[highestInformationGainIndex].Name, highestInformationGainIndex, attributes[highestInformationGainIndex], inEdge));
        }