Map a Numpy array into a list of characters

609 views Asked by At

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!

3

There are 3 answers

2
FLab On BEST ANSWER

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]
3
Haleemur Ali On

i think you ought to be able to do this via a list comprehension

# naming something `dict` is a bad idea
d = {-1:'a', 0:'b', 1:'c'}
out_put = ['%s%s' % (d[x], d[y]) for x, y in a]
0
G. Iacono On

I think the following is very readable:

def switch(row):
    dic = {
        -1:'a',
        0:'b',
        1:'c'
    }
    return dic.get(row)

out_put = [switch(x)+switch(y) for x,y in a]