Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.column_stack with numeric and string arrays

Tags:

python

numpy

I have several arrays, some of them have float numbers and others have string characters, all the arrays have the same length. When I try to use numpy.column_stack in these arrays, this function convert to string the float numbers, for example:

a = np.array([3.4,3.4,6.4])
b = np.array(['holi','xlo','xlo'])

B = np.column_stack((a,b))

print B
>>> [['3.4' 'holi']
     ['3.4' 'xlo']
     ['3.4' 'xlo']

type(B[0,0])
>>> numpy.string

Why? It's possible to avoid it? Thanks a lot for your time.

like image 571
Nicolás Medina Avatar asked Mar 12 '23 10:03

Nicolás Medina


1 Answers

The easiest structured array approach is with the rec.fromarrays function:

In [1411]: a=np.array([3.4,3.4,6.4]); b=np.array(['holi','xlo','xlo'])
In [1412]: B = np.rec.fromarrays([a,b],names=['a','b'])
In [1413]: B
Out[1413]: 
rec.array([(3.4, 'holi'), (3.4, 'xlo'), (6.4, 'xlo')], 
          dtype=[('a', '<f8'), ('b', '<U4')])
In [1414]: B['a']
Out[1414]: array([ 3.4,  3.4,  6.4])
In [1415]: B['b']
Out[1415]: 
array(['holi', 'xlo', 'xlo'], 
      dtype='<U4')

Check its docs for more parameters. But it basically constructs an empty array of the correct compound dtype, and copies your arrays to the respective fields.

like image 153
hpaulj Avatar answered Mar 20 '23 08:03

hpaulj