4

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.

8
  • 1
    np.concatenate((a,b),axis=0) extra parens are crucial Commented Feb 12, 2019 at 15:19
  • np.hstack([a,b]) Commented Feb 12, 2019 at 15:20
  • 1
    I think it should be along axis 1 here @MadPhysicist, given the reshape Commented Feb 12, 2019 at 15:23
  • @yatu I agree, posting as the answer Commented Feb 12, 2019 at 15:25
  • G is a list. Leave it that way. Commented Feb 12, 2019 at 17:16

4 Answers 4

4
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).

Sign up to request clarification or add additional context in comments.

Comments

3

You cant really stack arrays with different dimensions or size of dimensions.

This is list (kind of your desired ouput if I understand correctly):

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

Transformed to numpy array:

G_np = np.array(G)

>>> G_np.shape 
(2,)
>>> G_np 
array([list([0, 1, 2, 3]), list([0, 1, 2])], dtype=object)
>>>

Solution in your case (based on your requirements):

a = np.arange(0,4)
b = np.arange(0,3)
G_npy = np.array([a,b])

>>> G_np.shape 
(2,)

>>> G_np 
array([array([0, 1, 2, 3]), array([0, 1, 2])], dtype=object)

>>> G_npy[-1]
array([0, 1, 2])

Edit: In relation to your Question in comment

I must admit I have no Idea how to do it in correct way.

But if a hacky way is ok(Maybe its the correct way), then:

G_npy = np.array([a,b])
G_npy = np.append(G_npy,None) # Allocate space for your new array
G_npy[-1] = np.arange(5) # populate the new space with new array

>>> G_npy
array([array([0, 1, 2, 3]), array([0, 1, 2]), array([0, 1, 2, 3, 4])],
      dtype=object)
>>>

Or this way - but then, there is no point in using numpy

temp = [i for i in G_npy]
temp.append(np.arange(5))
G_npy = np.array(temp)

NOTE:

To be honest, i dont think numpy is good for collecting objects(list like this). If I were you, I would just keep appending a real list. At the end, I would transform it to numpy. But after all, I dont know your application, so I dont know what is best attitude

1 Comment

that looks much better ... I have one question though ... I get a lot of updates to the list G_np ... so lets say that G_np is now equal to array([array([0, 1, 2, 3]), array([0, 1, 2])], dtype=object). lets define c = np.arange(0,7) and I need to "append" that to G_np ... I do G_npy = np.array([G_npy,c]) . But now I cannot identify the individual elements of G_npy ! G_npy[0] is array([array([0, 1, 2, 3]), array([0, 1, 2])], dtype=object) which I do not want ... I need G_npy[0] = array[0,1,2,3] ?
2

Try this way:

import numpy as np
a = np.arange(4).reshape(2,2)
b = np.arange(6).reshape(2,3)
c = np.arange(8).reshape(2,4)
a
# array([[0, 1],
#       [2, 3]])
b
# array([[0, 1, 2],
#       [3, 4, 5]])
c
# array([[0, 1, 2, 3],
#       [4, 5, 6, 7]])
np.hstack((a,b,c))
#array([[0, 1, 0, 1, 2, 0, 1, 2, 3],
#       [2, 3, 3, 4, 5, 4, 5, 6, 7]])

Hope it helps. Thanks

Comments

2

You are missing a parentheses there.

Please refer to the concatenate documentation below.

https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.concatenate.html

import numpy as np

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

c = np.concatenate((a,b), axis=1) #axis 1 as you have reshaped the numpy array

The above will give you the concatenated output c as:

array([[0, 1, 2, 3, 0, 1, 2]])

4 Comments

ok, I see ... what if I need the c matrix to be [[0,1,2,3],[0,1,2]], so that I can call c[-1] = [0,1,2] ?
You can't have a ragged array
Try this and tell me if this is what you are looking for, define the arrays without reshape, then np.c_[[a, b]]
So I wrote a = np.arange(0,4) and b = np.arange(0,3) and G_npy = np.array([a,b]). Which looks correct. Then I get an update I need to add to the G_npy. Call that c = np.arange(0,7). How do I add c to the G_npy list ? If I write G_npy = np.c_[[G_npy,c]] I get something that does not look right. I need G_npy = array([[array([0, 1, 2, 3])], [array([0, 1, 2])], [array([0, 1, 2, 3, 4, 5, 6])]], dtype=object).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.