0

This is my code

from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
labelencoder_X = preprocessing.LabelEncoder()
X[:,0] = labelencoder_X.fit_transform(X[:,0])
onehotencoder = OneHotEncoder(categories=[])
X = onehotencoder.fit_transform(X).toarray()

After trying many times i am not able to solve this error

ValueError: Shape mismatch: if categories is an array, it has to be of shape (n_features,).
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 2
    Does this answer your question? [ValueError: Shape mismatch: if categories is an array, it has to be of shape (n\_features,)](https://stackoverflow.com/questions/59525929/valueerror-shape-mismatch-if-categories-is-an-array-it-has-to-be-of-shape-n) – xyzjayne Jun 09 '21 at 16:53

1 Answers1

1

From your code, it's not entirely clear what are you trying to do, but the error is due to this line:

onehotencoder = OneHotEncoder(categories=[])

The documentation says categories argument specifies the order of the one-hot encoded columns returned. If you have n categories in the column you're trying to encode, the length of the categories list should be n. But you're passing an empty list. If you do not specify that, the value categories will default to auto which will automatically determine the categories from data.

In order to fix your error, do this:

onehotencoder = OneHotEncoder()
akilat90
  • 5,436
  • 7
  • 28
  • 42