In [2]:
import numpy as np
In [3]:
arr1 = np.array([[1,2,3,4], [1,2,3,4]])
arr2 = np.array([[5,6,7,8], [5,6,7,8]])
np.concatenate((a, b), axis=0)
¶
In [4]:
arr1
Out[4]:
In [5]:
arr2
Out[5]:
In [6]:
# concat along the row
cat = np.concatenate((arr1, arr2), axis=0)
print(cat)
In [7]:
# concat along the column
cat = np.concatenate((arr1, arr2), axis=1)
print(cat)
Stacking: np.hstack()
and n.vstack()
¶
Stacking is done using the np.hstack()
and np.vstack()
methods. For horizontal stacking, the number of rows should be the same, while for vertical stacking, the number of columns should be the same.
In [8]:
# stack arrays vertically
cat = np.vstack((arr1, arr2))
print(cat)
In [9]:
# stack arrays vertically
cat = np.r_[arr1, arr2]
print(cat)
In [10]:
# stack arrays horizontally
cat = np.hstack((arr1, arr2))
print(cat)
In [11]:
# stack arrays horizontally
cat = np.c_[arr1, arr2]
print(cat)
split an array¶
In [12]:
arr = np.random.rand(6,6)
In [13]:
# split the array vertically into n evenly spaced chunks
arr1 = np.vsplit(arr, 2)
print(arr1)
In [15]:
# split the array horizontally into n evenly spaced chunks
arr2 = np.hsplit(arr, 2)
print(arr2)