Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fastai] 'DataFrame' object has no attribute 'to_frame' with fastai #523

Open
ming-cui opened this issue Aug 1, 2022 · 12 comments
Open
Labels
feature 💡 New feature or enhancement request Python 🐍 Related to Python question ❔ Further information is requested

Comments

@ming-cui
Copy link

ming-cui commented Aug 1, 2022

I'm trying to wrap a fastai tabular learner with DALEX. I got 'DataFrame' object has no attribute 'to_frame' error with dx.Explainer(learn, xs, y, label = "Deep NN"). Any potential problems with this line of code? Thanks!

@hbaniecki hbaniecki added question ❔ Further information is requested Python 🐍 Related to Python labels Aug 1, 2022
@hbaniecki
Copy link
Member

What is the verbose output from the Explainer?

image

@ming-cui
Copy link
Author

ming-cui commented Aug 2, 2022

Please see the screenshot.
Snipaste_2022-08-02_10-21-26

@hbaniecki
Copy link
Member

hbaniecki commented Aug 2, 2022

Thanks, it is required to pass predict_function to the Explainer, which inputs (model, pandas.DataFrame) and outputs a 1d numpy array.

An example for the xgboost package is available at https://dalex.drwhy.ai/python-dalex-xgboost.html.

The already implemented predict_functions (e.g. for h2o, pycaret, tensorflow) are available at https://github.com/ModelOriented/DALEX/blob/master/python/dalex/dalex/_explainer/yhat.py.

We could add native support for fastai to dalex -- feel free to initiate a PR with changes to the yhat.py file, and I would try to make it happen.

@hbaniecki hbaniecki added the feature 💡 New feature or enhancement request label Aug 2, 2022
@ming-cui
Copy link
Author

ming-cui commented Aug 2, 2022

Thanks, Hubert. fastai .predict method returns three objects. The source code is
Snipaste_2022-08-02_21-11-34 Is it not compatible with DALEX? I'm still getting errors, and the messages were lengthy.

@hbaniecki
Copy link
Member

hbaniecki commented Aug 2, 2022

Yes, as I said, you need to create a new function, which will use the predict method and return only one object -- a 1-dimensional numpy array with predictions for a class of interest. Then pass it to the predict_function parameter in dx.Explainer

@ming-cui
Copy link
Author

ming-cui commented Aug 3, 2022

Hi Hubert, I think I got the basic idea, but I struggled for quite a while as I'm new to Python and fastai. I got the error as illustrated in the screenshot. It seems the .predict function only takes a row as input. I guess I need to write some customized prediction function to pass into DALEX. I've linked the source code of .predict here, or you can view the code in the sceenshot. Could you help with getting the right prediction function to work with DALEX? Thank you so much!
Error prediction function

.predict source code

@hbaniecki
Copy link
Member

Hi! Maybe you can try predict_function = lambda m, d: m.predict(d)[2]?

@ming-cui
Copy link
Author

ming-cui commented Aug 3, 2022

Thanks for the function. I got the error message 'DataFrame' object has no attribute 'to_frame' with details below. Will the .get_preds() method be useful (shown in the photo in my last reply)? .get_preds() returns tuples (one for predictions and the other for true labels), and I haven't figured out how to put it in a explainer.
Snipaste_2022-08-03_20-59-07
.

@ming-cui
Copy link
Author

ming-cui commented Aug 3, 2022

Is it possible that the tensor format predictions returned by .predict() caused 'DataFrame' object has no attribute 'to_frame'?

@hbaniecki
Copy link
Member

Yes, you need to first apply the predict_function outside of the explainer and see what it returns.

@ming-cui
Copy link
Author

ming-cui commented Aug 3, 2022

It returns a tensor. For example, learn.predict(xs_nn.iloc[0])[2] will return tensor([1.7050]) as the predicted value based on the first row of the training dataframe xs_nn. What should I do with that? Many thanks!

@hbaniecki
Copy link
Member

It should return a one dimensional numpy array

@hbaniecki hbaniecki changed the title 'DataFrame' object has no attribute 'to_frame' with fastai [fastai] 'DataFrame' object has no attribute 'to_frame' with fastai Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 💡 New feature or enhancement request Python 🐍 Related to Python question ❔ Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants