Search code examples
pythonarraysscikit-learnsklearn-pandas

Map a Numpy array into a list of characters


Given a two dim numpy array:

a =  array([[-1, -1],
       [-1,  1],
       [ 1,  1],
       [ 1,  1],
       [ 1,  0],
       [ 0, -1],
       [-1,  0],
       [ 0, -1],
       [-1,  0],
       [ 0,  1],
       [ 1,  1],
       [ 1,  1]])

and a dictionary of conversions:

d = {-1:'a', 0:'b', 1:'c'}

how to map the original array into a list of character combinations?

What I need is the following list (or array)

out_put = ['aa', 'ac', 'cc', 'cc', 'cb', 'ba', ....]

(I am doing some machine learning classification and my classes are labeled by the combination of -1, 0,1 and I need to convert the array of 'labels' into something readable, as 'aa', bc' and so on).

If there is a simple function (binarizer, or one-hot-encoding) within the sklearn package, which can convert the original bumpy array into a set of labels, that would be perfect!


Solution

  • Here's another approach with list comprehension:

    my_dict = {-1:'a', 0:'b', 1:'c'}
    out_put  = ["".join([my_dict[val] for val in row]) for row in a]