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

My code: (scikit-learn 0.22.1 python 3.7.4)

Here Insert Picture Description

Error as follows:

TypeError: __init__() got an unexpected keyword argument 'categorical_features'.

Error means a function __init __ () does not give a desired parameter obtained, meaning that no parameters 'categorical_features'. The original version 0.22.1 scikit-learn later, OneHotEncoder no parameters categorical_features.
If your scikit-learn version still use parameters categorical_features forward in 0.22.1, can not go wrong, but there will be prompt Warning (red line), you can not specify the encoding sequence, prompting you to modify the ColumnTransformer, you can specify its parameters.
Here Insert Picture Description

Corrective action:

Introducing ColumnTransformer (), on the inside OneHotEncoder the converter, to encode the specified column [1] indicates the second column i.e. country. Sex can be directly used LabelEncoder classification.
Here Insert Picture Description

ColumnTransformer of parameters [a]

ColumnTransformer (Transformers, - list> (name, converter column) tuples, specifying object data to be applied to the converter subset.

remainder = 'drop', -> defaults to 'drop', not specified by specifying the column will be removed.
REMAINDER = 'passthrough', all the remaining columns unspecified automatically by transformers)

sparse_threshold = 0.3, -> if the output of the transformer comprises a different sparse matrix, then if the density is less than the total value, they are stacked in a sparse matrix. Use sparse_threshold = 0 is always returned intensive. When the converted output includes all data-intensive, the results will be stacked densely, and will ignore this keyword.
)

Reference

[1]https://scikit-learn.org/stable/modules/generated/sklearn.compose.ColumnTransformer.html#sklearn.compose.ColumnTransformer

Released two original articles · won praise 2 · views 64

Guess you like

Origin blog.csdn.net/qq_44635691/article/details/104374481