LabelEncoder - fit list of tuples - y should be a 1d array

Viewed 524

I want to label-encode list of tuples using sklearn.preprocessing.LabelEncoder, such as:

[(4,5), (6, 7), (1, 1), (6, 7), ... ]

So that every tuple gets a unique label. However, this structure gets converted to 2D np.ndarray which is the cause of the following error:

ValueError: y should be a 1d array, got an array of shape (N, 2) instead.

How can I manipulate my initial structure so that it can be encoded properly (I assume this is the only solution since LabelEncoder is not configurable) ?

1 Answers

Encoders require their input to be uniformly strings or numbers, so stringify your tuples before passing them into le.fit()?

data = [(4,5), (6, 7), (1, 1), (6, 7), (8, 9), (10, 11)]
le.fit([str(t) for t in data])
print(le.classes_)
#Output: ['(1, 1)' '(10, 11)' '(4, 5)' '(6, 7)' '(8, 9)']
Related