1

I want to add a numpy array to another numpy array:

x = np.ones((3))
y = np.zeros((3))

and if we print it we have:

array([ 1.,  1.,  1.])
array([ 0.,  0.,  0.])

I try to concatenate, append or insert x in y but it didn't work.

I want this numpy array:

[[0,0,0], [1,1,1]]

for instance in python:

example = []
test = [2,2,2]
test2 = [3,3,3]
example.append(test)
example.append(test2)
print(example)

Out:

[[2, 2, 2], [3, 3, 3]]

as you can see, python add test and test2 as a list, and example became a list of list. But in numpy i don't manage to have a array of array like that:

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

This array should have a length == 2. If I add an other array, length == 3. Numpy concatenate the two array and not create an array of array:

x = np.ones((3))
y = np.zeros((3))
z = np.concatenate((x,y)) #Out: array([ 2.,  1.,  1.,  0.,  0.,  0.])
z = np.append(x,y) # Out :  array([ 2.,  1.,  1.,  0.,  0.,  0.])
z = np.insert(x,0,y) # Out: array([ 0.,  0.,  0.,  2.,  1.,  1.])
z = np.array((x,y)) # Out array([[ 2.,  1.,  1.],
                                #[ 0.,  0.,  0.]])

the last Out is almost good but if I do that:

w = np.array((2,3,4))
z = np.array((z,w)) # Out: array([array([[ 2.,  1.,  1.],
                                 #[ 0.,  0.,  0.]]), array([2, 3, 4])],  dtype=object)

It creates array(array(z), array(w)) and not array(array(z[0], z[1],...z[-1], w).

iacob
  • 20,084
  • 6
  • 92
  • 119
Adam Bellaïche
  • 427
  • 3
  • 16

3 Answers3

0

You could use:

z = np.stack((x, y), axis=0)

And you would have: print(z)

array([[1., 1., 1.],
   [0., 0., 0.]])
DSlap0
  • 1
-1

You mean like this?

x = np.ones((3))
y = np.zeros((3))

np.array((x,y))

Out[ ]:
array([[1., 1., 1.],
       [0., 0., 0.]])

I try to concatenate, append or insert x in y but it didn't work.

Insert x in y? Like put x at the first position (first element of y) such that your output would look like:

array([[0.,0.,0.],1.,1.])

Is this what you are asking? If so...

x = list(np.ones((3)))
y = list(np.zeros((3)))

y[0] = np.array(x)
np.array(y)

Out[ ]:
array([array([1., 1., 1.]), 0.0, 0.0], dtype=object)
reka18
  • 7,440
  • 5
  • 16
  • 37
  • It works for two arrays, but if we want to add an other array? For example, in my case, i have to add many array, so i use a loop to do that. But i want array of array. – Adam Bellaïche Jun 19 '18 at 09:16
  • 1
    @AdamBellaïche have a look at [`numpy.vstack`](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.vstack.html) --- if the docs do not make sense I could write an answer but I think it shouldn't be necessary... --- another possibility, concatenate and reshape. – gboffi Jun 19 '18 at 09:33
  • @gboffi May be it can works, but it add the array vertically, do you have to do that horizontally? – Adam Bellaïche Jun 19 '18 at 09:39
  • @AdamBellaïche simply use `np.vstack([x,y])`. If you would do `np.hstack([x,y])`, you would obtain the same result as the concatenation you had above. – HerrIvan Jun 19 '18 at 09:47
  • 1
    @AdamBellaïche Why don't you TRY for yourself `vstack` and/or `hstack` at the interactive prompt? absolutely no computer will be harmed 'cause of your tests ;) Seriuosly, never underestimate the power of the interactive prompt. I'd like to add that the only thing better than the interactive prompt for your exploration of the lands of Python is [`ipython`](https://en.wikipedia.org/wiki/IPython). – gboffi Jun 19 '18 at 09:54
  • @gboffi , yes, I already test vstack and hstack. It's not what I wanted but it permits me to find the good way. In fact, on the vstack web page you have "vsplit" or "split" and this what I use to resolve my question. I post the answer. Thanks you. – Adam Bellaïche Jun 19 '18 at 11:06
-1

I have one answer which works:

x = np.ones((3))
y = np.zeros((3))
w = np.array((2,3,4))
x = np.append(x,y)
x = np.append(x, w)
x = np.split(x, len(x)/wanted size of sub-arrays)
x = np.asarray(x)
Adam Bellaïche
  • 427
  • 3
  • 16