Exemplo n.º 1
0
        public Frequency(AttributePack ap, string attributeName)
        {
            string classifierName = ap.classifierName;

            attributePack = ap;

            foreach (AttributeSet ds in ap.attributeSets)
            {
                //Values durchgehen
                string attributeValue;
                ds.attributes.TryGetValue(attributeName, out attributeValue);
                // check ob bereits existiert
                if (!_freqTable.ContainsKey(attributeValue))
                {
                    _freqTable.Add(attributeValue, new Dictionary <string, int>());
                }
                // classifier bestimmen
                string classifierValue;
                ds.attributes.TryGetValue(classifierName, out classifierValue);

                // classifier value checken und hochzählen
                Dictionary <string, int> a;
                if (_freqTable.TryGetValue(attributeValue, out a))
                {
                    if (!a.ContainsKey(classifierValue))
                    {
                        a.Add(classifierValue, 0);
                    }
                }

                _freqTable[attributeValue][classifierValue]++;
            }
        }
Exemplo n.º 2
0
        public void buildTree(AttributePack dp, TreeNode node)
        {
            double maxGain      = 0;
            string maxAttribute = null;

            if (dp.attributeNames.Count < 1)
            {
                //leave node mit mehrheit
                node.value = dp.attributeSets.Max(ds => ds.attributes[dp.classifierName]);
                return;
            }
            if (dp.attributeSets.All(ds => ds.attributes[dp.classifierName] == dp.attributeSets.First().attributes[dp.classifierName]))
            {
                node.value = dp.attributeSets.First().attributes[dp.classifierName];
                return;
            }

            foreach (string attributeName in dp.attributeNames)
            {
                // create Frequency table
                Frequency table = new Frequency(dp, attributeName);
                double    gain  = table.calcInfoGain();

                if (gain > maxGain)
                {
                    maxGain      = gain;
                    maxAttribute = attributeName;
                }
            }

            node.value = maxAttribute;

            Dictionary <string, AttributePack> dataPacks = dp.sortDatapack(maxAttribute);

            foreach (var d in dataPacks)
            {
                TreeNode newNode = new TreeNode();
                node.nodes.Add(d.Key, newNode);
                buildTree(d.Value, newNode);
            }
        }