{"record":{"id":"654ca7e816a4d1dd","repo":"TheAlgorithms/Python","slug":"predictor-hasn-t-been-fit-yet","errorCode":null,"errorMessage":"Predictor hasn't been fit yet","messagePattern":"Predictor hasn't been fit yet","errorType":"exception","errorClass":"ArithmeticError","httpStatus":null,"severity":"error","filePath":"machine_learning/polynomial_regression.py","lineNumber":178,"sourceCode":"                                    parameters are fit\n\n        >>> x = np.array([0, 1, 2, 3, 4])\n        >>> y = x**3 - 2 * x**2 + 3 * x - 5\n        >>> poly_reg = PolynomialRegression(degree=3)\n        >>> poly_reg.fit(x, y)\n        >>> poly_reg.predict(np.array([-1]))\n        array([-11.])\n        >>> poly_reg.predict(np.array([-2]))\n        array([-27.])\n        >>> poly_reg.predict(np.array([6]))\n        array([157.])\n        >>> PolynomialRegression(degree=3).predict(x)\n        Traceback (most recent call last):\n        ...\n        ArithmeticError: Predictor hasn't been fit yet\n        \"\"\"\n        if self.params is None:\n            raise ArithmeticError(\"Predictor hasn't been fit yet\")\n\n        return PolynomialRegression._design_matrix(data, self.degree) @ self.params\n\n\ndef main() -> None:\n    \"\"\"\n    Fit a polynomial regression model to predict fuel efficiency using seaborn's mpg\n    dataset\n\n    >>> pass    # Placeholder, function is only for demo purposes\n    \"\"\"\n    import seaborn as sns\n\n    mpg_data = sns.load_dataset(\"mpg\")\n\n    poly_reg = PolynomialRegression(degree=2)\n    poly_reg.fit(mpg_data.weight, mpg_data.mpg)\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/polynomial_regression.py#L160-L196","documentation":"Raised as ArithmeticError by PolynomialRegression.predict when self.params is None, i.e. fit has not been called on this instance yet. Prediction evaluates design_matrix(data) @ params, which is impossible without fitted coefficients; the class deliberately fails fast instead of returning garbage.","triggerScenarios":"Constructing PolynomialRegression(degree=3) and immediately calling predict(x); calling predict in a fresh process after forgetting to persist/reload fitted params; re-instantiating the model inside a loop and predicting before refitting.","commonSituations":"Skipping the fit step in demos/tests; pickling the unfitted object; control flow where fit is conditional but predict is unconditional.","solutions":["Call model.fit(x_train, y_train) before model.predict(x).","Check model.params is not None (or hasattr been fitted) before predicting.","When loading saved models, persist params and restore them (model.params = loaded_params) before predict."],"exampleFix":"# before\nmodel = PolynomialRegression(degree=3)\ny_hat = model.predict(x)\n\n# after\nmodel = PolynomialRegression(degree=3)\nmodel.fit(x_train, y_train)\ny_hat = model.predict(x)","handlingStrategy":"validation","validationCode":"if model.params is None:\n    model.fit(x_train, y_train)\ny_hat = model.predict(x_new)","typeGuard":"def is_fitted(model: PolynomialRegression) -> bool:\n    return getattr(model, \"params\", None) is not None","tryCatchPattern":"try:\n    y_hat = model.predict(x_new)\nexcept ArithmeticError as e:\n    if \"hasn't been fit\" in str(e):\n        model.fit(x_train, y_train)\n        y_hat = model.predict(x_new)\n    else:\n        raise","preventionTips":["Make fit a mandatory step in the pipeline before any predict call.","Check model.params is not None as a cheap fitted-state guard.","When persisting models, save and restore params alongside degree."],"tags":["machine-learning","regression","lifecycle","state-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}