- Timestamp:
- 07/06/17 10:31:09 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
stable/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/RandomForestModelView.cs
r15127 r15134 18 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 19 */ 20 #endregion 20 #endregion 21 21 22 using System.Drawing; 22 using HeuristicLab.Common; 23 using HeuristicLab.Core.Views; 23 24 using HeuristicLab.MainForm; 24 25 using HeuristicLab.Problems.DataAnalysis; 25 using HeuristicLab.Problems.DataAnalysis.Symbolic; 26 using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification; 26 using HeuristicLab.Problems.DataAnalysis.Symbolic; 27 27 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 28 using HeuristicLab.Problems.DataAnalysis.Views;29 28 30 29 namespace HeuristicLab.Algorithms.DataAnalysis.Views { 31 30 [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; } 37 37 } 38 38 … … 56 56 viewHost.Content = null; 57 57 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; 61 60 for (int i = 0; i < numTrees; i++) { 62 61 listBox.Items.Add(i + 1); … … 69 68 else { 70 69 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); 88 71 } 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; 89 92 } 90 93 }
Note: See TracChangeset
for help on using the changeset viewer.