# Combining `numpy` arrays

Combining arrays with:

• `np.concatenate()`
• `np.hstack()`
• `np.vstack()`
• `np.c_[]`
• `np.r_[]`
``````

In [1]:

import numpy as np

``````

## Column vectors

``````

In [2]:

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

``````
``````

In [3]:

a.shape

``````
``````

Out[3]:

(3, 1)

``````
``````

In [4]:

a[1]

``````
``````

Out[4]:

array([2])

``````
``````

In [5]:

a

``````
``````

Out[5]:

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

``````
``````

In [6]:

b

``````
``````

Out[6]:

array([[4],
[5],
[6]])

``````

For column vectors:

• `np.concatenate((a,b), axis=1)`
• `np.hstack([a,b])`
• `np.c_[a,b]`

yield the same

and

• `np.concatenate((a,b), axis=0)`
• `np.vstack([a,b])`
• `np.r_[a,b]`

yield the same

### Concatenation

``````

In [7]:

np.concatenate((a,b))

``````
``````

Out[7]:

array([[1],
[2],
[3],
[4],
[5],
[6]])

``````
``````

In [8]:

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

``````
``````

Out[8]:

array([[1],
[2],
[3],
[4],
[5],
[6]])

``````
``````

In [9]:

np.concatenate((a,b), axis=1)

``````
``````

Out[9]:

array([[1, 4],
[2, 5],
[3, 6]])

``````

### Horizontal stacking

``````

In [10]:

np.hstack([a,b])

``````
``````

Out[10]:

array([[1, 4],
[2, 5],
[3, 6]])

``````
``````

In [11]:

np.c_[a,b]

``````
``````

Out[11]:

array([[1, 4],
[2, 5],
[3, 6]])

``````

### Vertical stacking

``````

In [12]:

np.vstack([a,b])

``````
``````

Out[12]:

array([[1],
[2],
[3],
[4],
[5],
[6]])

``````
``````

In [13]:

np.vstack([a,b]).T

``````
``````

Out[13]:

array([[1, 2, 3, 4, 5, 6]])

``````
``````

In [14]:

np.r_[a,b]

``````
``````

Out[14]:

array([[1],
[2],
[3],
[4],
[5],
[6]])

``````

## Row vectors

``````

In [15]:

c = np.array([1,2,3])
d = np.array([4,5,6])

``````
``````

In [16]:

c.shape

``````
``````

Out[16]:

(3,)

``````
``````

In [17]:

c

``````
``````

Out[17]:

array([1, 2, 3])

``````
``````

In [18]:

d

``````
``````

Out[18]:

array([4, 5, 6])

``````

### Concatenation

``````

In [19]:

np.concatenate((c,d))

``````
``````

Out[19]:

array([1, 2, 3, 4, 5, 6])

``````
``````

In [20]:

np.concatenate((c,d), axis=0)

``````
``````

Out[20]:

array([1, 2, 3, 4, 5, 6])

``````
``````

In [21]:

np.concatenate((c,d), axis=1)

``````
``````

---------------------------------------------------------------------------
AxisError                                 Traceback (most recent call last)
<ipython-input-21-4b9db90c9ee3> in <module>()
----> 1 np.concatenate((c,d), axis=1)

AxisError: axis 1 is out of bounds for array of dimension 1

``````

For row vectors:

• `np.concatenate((c,d), axis=0)`
• `np.hstack([c,d])`
• `np.r_[c,d]`

yield the same

### Horizontal stacking

``````

In [22]:

np.hstack((c,d))

``````
``````

Out[22]:

array([1, 2, 3, 4, 5, 6])

``````
``````

In [23]:

np.r_[c,d]

``````
``````

Out[23]:

array([1, 2, 3, 4, 5, 6])

``````

### Vertical stacking

``````

In [24]:

np.vstack((c,d))

``````
``````

Out[24]:

array([[1, 2, 3],
[4, 5, 6]])

``````

``````

In [25]:

np.vstack((c,d)).T

``````
``````

Out[25]:

array([[1, 4],
[2, 5],
[3, 6]])

``````
``````

In [26]:

np.c_[c,d]

``````
``````

Out[26]:

array([[1, 4],
[2, 5],
[3, 6]])

``````