cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Machine Learning
Dive into the world of machine learning on the Databricks platform. Explore discussions on algorithms, model training, deployment, and more. Connect with ML enthusiasts and experts.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

XGBModel' object has no attribute 'feature_types'

MichaelO
New Contributor III

I saved an xgboost boost model in filetstore as a pkl file.

I call the model by the commands below

model = pickle.load(open('/.../model.pkl', 'rb'))

model.predict_proba(df[features])

The model has been running for sometime with the above commands but I now get an error saying

'XGBModel' object has no attribute 'feature_types'

I tried specifying the version of the xgboost library installation but that does not work.

What could be the issue?

3 REPLIES 3

Anonymous
Not applicable

@Michael Okelolaโ€‹ :

The error message 'XGBModel' object has no attribute 'feature_types' suggests that the XGBModel object does not have the attribute feature_types. This could happen if the model object was saved with an older version of XGBoost that did not include the feature_types attribute or if the model object was saved with a different library or version that did not have this attribute.

One possible solution is to retrain the XGBoost model with the latest version of XGBoost library and ensure that the feature_types attribute is included. Alternatively, you can try to investigate how the

XGBModel object was created and saved in the first place to determine if there were any issues with the saving process that might have caused the missing attribute.

Another option is to use the get_booster() method of the XGBModel object to obtain the underlying XGBoost Booster object, and then call the set_feature_types() method of the Booster object to set the feature types manually. Here's an example:

import xgboost as xgb
 
model = pickle.load(open('/.../model.pkl', 'rb'))
booster = model.get_booster()
booster.set_feature_types({'feature_name': 'feature_type'})
probs = booster.predict(dmatrix)

In the code above, you would replace feature_name with the actual name of your feature, and feature_type with the type of your feature, such as 'int' or 'float'. You can specify the feature types for all the features in your dataset this way.

MichaelO
New Contributor III

This is a model that has been running for some time and nothing was changed of recent, so as much as what you said makes sense, it was not the issue.

I finally resolved the matter by changing the version of the cluster. I realized that the latest cluster is using the latest version of python which was just released 2 days to the error above. By changing the cluster version to 9.1 LTS, the error message no longer came up. Weird one, I know right.

Anonymous
Not applicable

Hi @Michael Okelolaโ€‹ 

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! 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group