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! 

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.