Unable to transform string column to categorical matrix using Keras and Sklearn Unable to transform string column to categorical matrix using Keras and Sklearn pandas pandas

Unable to transform string column to categorical matrix using Keras and Sklearn


Its because np_utils.to_categorical takes y of datatype int, but you have strings either convert them into int by giving them a key i.e :

cats = data.PriceRange.values.categoriesdi = dict(zip(cats,np.arange(len(cats))))#{'0 - 50000': 0,# '10000001 - 10050000': 200,# '1000001 - 1050000': 20,# '100001 - 150000': 2,# '10050001 - 10100000': 201,# '10100001 - 10150000': 202,target = np_utils.to_categorical(data.PriceRange.map(di))

or since you are using pandas you can use pd.get_dummies to get one hot encoding.

onehot = pd.get_dummies(data.PriceRange)target_labels = onehot.columnstarget = onehot.as_matrix()array([[ 1.,  0.,  0., ...,  0.,  0.,  0.],       [ 0.,  0.,  0., ...,  0.,  0.,  0.],       [ 0.,  0.,  0., ...,  0.,  0.,  0.],       ...,        [ 0.,  0.,  0., ...,  0.,  0.,  0.],       [ 1.,  0.,  0., ...,  0.,  0.,  0.],       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])


With only one line of code

tf.keras.utils.to_categorical(data.PriceRange.factorize()[0])