OneHotEncoder not behaving?

106 Views Asked by At

When I run the following code:

from sklearn.preprocessing import OneHotEncoder as ohc

enc = ohc(drop='if_binary', sparse_output=False).set_output(transform='pandas')

I get the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-f958840e2f7e> in <module>
      1 from sklearn.preprocessing import OneHotEncoder as ohc
      2 default = pd.read_csv("default.csv", index_col=[0])
----> 3 enc = ohc(drop = 'if_binary',sparse_output=False).set_output(transform='pandas')
      4 df = enc.fit_transform(default[["student"]])
      5 default_enc = default.assign(student = df['student_Yes'])

/usr/local/lib64/python3.6/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

TypeError: __init__() got an unexpected keyword argument 'sparse_output'

I have tried updating anaconda, and sklearn, but nothing worked.

1

There are 1 best solutions below

0
Mattravel On

You are probably using a version of sklearn < 1.2, you should rename sparse_output as sparse, or update to sklearn >= 1.2.

enc = ohc(drop='if_binary', sparse=False).set_output(transform='pandas')

See OneHotEncoder:

New in version 1.2: sparse was renamed to sparse_output