NumberOfFolds: Integer;
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 containing data for analysis with the DM_TABLE identifier.
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.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
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, PerformanceMatrix: System.Array;
Begin
MB := Params.Metabase;
// Create calculation method
Method := (New DataMiningMethod.Create()) As IDmMethod;
// Specify method type
Method.Kind := DmMethodKind.dmmkLogisticRegression;
// 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.cvstKfold;
// Set number of folds
CrossValidation.NumberOfFolds := 4;
// Perform analysis and output results
Reports := Method.Execute();
// Display cross-validation results
DmReport := Reports.FindByType[DmReportType.drtCrossValidation];
DmReport.Generate();
CrossValPerf := CrossValidation.Results;
System.Diagnostics.Debug.WriteLine("Cross-validation results:");
System.Diagnostics.Debug.Indent();
System.Diagnostics.Debug.WriteLine("Analyzed attribute: " + CrossValPerf.ClassificatorName);
System.Diagnostics.Debug.Write("Number of factors influencing the analyzed attribute: ");
System.Diagnostics.Debug.WriteLine(CrossValPerf.FactorsNumber);
System.Diagnostics.Debug.WriteLine("Number of observations: " + CrossValPerf.ObservationsNumber.ToString());
System.Diagnostics.Debug.WriteLine("Number of repetitions: " + CrossValidation.NumberOfRandomTests.ToString());
System.Diagnostics.Debug.WriteLine("Accuracy of classification: " + CrossValPerf.ClassificationAccuracy.ToString());
System.Diagnostics.Debug.WriteLine("Categories:");
System.Diagnostics.Debug.Indent();
CategoriesList := CrossValPerf.CategoriesList;
For i := 0 To CategoriesList.Length - 1 Do
System.Diagnostics.Debug.WriteLine(CategoriesList[i]);
End For;
System.Diagnostics.Debug.Unindent();
System.Diagnostics.Debug.WriteLine("Fold intervals:");
System.Diagnostics.Debug.Indent();
KfoldIntervals := CrossValPerf.KfoldIntervals;
For i := 0 To KfoldIntervals.Length - 1 Do
System.Diagnostics.Debug.WriteLine(KfoldIntervals[i]);
End For;
System.Diagnostics.Debug.Unindent();
System.Diagnostics.Debug.WriteLine("Correct classification:");
System.Diagnostics.Debug.Indent();
PerformanceMatrix := CrossValPerf.PerformanceMatrix;
For i := 0 To PerformanceMatrix.GetUpperBound(1) Do
For j := 0 To PerformanceMatrix.GetUpperBound(0) Do
System.Diagnostics.Debug.Write(PerformanceMatrix[j, i].ToString() + char.ConvertFromUtf32(9));
End For;
System.Diagnostics.Debug.WriteLine("");
End For;
System.Diagnostics.Debug.Unindent();
System.Diagnostics.Debug.Unindent();
End Sub;
See also: