Skip to content

[FAQ]TypeError while creating OneHotEncoder Object #19

@RevathyRamalingam

Description

@RevathyRamalingam

Course

machine-learning-zoomcamp

Question

Why I am getting typeError while creating OneHotEncoder Object?

Answer

If you get Type Error when categorical values are oneHotEncoded using sparse as parameter, as below then here is the reason

TypeError Traceback (most recent call last)
Cell In[60], line 4
2 scaler = StandardScaler()
3 X_train_num = scaler.fit_transform(X_train_num)
----> 4 ohe = OneHotEncoder(sparse=False)
5 X_train_cat = ohe.fit_transform(df_train[categorical_columns].values)
6 X_train_cat

This error happens because in scikit-learn ≥1.2, the OneHotEncoder no longer uses the sparse parameter. Instead, it now uses sparse_output.
So create OneHotEncoder class using spare_output as argument like below,
ohe = OneHotEncoder(sparse_output=False)

Checklist

  • I have searched existing FAQs and this question is not already answered
  • The answer provides accurate, helpful information
  • I have included any relevant code examples or links

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions