简体   繁体   中英

ValueError: Shape mismatch: if categories is an array, it has to be of shape (n_features,)

I have create a simple code to implement OneHotEncoder .

from sklearn.preprocessing import OneHotEncoder
X = [[0, 'a'], [0, 'b'], [1, 'a'], [2, 'b']]
onehotencoder = OneHotEncoder(categories=[0])
X = onehotencoder.fit_transform(X).toarray()

I just want to use method called fit_transform to the X for index 0 , so it means for [0, 0, 1, 2] like what you see in X . But it causes an error like this :

ValueError: Shape mismatch: if categories is an array, it has to be of shape (n_features,).

Anyone can solve this problem ? I am stuck on it

You need to use ColumnTransformer to specify the column index not categories parameter.

Constructor parameter categories is to tell distinct category values explicitly. Eg you could provide [0, 1, 2] explicitly, but auto will determine it. Further, you can use slice() object instead.

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

X = [[0, 'a'], [0, 'b'], [1, 'a'], [2, 'b']]

ct = ColumnTransformer(
    [('one_hot_encoder', OneHotEncoder(categories='auto'), [0])],   # The column numbers to be transformed (here is [0] but can be [0, 1, 3])
    remainder='passthrough'                                         # Leave the rest of the columns untouched
)

X = ct.fit_transform(X)

pandas.get_dummies() method also can do same in the way below:

import numpy as np
import pandas as pd
X = np.array([[0, 'a'], [0, 'b'], [1, 'a'], [2, 'b']])
X = np.array(pd.concat([pd.get_dummies(X[:, 0]), pd.DataFrame(X[:, 1])], axis = 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM