TheAlgorithms/C-Sharp · error · ArgumentException
Input features cannot be empty.
Error message
Input features cannot be empty.
What it means
LogisticRegression.Fit validates its training input and throws ArgumentException when the feature matrix x is null-length or has zero columns. Training on an empty matrix would produce meaningless weights (nFeatures = x[0].Length would also fail), so the method rejects it up front.
Solutions
- Ensure the training set contains at least one sample with at least one feature before calling Fit.
- Guard the caller: skip training or surface a domain-specific message when the dataset is empty.
- Fix the upstream data-loading/filtering step that produced an empty matrix.
Example fix
// before
model.Fit(new double[0][], new int[0]); // throws
// after
if (x.Length > 0 && x[0].Length > 0)
{
model.Fit(x, y);
} Defensive patterns
Strategy: validation
Validate before calling
if (x == null || x.Length == 0 || x[0].Length == 0)
{
throw new ArgumentException("Training features must contain at least one sample with one feature.");
}
model.Fit(x, y); Type guard
static bool HasSamples(double[][] x) => x != null && x.Length > 0 && x[0] != null && x[0].Length > 0;
Try / catch
try
{
model.Fit(x, y);
}
catch (ArgumentException ex) when (ex.Message == "Input features cannot be empty.")
{
logger.LogWarning("Skipping training: empty dataset.");
} Prevention
- Validate dataset size right after loading, before any model code runs.
- Log dataset dimensions in training pipelines so empty inputs are obvious.
- Add integration tests covering empty-input paths in the data pipeline.
When it happens
Trigger: Calling Fit(double[][] x, int[] y, ...) with an empty array (x.Length == 0) or with rows of zero length (x[0].Length == 0).
Common situations: Upstream data pipeline returned no rows (empty CSV, filtered-out dataset); a reshape/split step produced an empty feature matrix; a deserialization bug yielding an empty array.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- k must be at least 1.
- Number of samples and labels must match.
- Feature count mismatch.
- message
- key must be non-empty string
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/dd782173da7e8225.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/MachineLearning/LogisticRegression.cs:27
public class LogisticRegression
{
private double[] weights = [];
private double bias;
public int FeatureCount => weights.Length;
/// <summary>
/// Fit the model using gradient descent.
/// </summary>
/// <param name="x">2D array of features (samples x features).</param>
/// <param name="y">Array of labels (0 or 1).</param>
/// <param name="epochs">Number of iterations.</param>
/// <param name="learningRate">Step size.</param>
public void Fit(double[][] x, int[] y, int epochs = 1000, double learningRate = 0.01)
{
if (x.Length == 0 || x[0].Length == 0)
{
throw new ArgumentException("Input features cannot be empty.");
}
if (x.Length != y.Length)
{
throw new ArgumentException("Number of samples and labels must match.");
}
int nSamples = x.Length;
int nFeatures = x[0].Length;
weights = new double[nFeatures];
bias = 0;
for (int epoch = 0; epoch < epochs; epoch++)
{
double[] dw = new double[nFeatures];
double db = 0;
for (int i = 0; i < nSamples; i++)
{View on GitHub (pinned to 96e2905cab)