SimpleImputer problem

Hello,

When using SimpleImputer with parameters SimpleImputer(handle_unknown=“use_encoded_value”, unknown_value=-1), I always get an error of the form :
init() got an unexpected keyword argument ‘handle_unknown’

If I omit the parameters with SimpleImputer(), I get the following form of error (wich is logical):
(Found unknown categories … in column … during transform).

How can I solve that?

Hi alidaher,

SimpleImputer() has no parameter handle_unknown.
OrdinalEncoder() has this parameter.

imputer = SimpleImputer(strategy="constant", fill_value="missing")
encoder = OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=-1)
categorical_processor = make_pipeline(imputer, encoder)

https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html

Hi,

Sorry, yes it’s the OrdinalEncoder that results in this error in all my codes.

When using OrdinalEncoder with parameters handle_unknown=“use_encoded_value”, unknown_value=-1), I always get an error when executing the encoder code, with the following form :
TypeError: init() got an unexpected keyword argument 'handle_unknown’

If I omit the parameters, I get the following form of error when executiong the cross-val on the testing data ((wich is logical because there are some new unknown values in the testing data)
ValueError: Found unknown categories [’ Holand-Netherlands’] in column 7 during transform

Thank you for your help.

When I use OrdinalEncoder().get_params() , I get only two parameters:

{‘categories’: ‘auto’, ‘dtype’: numpy.float64}

I am not getting the two other parameters:
{handle_unknown and unknown_value}

It seems that you are using an older version of scikit-learn. Are you running this code in a cell of the jupyter notebooks of the Fun MOOC platform or on an alternative jupyter installation?

To know which scikit-learn version you are running, you can type in a cell:

import sklearn
print(sklearn.__version__)

On the Fun MOOC platform, the currently installed version of scikit-learn is: 0.24.2.

I am using Jupyter Notebook ANACONDA. How can we update the sklearn version?

No problem with jupyter notebooks of the Fun MOOC.

If I try to update ANACONDA,SCIKIT-learn will be updated?

Thank you.

You can install scikit-learn from the conda-forge channel which is up to date:

https://anaconda.org/conda-forge/scikit-learn

On the command line this is:

conda install -c conda-forge scikit-learn

If you prefer to use the Anaconda Navigator, you can follow those instructions:

https://conda-forge.org/docs/user/introduction.html#display-conda-forge-packages-in-anaconda-navigator

If you plan to do so, it’s probably a good idea to use a dedicated conda environment to install conda-forge packages to avoid mixing them with the packages of the main channel:

https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/

You can also find more details on how to setup a valid environment for the MOOC here:

Actually I checked and scikit-learn 0.24.2 is also available from in the main Anaconda channel:

https://anaconda.org/anaconda/scikit-learn

Ok. Thank you very much

Yes it’s working now. Thank you.

1 Like