How do I get Feature orders from xgboost pickle model - python

I am trying to get predictions using my xgboost pickle model using the new data but getting the error "ValueError: Feature shape mismatch"
The reason for this is I need to pass the feature names exactly in the same order model was built. For that I am using the following code but it's not working:-
feature_order= model_pkl.get_booster().feature_names
X_new = X_new[feature_order]
Y_static = model_pkl.predict(X_new)
feature_order is returning "None" it was working in the xgboost version 1.1.1. But Now I am using the xgboost version 1.4.2.
Can you guys please help me in getting the feature order from my xgboost pickle model? So that I can pass my data also in the same order and not run into the error?

Use:
feature_order= model_pkl.feature_names

Related

Unable to save tensorflow model containing the DNNRegressor estimator

I am a beginner in Machine Learning, and I've got a small doubt!
I have been working on a machine learning code, where I am using tensorflow for predictions of future values. Now, the code uses a dataset which implements One Hot Encoding initially, then the OHE columns are combined and the estimator function is created. For the estimator model, I have used DNN Regressor. There is no Keras used anywhere in the code.
model=tf.estimator.DNNRegressor(hidden_units=[100,100,100],feature_columns=feature_columns,
optimizer=tf.optimizers.Adam(learning_rate=0.01),
activation_fn=tf.nn.relu )
Now, I tried using Pickle for saving. However, I get this error:
AttributeError: Can't pickle local object 'DNNRegressorV2.__init__.<locals>._model_fn'
I tried the same using joblib, but I got the following issue:
PicklingError: Can't pickle <function DNNRegressorV2.__init__.<locals>._model_fn at 0x00000175F1A0E948>: it's not found as tensorflow_estimator.python.estimator.canned.dnn.DNNRegressorV2.__init__.<locals>._model_fn
Following this, I tried this code
tf.keras.models.save_model( model, filepath, overwrite=True,
include_optimizer=True, save_format=None, signatures=None, options=None )
But I got the error:
AttributeError: 'DNNRegressorV2' object has no attribute 'built'
I have also tried other methods such as model.save(), model.to_json(), and also tried saving using API - none of them worked out.
Can someone help me out with the same?

Getting 'AttributeError: Can't get attribute 'DeprecationDict' on <module 'sklearn.utils.deprecation'' this error while executing ML project

AttributeError: Can't get attribute 'DeprecationDict' on
Showing error in model = pickle.load(open('rf_regression_model.pkl', 'rb')) this line.
You used a new version of sklearn to load a model which was trained by an old version of sklearn.
So, the options are:
Retrain the model with current version of sklearn if you have the
training script and data
Or fall back to the lower sklearn version
reported in the warning message
Try this
with open('rf_regression_model','rb') as f:
model=pickle.load(f)

Load and use XGBoost PMML or XGBoost .rds model in python sklearn with out loosing its dependencies/nature

I have a XGB model which is built in R, I wanted to use the same model in python for predictions without loosing its nature/embedded dependencies.
I've tried rpy2 and sklearn-pmml-model, seems there is no help for XGBoost models.
Appreciate any help.
You could use PyPMML to evaluate XGBoost PMML in Python, for example:
from pypmml import Model
model = Model.fromFile('the/pmml/model/file/path')
result = predict(data)
The data could be in dict, Series or DataFrame of Pandas, or JSON string.

Loading pre-trained resnet model to cleverhans model format

I am trying to load a pre-trained ResNet model from the MadryLab CIFAR-10 challenge into CleverHans to compute transfer attacks.
However restoring the saved models into the model_zoo.madry_lab_challenges.cifar10_model.ResNet object does not work. It appears to restore fine initially, but when I try to actually use the model, I get an error such as:
Attempting to use uninitialized value
ResNet/unit_3_1/residual_only_activation/BatchNorm/moving_mean
The easiest way to reproduce this error is to actually just run the provided attack_model.py example included in CleverHans here:
https://github.com/tensorflow/cleverhans/blob/master/examples/madry_lab_challenges/cifar10/attack_model.py
It encounters the same error after loading the model when it tries to use it, on both adv_trained and naturally_trained models.
Is there a workaround to this problem?
It seems the other option is to use the cleverhans.model.CallableModelWrapper instead, but I haven't been able to find an example of how to use that.

How to evaluate the PMML file using python

I have pmml file generated by python having random forest classifier, I need to test the model again in python. Kindly let me know how to import the pmml file back to python so that I can test the model using new dataset.
I have tried using titanium package but it went to error because of the version issue of PMML.
The expected output to be the predicted value of the model so that I can verify the accuracy of the model.
You could use PyPMML to load PMML in Python, then make predictions on new dataset, e.g.
from pypmml import Model
model = Model.fromFile('the/pmml/file/path')
result = model.predict(data)
The data could be dict, string in JSON, Series or DataFrame of Pandas.

Categories