Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/06/17 10:31:09 (7 years ago)
Author:
gkronber
Message:

#2690: merged r14839,r15103:15106,r15123,r15124 from trunk to stable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stable/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/RandomForestModelView.cs

    r15127 r15134  
    1818 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
    1919 */
    20 #endregion
     20#endregion           
    2121
    22 using System.Drawing;
     22using HeuristicLab.Common;
     23using HeuristicLab.Core.Views;
    2324using HeuristicLab.MainForm;
    2425using HeuristicLab.Problems.DataAnalysis;
    25 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    26 using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
     26using HeuristicLab.Problems.DataAnalysis.Symbolic;                       
    2727using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    28 using HeuristicLab.Problems.DataAnalysis.Views;
    2928
    3029namespace HeuristicLab.Algorithms.DataAnalysis.Views {
    3130  [View("Random forest model")]
    32   [Content(typeof(IRandomForestRegressionSolution), false)]
    33   [Content(typeof(IRandomForestClassificationSolution), false)]
    34   public partial class RandomForestModelView : DataAnalysisSolutionEvaluationView {
    35     public override Image ViewImage {
    36       get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
     31  [Content(typeof(IRandomForestModel), true)]
     32  public partial class RandomForestModelView : ItemView {
     33
     34    public new IRandomForestModel Content {
     35      get { return (IRandomForestModel)base.Content; }
     36      set { base.Content = value; }
    3737    }
    3838
     
    5656        viewHost.Content = null;
    5757        listBox.Items.Clear();
    58         var classSol = Content as IRandomForestClassificationSolution;
    59         var regSol = Content as IRandomForestRegressionSolution;
    60         var numTrees = classSol != null ? classSol.NumberOfTrees : regSol != null ? regSol.NumberOfTrees : 0;
     58        var rfModel = Content;
     59        var numTrees = rfModel.NumberOfTrees;
    6160        for (int i = 0; i < numTrees; i++) {
    6261          listBox.Items.Add(i + 1);
     
    6968      else {
    7069        var idx = (int)listBox.SelectedItem;
    71         idx -= 1;
    72         var rfModel = Content.Model as RandomForestModel;
    73         var regProblemData = Content.ProblemData as IRegressionProblemData;
    74         var classProblemData = Content.ProblemData as IClassificationProblemData;
    75         if (rfModel != null) {
    76           if (idx < 0 || idx >= rfModel.NumberOfTrees) return;
    77           if (regProblemData != null) {
    78             var syModel = new SymbolicRegressionModel(regProblemData.TargetVariable, rfModel.ExtractTree(idx),
    79               new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
    80             viewHost.Content = syModel.CreateRegressionSolution(regProblemData);
    81           } else if (classProblemData != null) {
    82             var syModel = new SymbolicDiscriminantFunctionClassificationModel(classProblemData.TargetVariable, rfModel.ExtractTree(idx),
    83               new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), new NormalDistributionCutPointsThresholdCalculator());
    84             syModel.RecalculateModelParameters(classProblemData, classProblemData.TrainingIndices);
    85             viewHost.Content = syModel.CreateClassificationSolution(classProblemData);
    86           }
    87         }
     70        viewHost.Content = CreateModel(idx);
    8871      }
     72    }
     73
     74    private void listBox_DoubleClick(object sender, System.EventArgs e) {
     75      var selectedItem = listBox.SelectedItem;
     76      if (selectedItem == null) return;
     77      var idx = (int)listBox.SelectedItem;
     78      MainFormManager.MainForm.ShowContent(CreateModel(idx));
     79    }
     80
     81    private IContent CreateModel(int idx) {
     82      idx -= 1;
     83      var rfModel = Content;
     84      var rfClassModel = rfModel as IClassificationModel; // rfModel is always a IRegressionModel and a IClassificationModel
     85      var targetVariable = rfClassModel.TargetVariable;
     86      if (rfModel == null) return null;
     87      if (idx < 0 || idx >= rfModel.NumberOfTrees)
     88        return null;
     89      var syModel = new SymbolicRegressionModel(targetVariable, rfModel.ExtractTree(idx),
     90          new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
     91      return syModel;
    8992    }
    9093  }
Note: See TracChangeset for help on using the changeset viewer.