Concatenating Two One-Dimensional Numpy Arrays

Concatenating two one-dimensional NumPy arrays

Use:

np.concatenate([a, b])

The arrays you want to concatenate need to be passed in as a sequence, not as separate arguments.

From the NumPy documentation:

numpy.concatenate((a1, a2, ...), axis=0)

Join a sequence of arrays together.

It was trying to interpret your b as the axis parameter, which is why it complained it couldn't convert it into a scalar.

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

Your title explains it - a 1d array does not have a 2nd axis!

But having said that, on my system as on @Oliver W.s, it does not produce an error

In [655]: np.concatenate((t1,t2),axis=1)
Out[655]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18,
19])

This is the result I would have expected from axis=0:

In [656]: np.concatenate((t1,t2),axis=0)
Out[656]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18,
19])

It looks like concatenate ignores the axis parameter when the arrays are 1d. I don't know if this is something new in my 1.9 version, or something old.

For more control consider using the vstack and hstack wrappers that expand array dimensions if needed:

In [657]: np.hstack((t1,t2))
Out[657]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18,
19])

In [658]: np.vstack((t1,t2))
Out[658]:
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9],
[11, 12, 13, 14, 15, 16, 17, 18, 19]])

Concatenate two arrays of different dimensions numpy

You can use vstack for this:

import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.array([7,8,9])

d = np.vstack((a,b))

e = np.vstack((d, c))

print(d)
print(e)

Gives:

[[1 2 3]
[4 5 6]]

[[1 2 3]
[4 5 6]
[7 8 9]]

Concatenating 2 dimensional numpy arrays in Python

You can use hstack and vector broadcasting for that:

a = np.array([[1,2,3],[3,4,5],[6,7,8]])  
b = np.array([9,10,11])
res = np.hstack((a, b[:,None]))
print(res)

Output:

[[ 1  2  3  9]
[ 3 4 5 10]
[ 6 7 8 11]]

Note that you cannot use concatenate because the array have different shapes. hstack stack horizontally the multi-dimentional arrays so it just add a new line at the end here. A broadcast operation (b[:,None]) is needed so that the appended vector is a vertical one.

Concatenate Multidimensional Numpy array with 1D numpy array

If you concatenate two arrays, you have to concatenate the data inside the arrays, not the shape. Furthermore you want to concatenate on the second ("short") axis, which is axis=1:

np.concatenate((X_train, y_train), axis=1)

Concatenating one dimensional numpyarrays with variable size numpy array in loop

A correct basic Python way of making a nested list of strings:

In [57]: nNumbers = [1,2,3]
...: baseVariables = ['a','b','c','d','e']

In [58]: alist = []
...: for i in nNumbers:
...: blist = []
...: for v in baseVariables:
...: blist.append(v+str(i))
...: alist.append(blist)
...:

In [59]: alist
Out[59]:
[['a1', 'b1', 'c1', 'd1', 'e1'],
['a2', 'b2', 'c2', 'd2', 'e2'],
['a3', 'b3', 'c3', 'd3', 'e3']]

That can be turned into an array if necessary - though numpy doesn't provide much added utility for strings:

In [60]: np.array(alist)
Out[60]:
array([['a1', 'b1', 'c1', 'd1', 'e1'],
['a2', 'b2', 'c2', 'd2', 'e2'],
['a3', 'b3', 'c3', 'd3', 'e3']], dtype='<U2')

Or in a compact list comprehension form:

In [61]: [[v+str(i) for v in baseVariables] for i in nNumbers]
Out[61]:
[['a1', 'b1', 'c1', 'd1', 'e1'],
['a2', 'b2', 'c2', 'd2', 'e2'],
['a3', 'b3', 'c3', 'd3', 'e3']]

You are starting with lists! And making strings! And selecting items from a JSON, with y['result'][i][v]. None of that benefits from using numpy, especially not the repeated use of np.append and np.concatenate.

Create a two-dimensional array with two one-dimensional arrays

If you wish to combine two 10 element one-dimensional arrays into a two-dimensional array, np.vstack((tp, fp)).T will do it.

np.vstack((tp, fp)) will return an array of shape (2, 10), and the T attribute returns the transposed array with shape (10, 2) (i.e., with the two one-dimensional arrays forming columns rather than rows).

>>> tp = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> tp.ndim
1
>>> tp.shape
(10,)

>>> fp = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>>> fp.ndim
1
>>> fp.shape
(10,)

>>> combined = np.vstack((tp, fp)).T
>>> combined
array([[ 0, 10],
[ 1, 11],
[ 2, 12],
[ 3, 13],
[ 4, 14],
[ 5, 15],
[ 6, 16],
[ 7, 17],
[ 8, 18],
[ 9, 19]])

>>> combined.ndim
2
>>> combined.shape
(10, 2)

Concatenating two numpy array

Use np.concatenate, in axis parameter you define direction of concatenation.

>>> import numpy as np
>>> a = np.zeros((100,1,300,1))
>>> b = np.zeros((100,1,400,1))
>>> c = np.concatenate((a,b),axis=2)
>>> c.shape
(100, 1, 700, 1)

Manipulating numpy arrays (concatenating inner sub-arrays)

From your new update, you can do the following using np.lib.stride_tricks.as_strided:

>>> np.lib.stride_tricks.as_strided(a, shape=(3,3,2,2,3), strides=(72,24,216,12,4))
array([[[[[ 10, 20, 30],
[ 40, 40, 20]],

[[ 20, 40, 60],
[ 80, 80, 40]]],

[[[ 22, 44, 66],
[ 88, 88, 44]],

[[ 44, 88, 132],
[176, 176, 88]]],

[[[ 33, 66, 99],
[132, 132, 66]],

[[ 66, 132, 198],
[264, 264, 132]]]],

[[[[ 22, 44, 66],
[ 88, 88, 44]],

[[ 44, 88, 132],
[176, 176, 88]]],

[[[ 54, 108, 162],
[216, 216, 108]],

[[108, 216, 324],
[432, 432, 216]]],

[[[ 23, 46, 69],
[ 92, 92, 46]],

[[ 46, 92, 138],
[184, 184, 92]]]],

[[[[ 14, 28, 42],
[ 56, 56, 28]],

[[ 28, 56, 84],
[112, 112, 56]]],

[[[ 25, 50, 75],
[100, 100, 50]],

[[ 50, 100, 150],
[200, 200, 100]]],

[[[ 33, 66, 99],
[132, 132, 66]],

[[ 66, 132, 198],
[264, 264, 132]]]]])

Explanation:

Take another example: a small array q and our desired output after changing q:

>>> q = np.arange(12).reshape(4,3,-1)
>>> q
array([[[ 0],
[ 1],
[ 2]],

[[ 3],
[ 4],
[ 5]],

[[ 6],
[ 7],
[ 8]],

[[ 9],
[10],
[11]]])
# desired output:
# shape = (2, 3, 2)
array([[[ 0, 6],
[ 1, 7],
[ 2, 8]],

[[ 3, 9],
[ 4, 10],
[ 5, 11]]])

Here we are using numpy strides to achieve this. Let's check for q's strides:

>>> q.strides
(12, 4, 4)

In our output, all strides should remain the same, except the third stride, because in the third dimension we need to stack with the values from bottom half of q, ie: 6 is put next to 0, 7 next to 1 and so on...

So, how "far" is it from 0 to 6 ? Or in another word, how far is it from q[0,0,0] to q[2,0,0] ?

# obviously, distance = [2,0,0] - [0,0,0] = [2,0,0]
bytedistance = np.sum(np.array([2,0,0])*q.strides)
# 2*12 + 0*4 + 0*4 = 24 bytes

Okay then new_strides = (12, 4, 24) and hence we got:

>>> np.lib.stride_tricks.as_strided(q, shape=(2,3,2), strides=new_strides)
array([[[ 0, 6],
[ 1, 7],
[ 2, 8]],

[[ 3, 9],
[ 4, 10],
[ 5, 11]]])

Back to your question:

a.strides = (72,24,12,4)
new_strides = (72,24,216,12,4) # why is 216 here ? it's a homework :)
new_a = np.lib.stride_tricks.as_strided(a, shape=(3,3,2,2,3), strides=new_strides)


Related Topics



Leave a reply



Submit