简体   繁体   中英

convertion and triming of numpy array of lists to numpy array of numpy arrays problem

hey I'm new to numpy arrays and I have a short code I would like to understand, i tried to convert a numpy array of lists to numpy array of numpy arrays,

a=np.array([[1,2,3],[3,2,3,4]])
for i in range(len(a)):
    if len(a[i])>3:
        a[i]=a[i][:3]
    a[i]=np.array(a[i])

now i checked the types of a and a[0] and a[1] and all are numpy arrays. then i printed the shape of a and it was (2,) and not (2,3) like i expected it to be although a[0] and a[1] lengths are the same, so i would like to know why it happens and how could i convert the variable a from a numpy array of lists to a numpy array of numpy array with the shape (2,3) for example, thank you very much for your help.

Look at a at each step:

In [20]: a=np.array([[1,2,3],[3,2,3,4]])                                                               
In [21]: a                                                                                             
Out[21]: array([list([1, 2, 3]), list([3, 2, 3, 4])], dtype=object)

because the inner lists differ in length, a is 1d object dtype.

In [22]: len(a[0])                                                                                     
Out[22]: 3
In [23]: len(a[1])                                                                                     
Out[23]: 4
In [24]: a[1] = a[1][:3]                                                                               
In [25]: a                                                                                             
Out[25]: array([list([1, 2, 3]), list([3, 2, 3])], dtype=object)

Just because we changed a[1] it doesn't change the shape or dtype of a .

That's true even if we change the lists to arrays:

In [26]: a[0] = np.array(a[0])                                                                         
In [27]: a[1] = np.array(a[1])                                                                         
In [28]: a                                                                                             
Out[28]: array([array([1, 2, 3]), array([3, 2, 3])], dtype=object)

We have to use something like stack to combine those arrays into one:

In [29]: np.stack(a)                                                                                   
Out[29]: 
array([[1, 2, 3],
       [3, 2, 3]])

This is new array, not a modification of a .

np.array(a) doesn't repackage a either.

If the original lists matched, the result would be 2d.'

In [31]: a=np.array([[1,2,3],[3,2,3]])                                                                 
In [32]: a                                                                                             
Out[32]: 
array([[1, 2, 3],
       [3, 2, 3]])

Be careful about these potentially unequal lists - creating object arrays is an unreliable fallback option.

Please check your output again. It works for me which you want.

a=np.array([[1,2,3],[3,2,3,4]])
for i in range(len(a)):
    if len(a[i])>3:
        a[i]=a[i][:3]
    a[i]=np.array(a[i])
    print(a[i])

Output:

[1 2 3]
[3 2 3]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM