Stacking together different NumPy arrays

Author: Al-mamun Sarkar Date: 2020-03-31 19:44:35

Stacking together different NumPy arrays. The following shows how to stack together different NumPy Array of the Python NumPy module. 

 

import numpy as np
from numpy import newaxis

 

In [2]:

a = np.floor(10*np.random.random((2,2)))
a

 

Out[2]:

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

 

In [3]:

b = np.floor(10*np.random.random((2,2)))
b

Out[3]:

array([[2., 7.],
       [5., 4.]])

 

In [4]:

np.vstack((a,b))

Out[4]:

array([[1., 1.],
       [3., 0.],
       [2., 7.],
       [5., 4.]])

 

In [5]:

np.hstack((a,b))

Out[5]:

array([[1., 1., 2., 7.],
       [3., 0., 5., 4.]])

 

In [6]:

np.column_stack((a,b))

Out[6]:

array([[1., 1., 2., 7.],
       [3., 0., 5., 4.]])

 

In [7]:

a = np.array([4.,2.])
b = np.array([2.,8.])
a[:,newaxis]

Out[7]:

array([[4.],
       [2.]])

 

In [8]:

np.column_stack((a[:,newaxis],b[:,newaxis]))

Out[8]:

array([[4., 2.],
       [2., 8.]])

 

In [9]:

np.vstack((a[:,newaxis],b[:,newaxis]))

Out[9]:

array([[4.],
       [2.],
       [2.],
       [8.]])

 

In [10]:

np.hstack((a[:,newaxis],b[:,newaxis]))

Out[10]:

array([[4., 2.],
       [2., 8.]])

 

In [11]:

np.r_[1:4,0,4]

Out[11]:

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