Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append numpy arrays with different dimensions

I am trying to attach or concatenate two numpy arrays with different dimensions. It does not look good so far.

So, as an example,

a = np.arange(0,4).reshape(1,4)
b = np.arange(0,3).reshape(1,3)

And I am trying

G = np.concatenate(a,b,axis=0)

I get an error as a and b are not the same dimension. The reason I need to concatenate a and b is that I am trying to solve a model recursively and the state space is changing over time. So I need to call the last value function as an input to get a value function for the next time period, etc.:

for t in range(T-1,0,-1):

    VG,CG = findv(VT[-1])

    VT = np.append(VT,VG,axis=0)  
    CT = np.append(CT,CG,axis=0) 

But, VT has a different dimension from the time period to the next.

Does anyone know how to deal with VT and CT numpy arrays that keep changing dimension?

OK - thanks for the input ... I need the output to be of the following form:

G = [[0, 1, 2, 3],
     [0, 1, 2]]

So, if I write G[-1] I will get the last element,

[0,1,2].

I do not know if that is a numpy array?

Thanks, Jesper.

like image 899
terman Avatar asked Oct 15 '25 20:10

terman


1 Answers

In [71]: a,b,c = np.arange(0,4), np.arange(0,3), np.arange(0,7)

It's easy to put those arrays in a list, either all at once, or incrementally:

In [72]: [a,b,c]
Out[72]: [array([0, 1, 2, 3]), array([0, 1, 2]), array([0, 1, 2, 3, 4, 5, 6])]
In [73]: G =[a,b]
In [74]: G.append(c)
In [75]: G
Out[75]: [array([0, 1, 2, 3]), array([0, 1, 2]), array([0, 1, 2, 3, 4, 5, 6])]

We can make an object dtype array from that list.

In [76]: np.array(G)
Out[76]: 
array([array([0, 1, 2, 3]), array([0, 1, 2]),
       array([0, 1, 2, 3, 4, 5, 6])], dtype=object)

Be aware that sometimes this could produce a 2d array (if all subarrays were the same size), or an error. Usually it's better to stick with the list.

Repeated append or concatenate to an array is usually not recommended. It's trickier to do right, and slower when it does work.

But let's demonstrate:

In [80]: G = np.array([a,b])
In [81]: G
Out[81]: array([array([0, 1, 2, 3]), array([0, 1, 2])], dtype=object)

c gets 'expanded' with a simple concatenate:

In [82]: np.concatenate((G,c))
Out[82]: 
array([array([0, 1, 2, 3]), array([0, 1, 2]), 0, 1, 2, 3, 4, 5, 6],
      dtype=object)

Instead we need to wrap c in an object dtype array of its own:

In [83]: cc = np.array([None])
In [84]: cc[0]= c
In [85]: cc
Out[85]: array([array([0, 1, 2, 3, 4, 5, 6])], dtype=object)
In [86]: np.concatenate((G,cc))
Out[86]: 
array([array([0, 1, 2, 3]), array([0, 1, 2]),
       array([0, 1, 2, 3, 4, 5, 6])], dtype=object)

In general when we concatenate, the dtypes have to match, or at least be compatible. Here, all inputs need to be object dtype. The same would apply when joining compound dtypes (structured arrays). It's only when joining simple numeric dtypes (and strings) that we can ignore dtypes (provided we don't care about integers becoming floats, etc).

like image 169
hpaulj Avatar answered Oct 18 '25 08:10

hpaulj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!