简体   繁体   中英

How to use numpy.vstack to append row to empty array

I want to append some rows of ndarray as values of dictionary. However, I receive an error for unequal dimensions using np.vstack. Please guide me in this regard.

dic = {0:[],1:[]}
point = np.array([[1,2],
                  [4,5],
                  [7,8]])

Desired Output: dic = {0: [[4,5]], 1: [[1,2],[7,8]]}

Below is the code I tried:

import numpy as np
dic = {0:[],1:[]}
point = np.array([[1,2],
                  [4,5],
                  [7,8]])

dic[0] = np.vstack([dic[1],point[1]])
dic[1] = np.vstack([dic[1],point[0]])
dic[1] = np.vstack([dic[1],point[2]])

Error:

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 3

Not sure what you expect from vstack , but it looks like you're looking for simple slicing:

dic[0] = point[[1]]
dic[1] = point[[0,2]]

Output:

{0: array([[4, 5]]),
 1: array([[1, 2],
           [7, 8]])}
In [424]: dic = {0:[],1:[]}
     ...: point = np.array([[1,2],
     ...:                   [4,5],
     ...:                   [7,8]])
     ...: 
     ...: dic[0] = np.vstack([dic[1],point[1]])
     ...: dic[1] = np.vstack([dic[1],point[0]])
     ...: dic[1] = np.vstack([dic[1],point[2]])
Traceback (most recent call last):
  File "<ipython-input-424-e3ba6811826c>", line 6, in <module>
    dic[0] = np.vstack([dic[1],point[1]])
  File "<__array_function__ internals>", line 5, in vstack
  File "/usr/local/lib/python3.8/dist-packages/numpy/core/shape_base.py", line 282, in vstack
    return _nx.concatenate(arrs, 0)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 2

Look at the elements of the np.vstack([dic[1],point[1]]) . One is a empty list, the other a (2,) shape array. np.array([]) is a (0,) shape array, hence the mismatch in shapes. vstack needs to put equally sized array on top of each other. Do not assume that the empty list works the same when working with arrays.

In [425]: dic[1]
Out[425]: []
In [426]: point[1]
Out[426]: array([4, 5])

We could define the initial dic elements as (0,2) shaped array. Then they can be joined with (2,) or (1,2) arrays or even (n,2).

In [427]: dic = {0:np.zeros((0,2),int), 1:np.zeros((0,2),int)}
In [428]:      ...: dic[0] = np.vstack([dic[1],point[1]])
     ...:      ...: dic[1] = np.vstack([dic[1],point[0]])
     ...:      ...: dic[1] = np.vstack([dic[1],point[2]])
     ...: 
In [429]: dic
Out[429]: 
{0: array([[4, 5]]),
 1: array([[1, 2],
        [7, 8]])}

In general it's not a good idea of iteratively join arrays; getting dimensions right is tricky, and it is slower than list appends.

Using lists appends:

In [432]: dic = {0:[],1:[]}
     ...: dic[0].append(point[1])
     ...: dic[1].append(point[0])
     ...: dic[1].append(point[2])
In [433]: dic
Out[433]: {0: [array([4, 5])], 1: [array([1, 2]), array([7, 8])]}
In [434]: for k,v in dic.items():
     ...:     dic[k]=np.vstack(v)
     ...: 
In [435]: dic
Out[435]: 
{0: array([[4, 5]]),
 1: array([[1, 2],
        [7, 8]])}

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