You can instantiate the OrdinalEncoder method like this:
OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=np.nan)
OrdinalEncoder has two parameters handle_unknown{‘error’, ‘use_encoded_value’} and unknown_value a you can see in the documentation in the parameters section https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OrdinalEncoder.html
Answer from Jorge Luis Jiménez on Stack OverflowYou can instantiate the OrdinalEncoder method like this:
OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=np.nan)
OrdinalEncoder has two parameters handle_unknown{‘error’, ‘use_encoded_value’} and unknown_value a you can see in the documentation in the parameters section https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OrdinalEncoder.html
Try using ord_enc = OrdinalEncoder(categories = cat_s, handle_unknown='ignore', unknown_value = np.nan)
Your problem is that the model has encountered a value in the test data that it had not seen in the training data. This is fine. You just need to add the 'handle_unknown' argument to your encoder.
You should fit encoders and scalers to the training data (but not the test data) and then use them to transform both training and test data. Thus, you must plan for the possibility of unexpected values in the test data.
This will fix your problem:
OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1)
Note that for this to work you have to be on Scikit Learn version 0.24 or above.