public void F64Matrix_GetColumns() { // matrix is created row wise var matrix = new F64Matrix(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 3, 3); // returns selected columns a a new matrix var cols = matrix.Columns(new int[] { 0, 2 }); // [1, 3, // 4, 6 // 7, 9] }
double SelectNextModelToRemove(F64Matrix crossValidatedModelPredictions, double[] targets, double currentBestError) { var candidateModelMatrix = new F64Matrix(crossValidatedModelPredictions.RowCount, m_remainingModelIndices.Count - 1); var candidatePredictions = new double[crossValidatedModelPredictions.RowCount]; var candidateModelIndices = new int[m_remainingModelIndices.Count - 1]; var bestError = double.MaxValue; 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]; } } crossValidatedModelPredictions.Columns(candidateModelIndices, candidateModelMatrix); 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); }
double SelectNextModelToAdd(F64Matrix crossValidatedModelPredictions, double[] targets, double currentBestError) { var candidateModelMatrix = new F64Matrix(crossValidatedModelPredictions.RowCount, m_selectedModelIndices.Count + 1); var candidatePredictions = new double[crossValidatedModelPredictions.RowCount]; 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; crossValidatedModelPredictions.Columns(candidateModelIndices, candidateModelMatrix); 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); }
/// <summary> /// Iterative random selection of ensemble models. /// </summary> /// <param name="crossValidatedModelPredictions">cross validated predictions from multiple models. /// Each column 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(F64Matrix crossValidatedModelPredictions, double[] targets) { if (crossValidatedModelPredictions.ColumnCount < m_numberOfModelsToSelect) { throw new ArgumentException("Available models: " + crossValidatedModelPredictions.ColumnCount + " is smaller than number of models to select: " + m_numberOfModelsToSelect); } m_allIndices = Enumerable.Range(0, crossValidatedModelPredictions.ColumnCount).ToArray(); var bestModelIndices = new int[m_numberOfModelsToSelect]; var candidateModelIndices = new int[m_numberOfModelsToSelect]; var candidateModelMatrix = new F64Matrix(crossValidatedModelPredictions.RowCount, m_numberOfModelsToSelect); var candidatePredictions = new double[crossValidatedModelPredictions.RowCount]; var bestError = double.MaxValue; for (int i = 0; i < m_iterations; i++) { SelectNextRandomIndices(candidateModelIndices); crossValidatedModelPredictions.Columns(candidateModelIndices, candidateModelMatrix); 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); }