IDmRandomForest.ForestSize

Syntax

ForestSize: Integer;

ForestSize: integer;

Description

The ForestSize property determines the number of trees in random forest.

Comments

Available values within the [1; ∞) range.

Default value is 100.

Examples

Executing the example requires that the repository contains a table containing data for analysis with the DM_TABLE identifier. A regular report with the DM_REPORT_RES identifier where analysis results will be loaded must also be present.

Add links to the Metabase, Ms, Report, Stat, Tab, Ui system assemblies.

Sub UserRandFor;
Var
    mb: IMetabase;
    TableDS: IDmTableDataSource;
    ReportDS: IDmReportDataSource;
    Method: IDmMethod;
    Report: IPrxReport;
    Shs: IPrxSheets;
    Sheet: ITabSheet;
    RandFor: IDmRandomForest;
    Binning: IDmField;
    i: Integer;
    Attrs: Array 
Of Integer;
    Target: IUiCommandTarget;
    Reports: IDmReports;
    DmReport: IDmReport;
Begin
    mb := MetabaseClass.Active;
    
// Create table data source
    TableDS := (New TableDataSource.Create) As IDmTableDataSource;
    
// Determine source table
    TableDS.Table := mb.ItemByID("DM_TABLE").Bind;
    
// Determine that data is located in table columns
    TableDS.DataInColumns := True;
    
// Create data source which is a regular report
    ReportDS := (New ReportDataSource.Create) As IDmReportDataSource;
    
// Get regular report
    Report := mb.ItemByID("DM_REPORT_RES").Edit As IPrxReport;
    Shs := Report.Sheets;
    Shs.Clear;
    
// Create page to load results
    Sheet := (Shs.Add("", PrxSheetType.Table) As IPrxTable).TabSheet;
    
// Determine page to which data will be loaded    
    ReportDS.TabSheet := Sheet;
    
// Determine data range
    ReportDS.Range := Sheet.Cell(00);
    ReportDS.AddResultColumn(
"Category");
    
// Create calculation method
    Method := (New DataMiningMethod.Create) As IDmMethod;
    
// Determine method type
    Method.Kind := DmMethodKind.RandomForest;
    
// Set entry data source
    Method.InputDataSource := TableDS;
    
// Set data consumer
    Method.OutputDataSource := ReportDS;
    
// Set up calculation method parameters
    RandFor := Method.Details As IDmRandomForest;
    
// Forest size
    RandFor.ForestSize := 20;
    RandFor.LearningSamplePortion := 
0.6;
    
// Number of attributes
    RandFor.NumberOfPredictors := 2;
    
//Forest size
    RandFor.TreeSizeSpecification.MaximumNumberOfLevels := 10;
    RandFor.TreeSizeSpecification.MinimumNumberOfCases := 
2;
    
// Consider each category as an attribute
    RandFor.TreeSizeSpecification.ReduceCategories := True;
    
// Determine column for analysis
    RandFor.Target := TableDS.FieldCount - 1;
    Debug.WriteLine(
"Column for key impact factors analysis:");
    Debug.WriteLine(
" - " + TableDS.Field(RandFor.Target).Name);
    
// Set factors impacting analysis
    Attrs := New Integer[TableDS.FieldCount - 2];
    Debug.WriteLine(
"Factors that impact analysis:");
    
For i := 0 To Attrs.Length - 1 Do
        Attrs[i] := i + 
1;
        Binning := TableDS.Field(i + 
1);
        Debug.WriteLine(Binning.Index.ToString + 
". " + Binning.Name);
        Debug.Indent;
        
// Set parameters of the Binning procedure
        If Binning.IsNumerical Then
            Binning.BinningType := BinningMethod.EqualDepth;
            Binning.CategoriesCount := 
4;
            Binning.TreatNanAsCategory := 
False;
            Debug.WriteLine(
"number of non empty values: " + Binning.NonEmptyCount.ToString);
        
End If;
        
Select Case Binning.FieldType
            
Case DmFieldType.Date: Debug.WriteLine("data type: date");
            
Case DmFieldType.Integer: Debug.WriteLine("data type: integer");
            
Case DmFieldType.Numeric: Debug.WriteLine("data type: numeric");
            
Case DmFieldType.String: Debug.WriteLine("data type: string");
        
End Select;
        Debug.WriteLine(
"data source: " + Binning.DataSource.Caption);
        
Select Case Binning.ExplanatoryType
            
Case DmExplanatoryType.Continuous: Debug.WriteLine("factor type: quantitative");
            
Case DmExplanatoryType.Ordered: Debug.WriteLine("factor type: quantitative");
            
Case DmExplanatoryType.Categorical: Debug.WriteLine("factor type: quantitative");
        
End Select;
        Debug.Unindent;
    
End For;
    RandFor.Attributes := Attrs;
    
// Analyze and load results
    Reports := Method.Execute;
    DmReport := reports.FindByType(DmReportType.Forest);
    ReportDS := DmReport.Generate;
    Debug.WriteLine(
"Report title: " + DmReport.Caption);
    Debug.WriteLine(
"Analysis type: " + RandFor.DisplayName);
    ReportDS.TabSheet.View.Selection.SelectAll;
    ReportDS.TabSheet.View.Selection.Copy;
    Sheet.Table.Paste;
    Sheet.Columns(
01).AdjustWidth;
    Sheet.Rows(
01).AdjustHeight;
    Report.Sheets.Item(
0).Name := ReportDS.Caption;
    (Report 
As IMetabaseObject).Save;
    
// Open regular report containing analysis result
    Target := WinApplication.Instance.GetObjectTarget(Report As IMetabaseObject);
    Target.Execute(
"Object.Open"Null);
End Sub UserRandFor;

Imports Prognoz.Platform.Interop.Metabase;
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Report;
Imports Prognoz.Platform.Interop.Stat;
Imports Prognoz.Platform.Interop.Tab;
Imports Prognoz.Platform.Interop.Ui;

Public Shared Sub Main(Params: StartParams);
Var
    mb: IMetabase;
    TableDS: IDmTableDataSource;
    ReportDS: IDmReportDataSource;
    Method: IDmMethod;
    Report: IPrxReport;
    Shs: IPrxSheets;
    Sheet: TabSheet;
    RandFor: IDmRandomForest;
    Binning: IDmField;
    i: Integer;
    Attrs: Array 
Of Integer;
    Target: IUiCommandTarget;
    Reports: IDmReports;
    DmReport: IDmReport;
    WinApplication: WinApplicationClassClass = 
New WinApplicationClassClass();
Begin
    mb := Params.Metabase;
    
// Create table data source
    TableDS := (New TableDataSource.Create()) As IDmTableDataSource;
    
// Determine source table
    TableDS.Table := mb.ItemByID["DM_TABLE"].Bind();
    
// Determine that data is located in table columns
    TableDS.DataInColumns := True;
    
// Create data source which is a regular report
    ReportDS := (New ReportDataSource.Create()) As IDmReportDataSource;
    
// Get regular report
    Report := mb.ItemByID["DM_REPORT_RES"].Edit() As IPrxReport;
    Shs := Report.Sheets;
    Shs.Clear();
    
// Create page to load results
    Sheet := (Shs.Add("", PrxSheetType.pstTable) As IPrxTable).TabSheet;
    
// Determine page to which data will be loaded    
    ReportDS.TabSheet := Sheet;
    
// Determine data range
    ReportDS.Range := Sheet.Cell[00];
    ReportDS.AddResultColumn(
"Category");
    
// Create calculation method
    Method := (New DataMiningMethod.Create()) As IDmMethod;
    
// Determine method type
    Method.Kind := DmMethodKind.dmmkRandomForest;
    
// Set entry data source
    Method.InputDataSource := TableDS;
    
// Set data consumer
    Method.OutputDataSource := ReportDS;
    
// Set up calculation method parameters
    RandFor := Method.Details As IDmRandomForest;
    
// Forest size
    RandFor.ForestSize := 20;
    RandFor.LearningSamplePortion := 
0.6;
    
// Number of attributes
    RandFor.NumberOfPredictors := 2;
    
//Forest size
    RandFor.TreeSizeSpecification.MaximumNumberOfLevels := 10;
    RandFor.TreeSizeSpecification.MinimumNumberOfCases := 
2;
    
// Consider each category as an attribute
    RandFor.TreeSizeSpecification.ReduceCategories := True;
    
// Determine column for analysis
    RandFor.Target := TableDS.FieldCount - 1;
    System.Diagnostics.Debug.WriteLine(
"Column to analyze key impact factors:");
    System.Diagnostics.Debug.WriteLine(
" - " + TableDS.Field[RandFor.Target].Name);
    
// Set factors impacting analysis
    Attrs := New Integer[TableDS.FieldCount - 2];
    System.Diagnostics.Debug.WriteLine(
"Factors impacting analysis:");
    
For i := 0 To Attrs.Length - 1 Do
        Attrs[i] := i + 
1;
        Binning := TableDS.Field[i + 
1];
        System.Diagnostics.Debug.WriteLine(Binning.Index.ToString() + 
". " + Binning.Name);
        System.Diagnostics.Debug.Indent();
        
// Set parameters of the Binning procedure
        If Binning.IsNumerical Then
            Binning.BinningType := BinningMethod.bmEqualDepth;
            Binning.CategoriesCount := 
4;
            Binning.TreatNanAsCategory := 
False;
            System.Diagnostics.Debug.WriteLine(
"number of non-empty values: " + Binning.NonEmptyCount.ToString());
        
End If;
        
Select Case Binning.FieldType
            
Case DmFieldType.dftDate: System.Diagnostics.Debug.WriteLine("data type: date");
            
Case DmFieldType.dftInteger: System.Diagnostics.Debug.WriteLine("data type: integer");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Case</font><font color="#000000">&nbsp;DmFieldType.dftNumeric:&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;data&nbsp;type:&nbsp;numeric&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Case</font><font color="#000000">&nbsp;DmFieldType.dftString:&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;data&nbsp;type:&nbsp;string&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Select</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Select</font><font color="#000000">&nbsp;</font><font color="#008080">Case</font><font color="#000000">&nbsp;Binning.ExplanatoryType<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Case</font><font color="#000000">&nbsp;DmExplanatoryType.detContinuous:&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;factor&nbsp;type:&nbsp;quantitative&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Case</font><font color="#000000">&nbsp;DmExplanatoryType.detOrdered:&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;factor&nbsp;type:&nbsp;order&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Case</font><font color="#000000">&nbsp;DmExplanatoryType.detCategorical:&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;factor&nbsp;type:&nbsp;categorical&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Select</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;data&nbsp;source:&nbsp;&quot;</font><font color="#000000">&nbsp;+&nbsp;Binning.DataSource.Caption);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Debug.Unindent();<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;RandFor.Attributes&nbsp;:=&nbsp;Attrs;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Analyze&nbsp;and&nbsp;load&nbsp;results<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Reports&nbsp;:=&nbsp;Method.Execute();<br/> &nbsp;&nbsp;&nbsp;&nbsp;DmReport&nbsp;:=&nbsp;reports.FindByType[DmReportType.drtForest];<br/> &nbsp;&nbsp;&nbsp;&nbsp;ReportDS&nbsp;:=&nbsp;DmReport.Generate();<br/> &nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;Report&nbsp;title:&nbsp;&quot;</font><font color="#000000">&nbsp;+&nbsp;DmReport.Caption);<br/> &nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Debug.WriteLine(</font><font color="#800000">&quot;Analysis&nbsp;type:&nbsp;&quot;</font><font color="#000000">&nbsp;+&nbsp;RandFor.DisplayName);<br/> &nbsp;&nbsp;&nbsp;&nbsp;ReportDS.TabSheet.View.Selection.SelectAll();<br/> &nbsp;&nbsp;&nbsp;&nbsp;ReportDS.TabSheet.View.Selection.Copy();<br/> &nbsp;&nbsp;&nbsp;&nbsp;Sheet.Table.Paste();<br/> &nbsp;&nbsp;&nbsp;&nbsp;Sheet.Columns[</font><font color="#008000">0</font><font color="#000000">,&nbsp;</font><font color="#008000">1</font><font color="#000000">].AdjustWidth(-</font><font color="#008000">1</font><font color="#000000">,&nbsp;-</font><font color="#008000">1</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Sheet.Rows[</font><font color="#008000">0</font><font color="#000000">,&nbsp;</font><font color="#008000">1</font><font color="#000000">].AdjustHeight(-</font><font color="#008000">1</font><font color="#000000">,&nbsp;-</font><font color="#008000">1</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Report.Sheets.Item[</font><font color="#008000">0</font><font color="#000000">].Name&nbsp;:=&nbsp;ReportDS.Caption;<br/> &nbsp;&nbsp;&nbsp;&nbsp;(Report&nbsp;</font><font color="#008080">As</font><font color="#000000">&nbsp;IMetabaseObject).Save();<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Open&nbsp;regular&nbsp;report&nbsp;containing&nbsp;analysis&nbsp;results<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Target&nbsp;:=&nbsp;WinApplication.Instance[</font><font color="#008080">Null</font><font color="#000000">].GetObjectTarget(Report&nbsp;</font><font color="#008080">As</font><font color="#000000">&nbsp;IMetabaseObject);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Target.Execute(</font><font color="#800000">&quot;Object.Open&quot;</font><font color="#000000">,&nbsp;</font><font color="#008080">Null</font><font color="#000000">,&nbsp;</font><font color="#008080">Null</font><font color="#000000">);<br/> </font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Sub</font><font color="#000000">;</font>

The result of procedure execution: for data from the DM_TABLE table, data mining is executed using the Random Forest algorithm of the Decision Tree Ensembles method using the Binning procedure for numeric data, analysis parameters are displayed to the console window, analysis results are loaded to the DM_REPORT_RES report.

See also:

IDmRandomForest