Esempio n. 1
0
        /// <summary>
        /// Parses a random forest from a weka output file
        /// </summary>
        public static RandomForest FromWekaFile(string file)
        {
            var    output = new RandomForest(new HashSet <string>(s_classLabels));
            var    reader = new StreamReader(file);
            int    id     = 0;
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                // keep reading until we find one of these. This one is followed
                // by another line with equal signs and then empty lines until we find the first text
                if (line.StartsWith("RandomTree"))
                {
                    ++id;
                    string firstLine;
                    reader.ReadLine();
                    while ((firstLine = reader.ReadLine()).Trim().Length == 0)
                    {
                        continue;
                    }
                    // now, we can start reading the tree
                    output.Trees.Add(RandomTree.FromStream(reader, firstLine, DefaultPrecision, id));
                }
            }
            reader.Close();
            return(output);
        }
Esempio n. 2
0
        internal static RandomTree FromStream(StreamReader reader, string firstLine, int nodePrecision, int id)
        {
            var output = new RandomTree()
            {
                Id = id
            };
            var            keepReading = true;
            RandomTreeNode previous    = null;
            int            initialId   = 0;
            bool           first       = true;

            while (keepReading)
            {
                ++initialId;
                try
                {
                    var line = first ? firstLine : reader.ReadLine();
                    var node = RandomTreeNode.BuildFromString(line, initialId, nodePrecision);
                    output.NumNodes = +1;
                    if (node.Level == 0)
                    {
                        // for roots
                        output.Roots.Add(node);
                        previous = node;
                        first    = false;
                    }
                    else
                    {
                        // count the levels
                        if (node.Level > previous.Level)
                        {
                            // child
                            previous.Children.Add(node);
                            node.Parent = previous;
                        }
                        else if (node.Level < previous.Level)
                        {
                            // predecessor: travel backwards and look for the first parent with level = level
                            while (previous != null)
                            {
                                if (previous.Level == node.Level - 1)
                                {
                                    // we found its parent
                                    previous.Children.Add(node);
                                    node.Parent = previous;
                                    break;
                                }
                                previous = previous.Parent;
                            }
                        }
                        else
                        {
                            // same level, they are siblings
                            previous.Parent.Children.Add(node);
                            node.Parent = previous.Parent;
                        }
                        previous = node;
                    }
                }
                #pragma warning disable ERP022
                catch (Exception)
                {
                    // we could not read, so we are in the line after we finished.. how do we know? cause we could not build a predicate
                    break;
                }
                #pragma warning disable ERP022
            }
            // done...
            return(output);
        }