dotnet/machinelearning · error · ArgumentException
{nameof(numberOfCVFolds)} must be at least 2
Error message
{nameof(numberOfCVFolds)} must be at least 2 What it means
UserInputValidationUtil.ValidateNumberOfCVFoldsArg rejects cross-validation fold counts below 2 with ArgumentException naming the parameter — cross-validation requires at least a train/test split, so 0 or 1 folds is meaningless.
Source
Thrown at src/Microsoft.ML.AutoML/Utils/UserInputValidationUtil.cs:57
ValidatePath(path);
}
public static void ValidateInferColumnsArgs(string path, string labelColumn)
{
ValidateLabelColumn(labelColumn);
ValidatePath(path);
}
public static void ValidateInferColumnsArgs(string path)
{
ValidatePath(path);
}
public static void ValidateNumberOfCVFoldsArg(uint numberOfCVFolds)
{
if (numberOfCVFolds <= 1)
{
throw new ArgumentException($"{nameof(numberOfCVFolds)} must be at least 2", nameof(numberOfCVFolds));
}
}
public static void ValidateSamplingKey(string samplingKeyColumnName, string groupIdColumnName, TaskKind task)
{
if (task == TaskKind.Ranking && samplingKeyColumnName != null && samplingKeyColumnName != groupIdColumnName)
{
throw new ArgumentException($"If provided, {nameof(samplingKeyColumnName)} must be the same as {nameof(groupIdColumnName)} for Ranking Experiments", samplingKeyColumnName);
}
}
private static void ValidateTrainData(IDataView trainData, ColumnInformation columnInformation)
{
if (trainData == null)
{
throw new ArgumentNullException(nameof(trainData), "Training data cannot be null");
}
View on GitHub (pinned to 7b76e69cf9)
Solutions
- Pass numberOfCVFolds >= 2 (call ValidateNumberOfCVFoldsArg early to fail fast).
- Clamp user/config-supplied fold values to Math.Max(2, value) before calling the API.
- Use non-cross-validation execution (train/validation split) if only one split is desired.
Example fix
// before experimentSettings.NumberOfCVFolds = 1; // after experimentSettings.NumberOfCVFolds = Math.Max(2, userConfiguredFolds);
Defensive patterns
Strategy: validation
Validate before calling
if (numberOfCVFolds < 2)
throw new ArgumentException(nameof(numberOfCVFolds), "Must be at least 2");
UserInputValidationUtil.ValidateNumberOfCVFoldsArg(numberOfCVFolds); Type guard
bool IsValidCvFolds(uint folds) => folds >= 2;
Try / catch
try { UserInputValidationUtil.ValidateNumberOfCVFoldsArg(folds); }
catch (ArgumentException ex) { folds = 5; logger.LogWarning(ex.Message + "; defaulting to 5"); } Prevention
- Clamp config/UI-supplied fold counts to >= 2
- Call UserInputValidationUtil early in pipelines
- Document that 1 fold is invalid for cross-validation
When it happens
Trigger: Calling an AutoML experiment API (e.g. CreateExperiment/Execute with cross-validation) passing numberOfCVFolds = 0 or 1, typically from user-supplied settings or config defaults.
Common situations: Users configuring fold counts from app settings that default to 0/1; off-by-one when interpreting 'number of validation runs'; UI inputs allowing 1.
Related errors
- Please provide a message with content.
- Expected either {0} or {1} to be provided
- {nameof(ChannelMessageKind)}.{e.Kind} is not yet implemented
- Training failed with the exception: {_history.Last().Excepti
- Metric {typeof(TMetrics)} not implemented
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/0aa7e8d4e3891d77.
Report an issue: GitHub.