public void Fit(List <List <int> > X, List <int> y)
        {
            int numExamples = X.Count;
            int numFeatures = numExamples != 0 ? X[0].Count : 0;

            _stump.Fit(X, y);

            if (_maxDepth <= 1 || _stump.splitVariable == -1)
            {
                subModel1 = null;
                subModel0 = null;
                return;
            }

            var y1 = new List <int>();
            var y0 = new List <int>();

            for (int i = 0; i < numExamples; i++)
            {
                if (X[i][_stump.splitVariable] > _stump.splitValue)
                {
                    y1.Add(y[i]);
                }
                else
                {
                    y0.Add(y[i]);
                }
            }

            // TODO fix this to add back in randomness.
            if (y1.Count != 0)
            {
                subModel1 = new DecisionTree(_maxDepth - 1, new DecisionStumpInfoGain());
                subModel1.Fit(X.Where(x => x[_stump.splitVariable] > _stump.splitValue).ToList(), y1);
            }
            else
            {
                subModel1 = null;
            }

            if (y0.Count != 0)
            {
                subModel0 = new DecisionTree(_maxDepth - 1, new DecisionStumpInfoGain());
                subModel0.Fit(X.Where(x => x[_stump.splitVariable] <= _stump.splitValue).ToList(), y0);
            }
            else
            {
                subModel0 = null;
            }
        }
Beispiel #2
0
        public void Fit(List <List <int> > X, List <int> y)
        {
            // TODO this is duplicated.
            int numExamples = X.Count;

            var bootstrapX = new List <List <int> >();
            var bootstrapY = new List <int>();

            // Fill up array with random examples with replacement.
            var rand = new Random();

            for (int i = 0; i < numExamples; i++)
            {
                var idx = rand.Next(0, numExamples);
                bootstrapX.Add(X[idx]);
                bootstrapY.Add(y[idx]);
            }

            decisionTreeRoot.Fit(bootstrapX, bootstrapY);
        }