Exemplo n.º 1
0
        /// <summary>
        /// Predicts a single observation using the ensembled probabilities
        /// </summary>
        /// <param name="observation"></param>
        /// <returns></returns>
        public ProbabilityPrediction PredictProbability(double[] observation)
        {
            var ensembleCols = m_ensembleModels.Length;

            var ensemblePredictions = new ProbabilityPrediction[ensembleCols];

            for (int i = 0; i < m_ensembleModels.Length; i++)
            {
                ensemblePredictions[i] = m_ensembleModels[i].Predict(observation);
            }

            return(m_ensembleStrategy.Combine(ensemblePredictions));
        }
        double SelectNextModelToAdd(ProbabilityPrediction[][] crossValidatedModelPredictions, 
            double[] targets, 
            double currentBestError)
        {
            var rows = crossValidatedModelPredictions.First().Length;
            var candidateModelMatrix = new ProbabilityPrediction[m_selectedModelIndices.Count + 1][];
            var candidatePredictions = new ProbabilityPrediction[rows];
            var candidateModelIndices = new int[m_selectedModelIndices.Count + 1];

            var bestError = currentBestError;
            var bestIndex = -1;

            foreach (var index in m_remainingModelIndices)
            {
                m_selectedModelIndices.CopyTo(candidateModelIndices);
                candidateModelIndices[candidateModelIndices.Length - 1] = index;

                for (int i = 0; i < candidateModelIndices.Length; i++)
                {
                    candidateModelMatrix[i] = crossValidatedModelPredictions[candidateModelIndices[i]];
                }

                m_ensembleStrategy.Combine(candidateModelMatrix, candidatePredictions);
                var error = m_metric.Error(targets, candidatePredictions);

                if (error < bestError)
                {
                    bestError = error;
                    bestIndex = index;
                }
            }

            if(bestIndex != -1)
            {
                m_selectedModelIndices.Add(bestIndex);
                
                if(!m_selectWithReplacement)
                {
                    m_remainingModelIndices.Remove(bestIndex);
                }
            }

            return bestError;
        }
        double SelectNextModelToRemove(ProbabilityPrediction[][] crossValidatedModelPredictions,
                                       double[] targets,
                                       double currentBestError)
        {
            var rows = crossValidatedModelPredictions.First().Length;
            var candidateModelMatrix  = new ProbabilityPrediction[m_remainingModelIndices.Count - 1][];
            var candidatePredictions  = new ProbabilityPrediction[rows];
            var candidateModelIndices = new int[m_remainingModelIndices.Count - 1];

            var bestError = currentBestError;
            var bestIndex = -1;

            foreach (var index in m_remainingModelIndices)
            {
                var candidateIndex = 0;
                for (int i = 0; i < m_remainingModelIndices.Count; i++)
                {
                    var curIndex = m_remainingModelIndices[i];
                    if (curIndex != index)
                    {
                        candidateModelIndices[candidateIndex++] = m_remainingModelIndices[i];
                    }
                }

                for (int i = 0; i < candidateModelIndices.Length; i++)
                {
                    candidateModelMatrix[i] = crossValidatedModelPredictions[candidateModelIndices[i]];
                }

                m_ensembleStrategy.Combine(candidateModelMatrix, candidatePredictions);
                var error = m_metric.Error(targets, candidatePredictions);

                if (error < bestError)
                {
                    bestError = error;
                    bestIndex = index;
                }
            }

            m_remainingModelIndices.Remove(bestIndex);

            return(bestError);
        }
        /// <summary>
        /// Greedy forward selection of ensemble models.
        /// </summary>
        /// <param name="crossValidatedModelPredictions">cross validated predictions from multiple models.
        /// Each row in the matrix corresponds to predictions from a separate model</param>
        /// <param name="targets">Corresponding targets</param>
        /// <returns>The indices of the selected model</returns>
        public int[] Select(ProbabilityPrediction[][] crossValidatedModelPredictions, double[] targets)
        {
            if (crossValidatedModelPredictions.Length < m_numberOfModelsToSelect)
            {
                throw new ArgumentException("Availible models: " + crossValidatedModelPredictions.Length +
                                            " is smaller than number of models to select: " + m_numberOfModelsToSelect);
            }

            m_allIndices = Enumerable.Range(0, crossValidatedModelPredictions.Length).ToArray();

            var rows = crossValidatedModelPredictions.First().Length;
            var candidateModelMatrix  = new ProbabilityPrediction[m_numberOfModelsToSelect][];
            var candidatePredictions  = new ProbabilityPrediction[rows];
            var candidateModelIndices = new int[m_numberOfModelsToSelect];
            var bestModelIndices      = new int[m_numberOfModelsToSelect];

            var bestError = double.MaxValue;

            for (int i = 0; i < m_iterations; i++)
            {
                SelectNextRandomIndices(candidateModelIndices);

                for (int j = 0; j < candidateModelIndices.Length; j++)
                {
                    candidateModelMatrix[j] = crossValidatedModelPredictions[candidateModelIndices[j]];
                }

                m_ensembleStrategy.Combine(candidateModelMatrix, candidatePredictions);
                var error = m_metric.Error(targets, candidatePredictions);

                if (error < bestError)
                {
                    bestError = error;
                    candidateModelIndices.CopyTo(bestModelIndices, 0);
                    Trace.WriteLine("Models selected: " + bestModelIndices.Length + ": " + error);
                }
            }

            Trace.WriteLine("Selected model indices: " + string.Join(", ", bestModelIndices.ToArray()));

            return(bestModelIndices);
        }