Is it possible to specify handle_unknown = 'ignore' for certain columns and 'error' for others inside OneHotEncoder?
I think ColumnTransformer()
would help you to solve the problem. You can specify the list of
columns for which you want to apply OneHotEncoder
with ignore
for handle_unknown
and similarly for error
.
Convert your pipeline to the following using ColumnTransformer
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([("ohe_ignore", OneHotEncoder(handle_unknown ='ignore'),
["Flower", "Fruits"]),
("ohe_raise_error", OneHotEncoder(handle_unknown ='error'),
["Country"])])
steps = [('OneHotEncoder', ct),
('LReg', LinearRegression())]
pipeline = Pipeline(steps)
Now, when we want to predict
>>> pipeline.predict(pd.DataFrame({'Country': ['UK'], 'Fruits': ['Apple'], 'Flower': ['Rose']}))
array([2.83333333])
>>> pipeline.predict(pd.DataFrame({'Country': ['UK'], 'Fruits': ['chk'], 'Flower': ['Rose']}))
array([3.66666667])
>>> pipeline.predict(pd.DataFrame({'Country': ['chk'], 'Fruits': ['Apple'], 'Flower': ['Rose']}))
> ValueError: Found unknown categories ['chk'] in column 0 during
> transform
Note: ColumnTransformer
is available from version 0.20
.