0

I have tried using keras to one-hot encode my categorical variables from a string into a dummy variable. My dataset looks like this below:

    Region         Location           Price
    North Region   Green wood city    150
    West Region    Pebble hill        200
    North Region   Green wood city    160
    West Region    Pebble hill        250
    South Region   Astoria            220

I was converted my categorical features to an array using numpy and one-hot encode using from keras.utils import to_categorical. as shown below:

data = np.array(df1[['Region', 'Location']])
encoded = to_categorical(data)

However, i got the error

ValueError: invalid literal for int() with base 10: 'North Region'.

I looked at the traceback error and this is what it said.

---> 43     y = np.array(y, dtype='int')
     44     input_shape = y.shape

Any help is appreciated. Thanks!

Edit:

to_categorical keras.utils.to_categorical(y, num_classes=None, dtype='float32') Converts a class vector (integers) to binary class matrix.

E.g. for use with categorical_crossentropy.

Arguments

y: class vector to be converted into a matrix (integers from 0 to num_classes). num_classes: total number of classes. dtype: The data type expected by the input, as a string (float32, float64, int32...) Returns

Jonathan
  • 424
  • 4
  • 14
  • 1
    It need int data, you can follow this answer https://stackoverflow.com/questions/56227671/how-can-i-one-hot-encode-a-list-of-strings-with-keras/56227965 – Phung Duy Phong Feb 25 '20 at 08:03
  • 1
    Does this answer your question? [How can I one hot encode a list of strings with Keras?](https://stackoverflow.com/questions/56227671/how-can-i-one-hot-encode-a-list-of-strings-with-keras) – Phung Duy Phong Feb 25 '20 at 08:03
  • 1
    `to_categorical`, as stated in its document, only accepts integer array. Use [`sklearn.preprocessing.OneHotEncoder`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html) – Chris Feb 25 '20 at 08:04
  • got it! thanks @Chris for pointing it out. I missed it earlier when i was reading the keras document – Jonathan Feb 25 '20 at 08:14

0 Answers0