2

I am trying to convert a list of lists into an array and something weird is happening.

For example, if I declare the list as:

a=[]
a.append(['a',1,10,100])
a.append(['b',2,20,200])
a.append(['c',3,30,300])
a.append(['d',4,40,400])

and then convert into a vector by

a=np.array(a)

the end result is

[['a','1','1','1'],['b','2','2','2'],['c','3','3','3'],['d','4','4','4']]

I'm a total Python beginner, but from what I've read by using np.array everything in the vector is converted into a string (please correct me if I'm wrong). Why are the zeroes being ignored here and what can I do to fix it?

0

3 Answers 3

5

Since your array(s) have non-uniform data type, you must specify a "structured" dtype

In [2]: a = [('a', 1, 10, 100),
   ...:      ('b', 2, 20, 200),
   ...:      ('c', 3, 30, 300),
   ...:      ('d', 4, 40, 400)]

In [3]: a = np.array(a, dtype = "S1, int, int, int")

In [4]: a
Out[4]: 
array([('a', 1, 10, 100),
       ('b', 2, 20, 200),
       ('c', 3, 30, 300),
       ('d', 4, 40, 400)], 
      dtype=[('f0', 'S1'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8')])

Which you can access easily as:

In [5]: a.dtype.names = ['name','x','y','z']

In [6]: a['x']
Out[6]: array([1, 2, 3, 4])

In [7]: a['name']
Out[7]: 
array(['a', 'b', 'c', 'd'], 
      dtype='|S1')

In [8]: a[0]
Out[8]: ('a', 1, 10, 100)
Sign up to request clarification or add additional context in comments.

Comments

3

This is because the resulting data type (that numpy had to guess) of the array is "string of length 1". You need to specify a maximum item size:

In [51]: np.array(['a', 10])
Out[51]:
array(['a', '1'],
      dtype='|S1')

In [56]: np.array(['a', 10], dtype=(str, 5))
Out[56]:
array(['a', '10'],
      dtype='|S5')

In [57]: np.array(['a', 123456], dtype=(str, 5))
Out[57]:
array(['a', '12345'],
      dtype='|S5')

In [58]: np.array(['a', 123456], dtype=(str, 6))
Out[58]:
array(['a', '123456'],
      dtype='|S6')

Numpy data types

Are you sure your 2d array shouldn't be a 1d array of records (see askewchan's answer)?

4 Comments

Actually this is open for some changes, but better just give the correct string length when non-strings are involved
I need the 2d, but this took care of the problem perfectly. Thanks!
@user1876307 Do you really want strings?
3

Using np.array does not convert everything to string. numpy.array Says:

"dtype : data-type, optional The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array."

In general using numpy you should not mix types. In your example you do. My guess would be, numpy tried to guess the correct type for your arguments, which resulted in up-casting to something string-like. But it did go wrong. I am not experienced with numpy enough to say exactly what happened. So maybe someone smarter will address this. I'd suggest numpy.dtype for futher reading. You can do some magic with it.

What I do suggest is to stick to one type when you create numpy.array. Especially as it is designed to perform computations, while you tried to sneak in some strings/characters. I am not sure what computations you want to do.

Edit: Yea, I guess I'm a numpy noob. Structured dtype seems to be the perfect way for mixed types arrays.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.