Ejemplo n.º 1
0
        public TreeTrainingOperation(
            Random randomNumberGenerator,
            ITrainingContext <F, S> trainingContext,
            TrainingParameters parameters,
            IDataPointCollection data,
            ProgressWriter progress)
        {
            data_            = data;
            trainingContext_ = trainingContext;
            parameters_      = parameters;

            random_   = randomNumberGenerator;
            progress_ = progress;

            indices_ = new int[data.Count()];
            for (int i = 0; i < indices_.Length; i++)
            {
                indices_[i] = i;
            }

            responses_ = new float[data.Count()];

            parentStatistics_ = trainingContext_.GetStatisticsAggregator();

            leftChildStatistics_  = trainingContext_.GetStatisticsAggregator();
            rightChildStatistics_ = trainingContext_.GetStatisticsAggregator();

            partitionStatistics_ = new S[parameters.NumberOfCandidateThresholdsPerFeature + 1];
            for (int i = 0; i < parameters.NumberOfCandidateThresholdsPerFeature + 1; i++)
            {
                partitionStatistics_[i] = trainingContext_.GetStatisticsAggregator();
            }
        }
        public void Aggregate(IDataPointCollection data, int index)
        {
            DataPointCollection concreteData = (DataPointCollection)(data);

            bins_[concreteData.GetIntegerLabel((int)index)]++;
            sampleCount_ += 1;
        }
        /// <summary>
        /// Train a new decision forest given some training data and a training
        /// problem described by an instance of the ITrainingContext interface.
        /// </summary>
        /// <param name="random">Random number generator.</param>
        /// <param name="parameters">Training parameters.</param>
        /// <param name="maxThreads">The maximum number of threads to use.</param>
        /// <param name="context">An ITrainingContext instance describing
        /// the training problem, e.g. classification, density estimation, etc. </param>
        /// <param name="data">The training data.</param>
        /// <returns>A new decision forest.</returns>
        public static Forest <F, S> TrainForest(
            Random random,
            TrainingParameters parameters,
            ITrainingContext <F, S> context,
            int maxThreads,
            IDataPointCollection data,
            ProgressWriter progress = null)
        {
            if (progress == null)
            {
                progress = new ProgressWriter(parameters.Verbose?Verbosity.Verbose:Verbosity.Interest, Console.Out);
            }

            Forest <F, S> forest = new Forest <F, S>();

            for (int t = 0; t < parameters.NumberOfTrees; t++)
            {
                progress.Write(Verbosity.Interest, "\rTraining tree {0}...", t);

                Tree <F, S> tree = ParallelTreeTrainer <F, S> .TrainTree(random, context, parameters, maxThreads, data, progress);

                forest.AddTree(tree);
            }
            progress.WriteLine(Verbosity.Interest, "\rTrained {0} trees.         ", parameters.NumberOfTrees);

            return(forest);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Train a new decision tree given some training data and a training
        /// problem described by an ITrainingContext instance.
        /// </summary>
        /// <param name="random">The single random number generator.</param>
        /// <param name="progress">Progress reporting target.</param>
        /// <param name="context">The ITrainingContext instance by which
        /// the training framework interacts with the training data.
        /// Implemented within client code.</param>
        /// <param name="parameters">Training parameters.</param>
        /// <param name="data">The training data.</param>
        /// <returns>A new decision tree.</returns>
        static public Tree <F, S> TrainTree(
            Random random,
            ITrainingContext <F, S> context,
            TrainingParameters parameters,
            IDataPointCollection data,
            ProgressWriter progress = null)
        {
            if (progress == null)
            {
                progress = new ProgressWriter(Verbosity.Interest, Console.Out);
            }
            TreeTrainingOperation <F, S> trainingOperation = new TreeTrainingOperation <F, S>(
                random, context, parameters, data, progress);

            Tree <F, S> tree = new Tree <F, S>(parameters.MaxDecisionLevels);

            progress.WriteLine(Verbosity.Verbose, "");

            trainingOperation.TrainNodesRecurse(tree.nodes_, 0, 0, data.Count(), 0); // will recurse until termination criterion is met

            progress.WriteLine(Verbosity.Verbose, "");

            tree.CheckValid();

            return(tree);
        }
Ejemplo n.º 5
0
 public AlertSystem()
 {
     dataPoints = new DataPointCollection();
     analysts   = new HashSet <IAnalyst>();
     clients    = new HashSet <IClient>();
     rules      = new List <IRule>();
 }
Ejemplo n.º 6
0
        public void Aggregate(IDataPointCollection data, int index, Object userData)
        {
            DataPointCollection concreteData  = (DataPointCollection)(data);
            HistogramData       histogramData = (HistogramData)(userData);

            histogramData.Increment(dataHandle_, concreteData.GetIntegerLabel((int)index));
            // bins_[concreteData.GetIntegerLabel((int)index)]++;
            sampleCount_ += 1;
        }
        public void Aggregate(IDataPointCollection data, int index)
        {
            DataPointCollection concreteData = (DataPointCollection)(data);

            // Always aggregate density statistics
            GaussianAggregator2d.Aggregate(data, index);

            // Only aggregate histogram statistics for those data points that have class labels
            if (concreteData.GetIntegerLabel((int)(index)) != DataPointCollection.UnknownClassLabel)
            {
                HistogramAggregator.Aggregate(data, index);
            }
        }
        public void Aggregate(IDataPointCollection data, int index)
        {
            DataPointCollection concreteData = (DataPointCollection)(data);

            sx_ += concreteData.GetDataPoint((int)index)[0];
            sy_ += concreteData.GetDataPoint((int)index)[1];

            sxx_ += Math.Pow(concreteData.GetDataPoint((int)index)[0], 2.0);
            syy_ += Math.Pow(concreteData.GetDataPoint((int)index)[1], 2.0);

            sxy_ += concreteData.GetDataPoint((int)index)[0] * concreteData.GetDataPoint((int)index)[1];

            sampleCount_ += 1;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Apply a forest of trees to a set of data points.
        /// </summary>
        /// <param name="data">The data points.</param>
        public int[][] Apply(IDataPointCollection data, ProgressWriter progress = null)
        {
            if (progress == null)
            {
                progress = new ProgressWriter(Verbosity.Interest, Console.Out);
            }

            int[][] leafNodeIndices = new int[TreeCount][];

            for (int t = 0; t < TreeCount; t++)
            {
                progress.Write(Verbosity.Interest, "\rApplying tree {0}...", t);
                leafNodeIndices[t] = trees_[t].Apply(data);
            }
            progress.WriteLine(Verbosity.Interest, "\rApplied {0} trees.      ", TreeCount);

            return(leafNodeIndices);
        }
        public void Aggregate(IDataPointCollection data, int index)
        {
            DataPointCollection concreteData = (DataPointCollection)(data);

            float[] datum  = concreteData.GetDataPoint((int)index);
            float   target = concreteData.GetTarget((int)index);

            XT_X_11_ += datum[0] * datum[0];
            XT_X_12_ += datum[0];
            XT_X_21_ += datum[0];
            XT_X_22_ += 1.0;

            XT_Y_1_ += datum[0] * target;
            XT_Y_2_ += target;

            Y2_ += target * target;

            sampleCount_ += 1;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Apply the decision tree to a collection of test data points.
        /// </summary>
        /// <param name="data">The test data.</param>
        /// <returns>An array of leaf node indices per data point.</returns>
        public int[] Apply(IDataPointCollection data)
        {
            CheckValid();

            int[] leafNodeIndices = new int[data.Count()]; // of leaf node reached per data point

            // Allocate temporary storage for data point indices and response values
            int[] dataIndices_ = new int[data.Count()];
            for (int i = 0; i < data.Count(); i++)
            {
                dataIndices_[i] = i;
            }

            float[] responses_ = new float[data.Count()];

            ApplyNode(0, data, dataIndices_, 0, data.Count(), leafNodeIndices, responses_);

            return(leafNodeIndices);
        }
Ejemplo n.º 12
0
        private void ApplyNode(
            int nodeIndex,
            IDataPointCollection data,
            int[] dataIndices,
            int i0,
            int i1,
            int[] leafNodeIndices,
            float[] responses_)
        {
            System.Diagnostics.Debug.Assert(nodes_[nodeIndex].IsNull == false);

            Node <F, S> node = nodes_[nodeIndex];

            if (node.IsLeaf)
            {
                for (int i = i0; i < i1; i++)
                {
                    leafNodeIndices[dataIndices[i]] = nodeIndex;
                }
                return;
            }

            if (i0 == i1) // No samples left
            {
                return;
            }

            for (int i = i0; i < i1; i++)
            {
                responses_[i] = node.Feature.GetResponse(data, dataIndices[i]);
            }

            int ii = Partition(responses_, dataIndices, i0, i1, node.Threshold);

            // Recurse for child nodes.
            ApplyNode(nodeIndex * 2 + 1, data, dataIndices, i0, ii, leafNodeIndices, responses_);
            ApplyNode(nodeIndex * 2 + 2, data, dataIndices, ii, i1, leafNodeIndices, responses_);
        }
        public ParallelTreeTrainingOperation(
            Random random,
            ITrainingContext <F, S> trainingContext,
            TrainingParameters parameters,
            int maxThreads,
            IDataPointCollection data,
            ProgressWriter progress)
        {
            data_            = data;
            trainingContext_ = trainingContext;
            parameters_      = parameters;

            maxThreads_ = maxThreads;

            random_ = random;

            progress_ = progress;

            parentStatistics_ = trainingContext_.GetStatisticsAggregator();

            leftChildStatistics_  = trainingContext_.GetStatisticsAggregator();
            rightChildStatistics_ = trainingContext_.GetStatisticsAggregator();

            responses_ = new float[data.Count()];

            indices_ = new int[data.Count()];
            for (int i = 0; i < indices_.Length; i++)
            {
                indices_[i] = i;
            }

            threadLocals_ = new ThreadLocalData[maxThreads_];
            for (int threadIndex = 0; threadIndex < maxThreads_; threadIndex++)
            {
                threadLocals_[threadIndex] = new ThreadLocalData(random_, trainingContext_, parameters_, data_);
            }
        }
            public ThreadLocalData(Random random, ITrainingContext <F, S> trainingContext_, TrainingParameters parameters, IDataPointCollection data)
            {
                parentStatistics_ = trainingContext_.GetStatisticsAggregator();

                leftChildStatistics_  = trainingContext_.GetStatisticsAggregator();
                rightChildStatistics_ = trainingContext_.GetStatisticsAggregator();

                partitionStatistics_ = new S[parameters.NumberOfCandidateThresholdsPerFeature + 1];
                for (int i = 0; i < parameters.NumberOfCandidateThresholdsPerFeature + 1; i++)
                {
                    partitionStatistics_[i] = trainingContext_.GetStatisticsAggregator();
                }

                responses_ = new float[data.Count()];

                random_ = new Random(random.Next());
            }
        public float GetResponse(IDataPointCollection data, int index)
        {
            DataPointCollection concreteData = (DataPointCollection)(data);

            return(dx_ * concreteData.GetDataPoint((int)index)[0] + dy_ * concreteData.GetDataPoint((int)index)[1]);
        }
        public float GetResponse(IDataPointCollection data, int sampleIndex)
        {
            DataPointCollection concreteData = (DataPointCollection)(data);

            return(concreteData.GetDataPoint((int)sampleIndex)[axis_]);
        }
 public float GetResponse(IDataPointCollection data, int sampleIndex)
 {
     DataPointCollection concreteData = (DataPointCollection)(data);
       return concreteData.GetDataPoint((int)sampleIndex)[axis_];
 }
 public float GetResponse(IDataPointCollection data, int index)
 {
     DataPointCollection concreteData = (DataPointCollection)(data);
       return dx_ * concreteData.GetDataPoint((int)index)[0] + dy_ * concreteData.GetDataPoint((int)index)[1];
 }