OneHotEncoder not behaving?

I am given a code chunk to run in Jupyter to learn about One Hot Encoding and when I run the code an error shows up.

    from sklearn.preprocessing import OneHotEncoder as ohc

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

    df = enc.fit_transform(default[["student"]])

    default_enc = default.assign(student = df['student_Yes'])

then I get the error code:

---------------------------------------------------------------------------
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. The code is supposed to work the next few problems rely on editing it to see how different parts affect it. your text

  • Try removing sparse_output=False. TypeError: __init__() got an unexpected keyword argument 'sparse_output' suggests that sparse_output is not an argument.

    – 

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

Leave a Comment