microsoft/qlib · error · ValueError

model is not fitted yet!

Error message

model is not fitted yet!

What it means

Thrown by CatBoostModel.predict when self.model is None, i.e. predict is called before fit ever ran. The model attribute only gets a CatBoost instance inside fit, so predicting from a freshly constructed (or failed-to-fit) CatBoostModel is rejected.

Source

Thrown at qlib/contrib/model/catboost_model.py:82

        valid_pool = Pool(data=x_valid, label=y_valid_1d, weight=w_valid)

        # Initialize the catboost model
        self._params["iterations"] = num_boost_round
        self._params["early_stopping_rounds"] = early_stopping_rounds
        self._params["verbose_eval"] = verbose_eval
        self._params["task_type"] = "GPU" if get_gpu_device_count() > 0 else "CPU"
        self.model = CatBoost(self._params, **kwargs)

        # train the model
        self.model.fit(train_pool, eval_set=valid_pool, use_best_model=True, **kwargs)

        evals_result = self.model.get_evals_result()
        evals_result["train"] = list(evals_result["learn"].values())[0]
        evals_result["valid"] = list(evals_result["validation"].values())[0]

    def predict(self, dataset: DatasetH, segment: Union[Text, slice] = "test"):
        if self.model is None:
            raise ValueError("model is not fitted yet!")
        x_test = dataset.prepare(segment, col_set="feature", data_key=DataHandlerLP.DK_I)
        return pd.Series(self.model.predict(x_test.values), index=x_test.index)

    def get_feature_importance(self, *args, **kwargs) -> pd.Series:
        """get feature importance

        Notes
        -----
            parameters references:
            https://catboost.ai/docs/concepts/python-reference_catboost_get_feature_importance.html#python-reference_catboost_get_feature_importance
        """
        return pd.Series(
            data=self.model.get_feature_importance(*args, **kwargs), index=self.model.feature_names_
        ).sort_values(ascending=False)


if __name__ == "__main__":
    cat = CatBoostModel()

View on GitHub (pinned to 79633dd950)

Solutions

  1. Call model.fit(dataset) before model.predict(dataset)
  2. If fit previously failed, fix the underlying fit error (often 'Empty data from dataset') before predicting
  3. When loading a dumped model, ensure you restore the fitted object, not a fresh instance

Example fix

# before
model = CatBoostModel()
pred = model.predict(dataset)  # ValueError: model is not fitted yet!

# after
model = CatBoostModel()
model.fit(dataset)
pred = model.predict(dataset)
Defensive patterns

Strategy: validation

Validate before calling

assert model.model is not None, "fit() must run before predict()"

Prevention

When it happens

Trigger: Instantiating CatBoostModel and calling predict(dataset) directly; a fit call that raised earlier (e.g. empty data) leaving self.model unset, followed by predict in a finally/except block; serializing/deserializing incorrectly so model is lost.

Common situations: Running a backtest/workflow where the model section was skipped; an exception in fit being swallowed and the pipeline continuing to the prediction stage.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/9d52fc95fd56a2f0. Report an issue: GitHub.