Skip to content

predict method on DecisionTreeRegressor panics when tree is not fit #469

Description

@slievens

The current code for predict is this:

pub fn predict(&self, x: &X) -> Result<Y, Failed> {
        self.tree_regressor.as_ref().unwrap().predict(x)
    }

this will panic when tree_regressor is still None (when fit wasn't called prior to predict).

Propose to change the implementation to:

pub fn predict(&self, x: &X) -> Result<Y, Failed> {
        match &self.tree_regressor {
            Some(tree) => tree.predict(x),
            None => Err(Failed::predict(
                "'fit' should be called before calling 'predict'",
            )),
        }
    }

Similar changes can be made for many other classifiers.

If you want, I can create a PR for this (and all classifiers/regressors that have the same or similar issue).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions