cancel
Showing results for 
Search instead for 
Did you mean: 
Machine Learning
cancel
Showing results for 
Search instead for 
Did you mean: 

MLFlow is throwing error for the shape of input

DebK
New Contributor III

I am running the code for prediction which will take the model from mlflow deployment. Code I have copied from the example given by mlflow experiment tab.

import mlflow
logged_model = 'runs:/id/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))

Here I am getting the error on this line

loaded_model.predict(pd.DataFrame(data))

MlflowException: The input pandas data frame column 'None' contains scalar values, which requires the shape to be (-1,), but got tensor spec shape of (-1, "Some big integer").

Can anyone help me how to resolve this error?

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @Koushik Deb​  try this your problem will be resolve now. if it works don't forget to accept and upvote the answer 🙂

import mlflow
 
import pandas as pd
 
 
 
# Load model as a PyFuncModel.
 
logged_model = 'runs:/id/model'
 
loaded_model = mlflow.pyfunc.load_model(logged_model)
 
 
 
# Create a dataframe with a single column
 
data = pd.DataFrame({'text': ["visit www.bet365.com for a free trial"]})
 
 
 
# Call predict method with the dataframe
 
predictions = loaded_model.predict(data)

View solution in original post

6 REPLIES 6

Tayyab_Vohra
Contributor

You can try this out, I don't know the shape of the dataframe it would be helpful if you would also print the shape of the dataframe , based on my assumption and the input you provided try this code.

import mlflow
import pandas as pd
 
# Load model as a PyFuncModel.
logged_model = 'runs:/id/model'
loaded_model = mlflow.pyfunc.load_model(logged_model)
 
# Create a list from the pandas DataFrame
data_list = data.values.tolist()
 
# Predict using the loaded model
predictions = loaded_model.predict(data_list)
 
# Print the predictions
print(predictions)

DebK
New Contributor III

I have tried this one.

I am providing data as a list of single element.

data = ["visit www.bet365.com for a free trial"]

Tried with

predictions = loaded_model.predict(data)

and

predictions = loaded_model.predict(pd.DataFrame(data))

Hi @Koushik Deb​  try this your problem will be resolve now. if it works don't forget to accept and upvote the answer 🙂

import mlflow
 
import pandas as pd
 
 
 
# Load model as a PyFuncModel.
 
logged_model = 'runs:/id/model'
 
loaded_model = mlflow.pyfunc.load_model(logged_model)
 
 
 
# Create a dataframe with a single column
 
data = pd.DataFrame({'text': ["visit www.bet365.com for a free trial"]})
 
 
 
# Call predict method with the dataframe
 
predictions = loaded_model.predict(data)

DebK
New Contributor III

After executing the given code by you, I have got this error

MlflowException: The input pandas dataframe column 'None' contains scalar values, which requires the shape to be (-1,), but got tensor spec shape of (-1, 14654).

Let me know if you have any idea on this.

please print actual shape of your dataframe before passing it to the .predict function, the list you have provided , I have created a code from that its working fine.

you are not showing the actual shape of dataframe, there is problem with the shape of the data that you are passing.

Anonymous
Not applicable

Hi @Koushik Deb​ 

Thank you for posting your question in our community! We are happy to assist you.

To help us provide you with the most accurate information, could you please take a moment to review the responses and select the one that best answers your question?

This will also help other community members who may have similar questions in the future. Thank you for your participation and let us know if you need any further assistance! 

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.