TheAlgorithms/Python · error · ArithmeticError

Predictor hasn't been fit yet

Error message

Predictor hasn't been fit yet

What it means

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.

Source

Thrown at machine_learning/polynomial_regression.py:178

                                    parameters are fit

        >>> x = np.array([0, 1, 2, 3, 4])
        >>> y = x**3 - 2 * x**2 + 3 * x - 5
        >>> poly_reg = PolynomialRegression(degree=3)
        >>> poly_reg.fit(x, y)
        >>> poly_reg.predict(np.array([-1]))
        array([-11.])
        >>> poly_reg.predict(np.array([-2]))
        array([-27.])
        >>> poly_reg.predict(np.array([6]))
        array([157.])
        >>> PolynomialRegression(degree=3).predict(x)
        Traceback (most recent call last):
        ...
        ArithmeticError: Predictor hasn't been fit yet
        """
        if self.params is None:
            raise ArithmeticError("Predictor hasn't been fit yet")

        return PolynomialRegression._design_matrix(data, self.degree) @ self.params


def main() -> None:
    """
    Fit a polynomial regression model to predict fuel efficiency using seaborn's mpg
    dataset

    >>> pass    # Placeholder, function is only for demo purposes
    """
    import seaborn as sns

    mpg_data = sns.load_dataset("mpg")

    poly_reg = PolynomialRegression(degree=2)
    poly_reg.fit(mpg_data.weight, mpg_data.mpg)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Call model.fit(x_train, y_train) before model.predict(x).
  2. Check model.params is not None (or hasattr been fitted) before predicting.
  3. When loading saved models, persist params and restore them (model.params = loaded_params) before predict.

Example fix

# before
model = PolynomialRegression(degree=3)
y_hat = model.predict(x)

# after
model = PolynomialRegression(degree=3)
model.fit(x_train, y_train)
y_hat = model.predict(x)
Defensive patterns

Strategy: validation

Validate before calling

if model.params is None:
    model.fit(x_train, y_train)
y_hat = model.predict(x_new)

Type guard

def is_fitted(model: PolynomialRegression) -> bool:
    return getattr(model, "params", None) is not None

Try / catch

try:
    y_hat = model.predict(x_new)
except ArithmeticError as e:
    if "hasn't been fit" in str(e):
        model.fit(x_train, y_train)
        y_hat = model.predict(x_new)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Skipping the fit step in demos/tests; pickling the unfitted object; control flow where fit is conditional but predict is unconditional.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/654ca7e816a4d1dd. Report an issue: GitHub.