cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get probability score for each prediction from mlflow

Zoumana
New Contributor II

I trained my model and was able to get the batch prediction from that model as specified below. But I want to also get the probability scores for each prediction. Do you have any idea?

Thank you!

logged_model = path_to_model

# Load model as a PyFuncModel.

loaded_model = mlflow.pyfunc.load_model(logged_model)

# Predict on a Pandas DataFrame.

import pandas as pd

loaded_model.predict(pd.DataFrame(data))

1 ACCEPTED SOLUTION

Accepted Solutions

SyedGhouri
New Contributor III

Hi @Kaniz Fatma​ 

The error said 'PyFuncModel' object has no attribute 'predict_proba'.

As shows above, I was using the following to load the model

loaded_model = mlflow.pyfunc.load_model(logged_model) and got the error.

After going through mlflow documentation, I changed it to

loaded_model = mlflow.sklearn.load_model(logged_model) and it is working fine.

It's all good now. Thanks for your time.

Syed

View solution in original post

10 REPLIES 10

Kaniz
Community Manager
Community Manager

Hi @ Zoumana! My name is Kaniz, and I'm the technical moderator here. Great to meet you, and thanks for your question! Let's see if your peers in the community have an answer to your question first. Or else I will get back to you soon. Thanks.

Zoumana
New Contributor II

Hi Kaniz,

Great to meet you too!

Thank you for replying to my question.

Best!

Kaniz
Community Manager
Community Manager

Hi @Zoumana Keita​ , Please refer to this doc here:-

For most models in sci-kit-learn, we can get the probability estimates for the classes through predict_proba.

https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklea...

SyedGhouri
New Contributor III

Hi @Kaniz Fatma​ 

Sorry for hijacking the post.

My question is - if I am reading a registered model from mlflow, I can only see the option of .predict method but not .predict_proba.

Do we have any straightforward solution to get the probabilities?

Thanks

Syed

Kaniz
Community Manager
Community Manager

Hi @Syed Ghouri​, Can you paste the error stack here?

@Syed Ghouri​, Also, I would suggest you go through this S.O thread. Please LMK how it goes.

SyedGhouri
New Contributor III

Hi @Kaniz Fatma​ 

The error said 'PyFuncModel' object has no attribute 'predict_proba'.

As shows above, I was using the following to load the model

loaded_model = mlflow.pyfunc.load_model(logged_model) and got the error.

After going through mlflow documentation, I changed it to

loaded_model = mlflow.sklearn.load_model(logged_model) and it is working fine.

It's all good now. Thanks for your time.

Syed

Kaniz
Community Manager
Community Manager

Hi @Syed Ghouri​ ​, It would mean a lot if you could select the "Best Answer" to help others find the correct answer faster.

This makes that answer appear right after the question, so it's easier to find within a thread.

It also helps us mark the question as answered so we can have more eyes helping others with unanswered questions.

Can I count on you?

SyedGhouri
New Contributor III

Hi @Kaniz Fatma​ 

I do not see the option to select "Best Answer" but feel free to do anything that you think can help this community.

Thanks

Syed

Kaniz
Community Manager
Community Manager

Thank you for your response @Syed Ghouri​ !

I've selected the "Best Answer" on your behalf.

OndrejHavlicek
New Contributor III

Now you can log the model using this parameter:

mlflow.sklearn.log_model(
    ...,  # the usual params
    pyfunc_predict_fn="predict_proba"
)

 which will return probabilities for the first class apparently when using the model for inference (e.g. when loading it using mlflow.pyfunc.spark_udf() ).

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.