NumberOfFolds: Integer;
The NumberOfFolds property determines the number of folds.
The property is taken into account if the K-fold cross-validation is used, that is, the IDmMethodCrossValidation.SamplingType property is set to CrossValidationSamplingType.Kfold.
Range of available values: [1; number of observations].
The default value: 5.
Executing the example requires that the repository contains a table with the DM_TABLE identifier containing data for analysis.
Add links to the Metabase, Ms, Stat system assemblies.
Sub UserProc;
Var
MB: IMetabase;
ReportDS: IDmReportDataSource;
TableDS: IDmTableDataSource;
Method: IDmMethod;
DM: IDmLogisticRegression;
i, j: Integer;
Attrs: Array Of Integer;
CrossValidation: IDmMethodCrossValidation;
Reports: IDmReports;
DmReport: IDmReport;
CrossValPerf: ICrossValidationPerformanceScores;
CategoriesList, KfoldIntervals: Array Of Integer;
PerformanceMatrix: Array Of Double;
Begin
MB := MetabaseClass.Active;
// Create calculation method
Method := (New DataMiningMethod.Create) As IDmMethod;
// Specify method type
Method.Kind := DmMethodKind.LogisticRegression;
// Create table data source
TableDS := (New TableDataSource.Create) As IDmTableDataSource;
// Determine source table
TableDS.Table := MB.ItemByID("DM_TABLE").Bind;
// Set input data source
Method.InputDataSource := TableDS;
// Create a data source that is a regular report
ReportDS := (New ReportDataSource.Create) As IDmReportDataSource;
// Set data consumer
Method.OutputDataSource := ReportDS;
// Set up calculation method parameters
DM := Method.Details As IDmLogisticRegression;
// Set factors impacting analyzed attribute
Attrs := New Integer[TableDS.FieldCount - 1];
For i := 0 To Attrs.Length - 1 Do
Attrs[i] := i + 1;
End For;
DM.Attributes := Attrs;
// Specify analyzed object
DM.Target := ReportDS.FieldCount;
// Set threshold
DM.Threshold := 0.57;
// Set up cross-validation parameters
CrossValidation := DM.CrossValidation;
// Specify that cross-validation is used
CrossValidation.Active := True;
// Set cross-validation method: K -fold cross-validation
CrossValidation.SamplingType := CrossValidationSamplingType.Kfold;
// Set number of folds
CrossValidation.NumberOfFolds := 4;
// Perform analysis and output results
Reports := Method.Execute;
// Display cross-validation results
DmReport := Reports.FindByType(DmReportType.CrossValidation);
DmReport.Generate;
CrossValPerf := CrossValidation.Results;
Debug.WriteLine("Cross-validation results:");
Debug.Indent;
Debug.WriteLine("Analyzed attribute: " + CrossValPerf.ClassificatorName);
Debug.Write("Number of factors influencing the analyzed attribute: ");
Debug.WriteLine(CrossValPerf.FactorsNumber);
Debug.WriteLine("Number of observations: " + CrossValPerf.ObservationsNumber.ToString);
Debug.WriteLine("Number of repetitions: " + CrossValidation.NumberOfRandomTests.ToString);
Debug.WriteLine("Accuracy of classification: " + CrossValPerf.ClassificationAccuracy.ToString);
Debug.WriteLine("Categories:");
Debug.Indent;
CategoriesList := CrossValPerf.CategoriesList;
For i := 0 To CategoriesList.Length - 1 Do
Debug.WriteLine(CategoriesList[i]);
End For;
Debug.Unindent;
Debug.WriteLine("Fold intervals:");
Debug.Indent;
KfoldIntervals := CrossValPerf.KfoldIntervals;
For i := 0 To KfoldIntervals.Length - 1 Do
Debug.WriteLine(KfoldIntervals[i]);
End For;
Debug.Unindent;
Debug.WriteLine("Correct classification:");
Debug.Indent;
PerformanceMatrix := CrossValPerf.PerformanceMatrix;
For i := 0 To PerformanceMatrix.GetUpperBound(1) Do
For j := 0 To PerformanceMatrix.GetUpperBound(2) Do
Debug.Write(PerformanceMatrix[i, j].ToString + #9);
End For;
Debug.WriteLine("");
End For;
Debug.Unindent;
Debug.Unindent;
End Sub UserProc;
After executing the example cross-validation is executed, its results are displayed in the console window.
See also: