AxisError: axis -1 is out of bounds for array of dimension 0

Viewed 36

I'm trying to implement One Hot Encoding on selected Categorical columns

mask = df.columns.isin(['cp','restecg','slope','thal'])[:-1]

After applying One Hot Encoding like this:

enc = OneHotEncoder(categories=mask, sparse=False)

and instantiate fit like this:

enc.fit(X_train)

I'm getting this error

axis -1 is out of bounds for array of dimension 0

So I searched many places, to what I'm doing wrong, some mentioned that calling OneHotEncoding directly will not work, so I tried this:

from sklearn.compose import ColumnTransformer
enc = ColumnTransformer([(mask, OneHotEncoder())], 
sparse_threshold=False)

After this, I tried to fit in again like this:

enc.fit(X_train)

Now, I'm getting this error:

ValueError: not enough values to unpack (expected 3, got 2)

What am I doing wrong?

0 Answers
Related