public C45AlgorithmDataOptimized(TableFixedData data, TreeOptions options)
 {
     _data = data;
     if (options == null)
     {
         options = new TreeOptions();
     }
     _options = options;
 }
Exemple #2
0
        public Forest BuildForest(TableFixedData data, TreeOptions options = null, int[] attributes = null)
        {
            if (options == null)
            {
                options = new TreeOptions();
            }
            var countOfItemsInTree     = Convert.ToInt32(_sampleRatio * data.Count);
            var countOfVariablesInTree =
                Convert.ToInt32(_coverageRatio.HasValue
                    ? Math.Floor(_coverageRatio.Value * (data.Attributes.Length - 1))
                    : Math.Ceiling(Math.Sqrt(data.Attributes.Length - 1)));

            var rows = Enumerable.Range(0, data.Count).ToArray();

            if (attributes == null)
            {
                attributes = new int[data.Attributes.Length - 1];
                var currentIndex = 0;
                for (int index = 0; index < data.Attributes.Length; index++)
                {
                    if (data.IsClassAttribute(index))
                    {
                        continue;
                    }
                    attributes[currentIndex++] = index;
                }
            }

            var sampleRows       = new int[countOfItemsInTree];
            var sampleAttributes = new int[countOfVariablesInTree];
            var c45Algorithm     = new C45AlgorithmDataOptimized(data, options);
            var ret = new Forest(data.ClassesValue.Length);


            for (int i = 0; i < _trees; i++)
            {
                rows.Sample(sampleRows);
                attributes.Sample(sampleAttributes);
                var tree = c45Algorithm.BuildConditionalTree(sampleRows, sampleAttributes);
                ret.Add(tree);
            }

            return(ret);
        }