TheAlgorithms/C-Sharp · error · ArgumentException

Input data cannot be empty.

Error message

Input data cannot be empty.

What it means

Fit validates its inputs before fitting: null x or y lists, empty lists, or mismatched lengths make regression impossible (zero/n undefined variance), so a generic ArgumentException is thrown; this record fires for the null-list guard.

Solutions

  1. Ensure at least one (x, y) observation pair exists before calling Fit.
  2. After loading/filtering data, check Count > 0 and surface a clear message instead of fitting.
  3. Fix the loading/filtering logic that removed all observations.

Example fix

// before
var xs = rows.Where(r => r.X > threshold).Select(r => r.X).ToList();
regression.Fit(xs, ys); // may be empty
// after
if (xs.Count == 0) throw new InvalidOperationException("No data after filtering.");
regression.Fit(xs, ys);
Defensive patterns

Strategy: validation

Validate before calling

if (xs.Count == 0 || ys.Count == 0)
    throw new InvalidOperationException("Cannot fit: dataset has no observations.");
regression.Fit(xs, ys);

Try / catch

try
{
    regression.Fit(xs, ys);
}
catch (ArgumentException ex) when (ex.Message.Contains("empty"))
{
    // no observations after loading/filtering
    logger.LogError(ex, "Empty dataset supplied to Fit");
}

Prevention

When it happens

Trigger: Calling Fit with x.Count == 0 or y.Count == 0 — e.g. after filtering removed all rows or an empty file was parsed into empty lists.

Common situations: Empty CSV or query returning no rows; over-aggressive filtering (e.g. removing NaNs) leaving no data; passing new List<double>() placeholders during development.

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


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/e4dc6326241a7851. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/MachineLearning/LinearRegression.cs:37

    public bool IsFitted { get; private set; }

    /// <summary>
    /// Fits the linear regression model to the provided data.
    /// </summary>
    /// <param name="x">List of independent variable values.</param>
    /// <param name="y">List of dependent variable values.</param>
    /// <exception cref="ArgumentException">Thrown if input lists are null, empty, or of different lengths.</exception>
    public void Fit(IList<double> x, IList<double> y)
    {
        if (x == null || y == null)
        {
            throw new ArgumentException("Input data cannot be null.");
        }

        if (x.Count == 0 || y.Count == 0)
        {
            throw new ArgumentException("Input data cannot be empty.");
        }

        if (x.Count != y.Count)
        {
            throw new ArgumentException("Input lists must have the same length.");
        }

        // Calculate means
        double xMean = x.Average();
        double yMean = y.Average();

        // Calculate slope (b) and intercept (a)
        double numerator = 0.0;
        double denominator = 0.0;
        for (int i = 0; i < x.Count; i++)
        {
            numerator += (x[i] - xMean) * (y[i] - yMean);
            denominator += (x[i] - xMean) * (x[i] - xMean);

View on GitHub (pinned to 96e2905cab)