marcotcr / lime

Lime: Explaining the predictions of any machine learning classifier
BSD 2-Clause "Simplified" License
11.4k stars 1.79k forks source link

Possible To Add Support for AutoGluon Models? #699

Closed Alex-Wenner-FHR closed 1 year ago

Alex-Wenner-FHR commented 1 year ago

This may exist today, but I cannot figure out how to do it.

It appears that lime wants a 1d array as the data_row parameter in the .explain_instance method. However AutoGluon Predictor Fns appear to only support their type of TabularDataset or that of a pd.DataFrame.

Both AutoGluon & Lime are great packages and it would be awesome if these two projects played nice and were able to be used together. Let me know if I am missing something or if this is supported today and I just am unable to figure it out. Thanks!

Alex-Wenner-FHR commented 1 year ago

Was Able to solve this with a snippit of Autogluon code that wraps their predictor ... See below:

class AutogluonWrapper:
    def __init__(self, predictor, feature_names, target_class=None):
        self.ag_model = predictor
        self.feature_names = feature_names
        self.target_class = target_class

    def predict_proba(self, X):
        if isinstance(X, pd.Series):
            X = X.values.reshape(1,-1)
        if not isinstance(X, pd.DataFrame):
            X = pd.DataFrame(X, columns=self.feature_names)
        preds = self.ag_model.predict_proba(X)
        if predictor.problem_type == "regression" or self.target_class is None:
            return preds
        else:
            return preds.to_numpy()