{"record":{"id":"dd782173da7e8225","repo":"TheAlgorithms/C-Sharp","slug":"input-features-cannot-be-empty","errorCode":null,"errorMessage":"Input features cannot be empty.","messagePattern":"Input features cannot be empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/MachineLearning/LogisticRegression.cs","lineNumber":27,"sourceCode":"public class LogisticRegression\n{\n    private double[] weights = [];\n    private double bias;\n\n    public int FeatureCount => weights.Length;\n\n    /// <summary>\n    /// Fit the model using gradient descent.\n    /// </summary>\n    /// <param name=\"x\">2D array of features (samples x features).</param>\n    /// <param name=\"y\">Array of labels (0 or 1).</param>\n    /// <param name=\"epochs\">Number of iterations.</param>\n    /// <param name=\"learningRate\">Step size.</param>\n    public void Fit(double[][] x, int[] y, int epochs = 1000, double learningRate = 0.01)\n    {\n        if (x.Length == 0 || x[0].Length == 0)\n        {\n            throw new ArgumentException(\"Input features cannot be empty.\");\n        }\n\n        if (x.Length != y.Length)\n        {\n            throw new ArgumentException(\"Number of samples and labels must match.\");\n        }\n\n        int nSamples = x.Length;\n        int nFeatures = x[0].Length;\n        weights = new double[nFeatures];\n        bias = 0;\n\n        for (int epoch = 0; epoch < epochs; epoch++)\n        {\n            double[] dw = new double[nFeatures];\n            double db = 0;\n            for (int i = 0; i < nSamples; i++)\n            {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/MachineLearning/LogisticRegression.cs#L9-L45","documentation":"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.","triggerScenarios":"Calling Fit(double[][] x, int[] y, ...) with an empty array (x.Length == 0) or with rows of zero length (x[0].Length == 0).","commonSituations":"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.","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."],"exampleFix":"// before\nmodel.Fit(new double[0][], new int[0]); // throws\n\n// after\nif (x.Length > 0 && x[0].Length > 0)\n{\n    model.Fit(x, y);\n}","handlingStrategy":"validation","validationCode":"if (x == null || x.Length == 0 || x[0].Length == 0)\n{\n    throw new ArgumentException(\"Training features must contain at least one sample with one feature.\");\n}\nmodel.Fit(x, y);","typeGuard":"static bool HasSamples(double[][] x) => x != null && x.Length > 0 && x[0] != null && x[0].Length > 0;","tryCatchPattern":"try\n{\n    model.Fit(x, y);\n}\ncatch (ArgumentException ex) when (ex.Message == \"Input features cannot be empty.\")\n{\n    logger.LogWarning(\"Skipping training: empty dataset.\");\n}","preventionTips":["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."],"tags":["machine-learning","validation","csharp"],"backgroundTag":"empty-required-field","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}