Encoding a tuple of strings into integers

J.Woods from

I'm trying to write by first Neural network from scratch using 2 inputs (length and width) to give the output (r/b). I want to encode the strings r and b into integers 0 and 1 so that I have an output vector. Without using a pre-made function like labelenconder.

length = np.array((3,2,4,3,3.5,2,5.5,1,4.5)) width = np.array((1.5,1,1.5,1,.5,.5,1,1,1)) c =('r','b','r','b','r','b','r','b','?')

Thanks in advance



answered 2 months ago C.Nivs #1

You can do this fairly quickly like so:

my_matcher = {'r':0, 'b':1}

new_input = [my_matcher.get(val) for val in c]

If you need it to be a tuple, you can wrap it with tuple(new_input)