# To install this library in Jupyter notebook
#import sys
#!{sys.executable} -m pip install numpy
import numpy as np
np.__version__ , np.__path__
The np.concatenate()
method is used to join arrays with respect to given axis.
np.concatenate(tup, axis=0)
tup
is comma separated ndarrays
Example: Concatenate two 1-D Arrays along axis = 0 (row wise). The 1-D arrays can be of any size/length.
import numpy as np
arr1 = np.random.randint(low = 1, high = 100, size = 5)
arr2 = np.random.randint(low = 1, high = 100, size = 3)
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr3 = np.concatenate((arr1, arr2))
arr3 = np.concatenate((arr1, arr2), axis=0)
print("\nnp.concatenate((arr1,arr2)) = ", arr3)
arr1 = [96 47 91 69 98] arr2 = [85 96 25] np.concatenate((arr1,arr2)) = [96 47 91 69 98 85 96 25]
You cannot concatenate 1-D arrays on axis=1
, as it do not exist :)
Example: Concatenate two 2-D Arrays along axis=0
(vertically/row-wise). The number of columns of two arrays must match.
arr1 = np.random.randint(low = 1, high = 10, size = (2,3))
arr2 = np.random.randint(low = 1, high = 10, size = (3,3))
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
arr3 = np.concatenate((arr1, arr2), axis=0)
print("\nnp.concatenate((arr1,arr2)) = \n", arr3)
arr1 = [[4 4 3] [9 3 5]] arr2 = [[4 1 6] [9 5 3] [9 3 2]] np.concatenate((arr1,arr2)) = [[4 4 3] [9 3 5] [4 1 6] [9 5 3] [9 3 2]]
Example: Concatenate two 2-D Arrays along axis=1
(horizontally/column-wise). The number of rows of two arrays must match.
arr1 = np.random.randint(low = 1, high = 10, size = (2,2))
arr2 = np.random.randint(low = 1, high = 10, size = (2,3))
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
arr3 = np.concatenate((arr1, arr2), axis=1)
print("\nnp.concatenate((arr1,arr2)) = \n", arr3)
arr1 = [[7 7] [6 1]] arr2 = [[7 2 9] [4 4 6]] np.concatenate((arr1,arr2)) = [[7 7 7 2 9] [6 1 4 4 6]]
np.vstack()
: it performs vertical stacking along the rows.np.hstack()
: it performs horizontal stacking along with the columns.np.dstack()
: it performs in-depth stacking along a new third axis (depth).Note:
numpy.stack()
is the most general of the three methods, offering an axis parameter for specifying which way to put the arrays together.np.column_stack()
is used to stack 1-D arrays as columns into 2-D array.np.row_stack()
is used to stack 1-D arrays as rows into 2-D array.Example: np.row_stack()
#ROW STACK
import numpy as np
arr1 = np.array([2,5,1])
arr2 = np.array([3,6,2])
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr3 = np.row_stack((arr1, arr2))
print ("\nnp.row_stack((arr1, arr2)):\n ", arr3)
arr1 = [2 5 1] arr2 = [3 6 2] np.row_stack((arr1, arr2)): [[2 5 1] [3 6 2]]
Example: np.column_stack()
#COLUMN STACK
import numpy as np
arr1 = np.array([2,5,1])
arr2 = np.array([3,6,2])
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr3 = np.column_stack((arr1, arr2))
print ("\nnp.column_stack((arr1, arr2)):\n ", arr3)
arr1 = [2 5 1] arr2 = [3 6 2] np.column_stack((arr1, arr2)): [[2 3] [5 6] [1 2]]
np.vstack()
for Row-Wise Concatenation¶The np.vstack()
method is used to stack arrays vertically or row-wise.
np.vstack(tup)
tup
is comma separated ndarrays
Example: Perform vertical stacking of two 1-D Arrays, which must have the same size/length.
import numpy as np
arr1 = np.random.randint(low = 1, high = 10, size = 4)
arr2 = np.random.randint(low = 1, high = 10, size = 4)
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr3 = np.vstack((arr1, arr2))
print ("\nnp.vstack((arr1, arr2)):\n ", arr3)
arr1 = [1 4 5 6] arr2 = [8 7 4 9] np.vstack((arr1, arr2)): [[1 4 5 6] [8 7 4 9]]
Note: The output array is a 2-D array
Example: Perform vertical stacking of two 2-D Arrays. The number of columns of two arrays must match
arr1 = np.random.randint(low = 1, high = 10, size = (2,3))
arr2 = np.random.randint(low = 1, high = 10, size = (3,3))
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
arr3 = np.vstack((arr1, arr2))
print ("\n np.vstack((arr1, arr2)):\n ", arr3)
arr1 = [[9 1 8] [5 8 3]] arr2 = [[9 8 6] [8 4 7] [7 1 8]] np.vstack((arr1, arr2)): [[9 1 8] [5 8 3] [9 8 6] [8 4 7] [7 1 8]]
np.hstack()
for Column-Wise Concatenation¶The np.hstack()
method is used to stack arrays horizontally or column-wise.
np.hstack(tup)
tup
is comma separated ndarrays
Example: Perform horizontal stacking of two 1-D Arrays, which can be of different size/length
arr1 = np.random.randint(low = 1, high = 10, size = 5)
arr2 = np.random.randint(low = 1, high = 10, size = 4)
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr3 = np.hstack((arr1, arr2))
print ("\n np.hstack((arr1, arr2)):\n ", arr3)
arr1 = [6 2 8 9 3] arr2 = [4 1 1 9] np.hstack((arr1, arr2)): [6 2 8 9 3 4 1 1 9]
Note: The output array is a 1-D array
Example: Perform horizontal stacking of two 2-D Arrays. The number of rows of two arrays must match
arr1 = np.random.randint(low = 1, high = 10, size = (2,2))
arr2 = np.random.randint(low = 1, high = 10, size = (2,3))
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
arr3 = np.hstack((arr1, arr2))
print ("\n np.hstack((arr1, arr2)):\n ", arr3)
arr1 = [[8 3] [9 8]] arr2 = [[6 8 9] [3 2 4]] np.hstack((arr1, arr2)): [[8 3 6 8 9] [9 8 3 2 4]]
np.stack()
¶np.stack()
method is used to join a sequence of same dimension arrays along a new axis.np.stack(a1, a2, a3, ..., axis=0)
tup
is comma separated ndarraysNote: Concatenating joins a sequence of tensors along an existing axis, and stacking joins a sequence of tensors along a new axis
Example: Perform stacking of two 1-D Arrays, which must have the same size/shape.
import numpy as np
arr1 = np.random.randint(low = 1, high = 10, size = 4)
arr2 = np.random.randint(low = 1, high = 10, size = 4)
print("arr1 = ", arr1)
print("arr2 = ", arr2)
# Stacking two 1-D arrays along axis 0 using stack()
arr3 = np.stack((arr1, arr2), axis = 0)
print ("\n np.stack(arr1, arr2, axis=0):\n ", arr3)
# Stacking the two 1-D arrays using row_stack()
arr4 = np.row_stack((arr1, arr2))
print ("\n np.row_stack(arr1, arr2)):\n ", arr4)
# Stacking two 1-D arrays along axis 1 using stack()
arr5 = np.stack((arr1, arr2), axis = 1)
print ("\n np.stack((arr1, arr2), axis=1):\n ", arr5)
# Stacking the two 1-D arrays using column_stack()
arr6 = np.column_stack((arr1, arr2))
print ("\n np.column_stack(arr1, arr2):\n ", arr6)
arr1 = [6 9 9 8] arr2 = [7 2 7 3] np.stack(arr1, arr2, axis=0): [[6 9 9 8] [7 2 7 3]] np.row_stack(arr1, arr2)): [[6 9 9 8] [7 2 7 3]] np.stack((arr1, arr2), axis=1): [[6 7] [9 2] [9 7] [8 3]] np.column_stack(arr1, arr2): [[6 7] [9 2] [9 7] [8 3]]
Example: Perform stacking of two 2-D Arrays, which must have the same size/shape.
import numpy as np
arr1 = np.array([[4,3,1],[5,6,2]])
arr2 = np.array([[5,1,8],[3,9,1]])
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
# Stacking the two arrays along axis 0
arr3 = np.stack((arr1, arr2), axis = 0)
print ("\n np.stack((arr1, arr2), axis=0): \n", arr3)
print("shape of arr3:", arr3.shape)
arr1 = [[4 3 1] [5 6 2]] arr2 = [[5 1 8] [3 9 1]] np.stack((arr1, arr2), axis=0): [[[4 3 1] [5 6 2]] [[5 1 8] [3 9 1]]] shape of arr3: (2, 2, 3)
import numpy as np
arr1 = np.array([[4,3,1],[5,6,2]])
arr2 = np.array([[5,1,8],[3,9,1]])
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
# Stacking the two arrays along axis 1
arr3 = np.stack((arr1, arr2), axis = 1)
print ("\n np.stack((arr1, arr2), axis=1): \n", arr3)
print("shape of arr3:", arr3.shape)
arr1 = [[4 3 1] [5 6 2]] arr2 = [[5 1 8] [3 9 1]] np.stack((arr1, arr2), axis=1): [[[4 3 1] [5 1 8]] [[5 6 2] [3 9 1]]] shape of arr3: (2, 2, 3)
import numpy as np
arr1 = np.array([[4,3,1],[5,6,2]])
arr2 = np.array([[5,1,8],[3,9,1]])
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
# Stacking the two arrays along last axis
arr3 = np.stack((arr1, arr2), axis = -1)
print ("\n np.stack((arr1, arr2), axis=-1):\n ", arr3)
print("shape of arr3:", arr3.shape)
np.split()
: Split array into a list of multiple sub-arrays of equal size.np.hsplit()
: Split array into multiple sub-arrays horizontally (column wise).np.vsplit()
: Split array into multiple sub-arrays vertically (row wise).
np.split()
and np.array_split()
Methods¶np.split()
method splits an array into multiple sub-arrays of equal sizes.np.split(arr, size, axis=0)
arr
is the array to be divided into sub-arrays.size
is an size of the sub-arrays, into which arr
will be divided along the axis.axis
is the axis along which to split, default is 0.np.array_split()
arr
Example: Use of split()
arr1 = np.random.randint(low = 1, high = 10, size = 20)
print("arr1:\n",arr1)
# The split size must be a factor of array size (can be 1, 2, 4, 5, 10)
print("\nSub-arrays: \n", np.split(arr1, 4))
Example: Use of array_split()
# create an array of float type
arr1 = np.random.randint(low = 1, high = 10, size = 13)
print("arr1:\n",arr1)
# The array_split() will not flag an error if size is not a factor of array size (will manage)
print("\nSub-arrays: \n", np.array_split(arr1, 4))
np.hsplit()
Method¶np.hsplit()
method is used to split an array into multiple sub-arrays horizontally (column-wise).np.hsplit()
is equivalent to split with axis=1, the array is always split along the second axis regardless of the array dimension.np.hsplit(arr, size)
arr
is the array to be divided into sub-arrays.size
is the size of the sub-arrays, into which arr
will be divided along the axis. For hsplit()
, size argument should be a factor of number of columns, else it flags an errorarr
Example:
# create an array of float type with 4 rows and 4 columns with sequential values from 0 to 15
arr1 = np.arange(16.0).reshape(4,4)
# print array
print("arr1:\n",arr1)
print("shape: ", arr1.shape)
# horizontally split array into 2 subarrays
print("\nSub-arrays: \n", np.hsplit(arr1, 2))
np.vsplit()
Method¶np.vsplit()
method is used to split an array into multiple sub-arrays vertically (row-wise). Not applicable for 1-D array.np.vsplit()
is equivalent to split with axis=0, the array is always split along the first axis regardless of the array dimension.np.vsplit(arr, size)
-Where,
arr
is the array to be divided into sub-arrays.size
is the size of the sub-arrays, into which arr
will be divided along the axis. For vsplit()
, size argument should be a factor of number of rows, else it flags an errorarr
Example:
# create an array of float type with 4 rows and 5 columns with sequential values from 0 to 19
arr1 = np.arange(16.0).reshape(4,4)
print("arr1:\n",arr1)
print("shape: ", arr1.shape)
# vertically split array into 2 subarrays (remember size argument must be a factor of number of rows )
print("\nSub-arrays: \n", np.vsplit(arr1, 2))
import numpy as np
arr1 = np.array([2,5,1])
arr2 = np.array([3,6,2])
arr3 = np.column_stack((arr1, arr2))
arr4 = np.vstack((arr1, arr2))
arr5 = np.hstack((arr1, arr2))
arr6 = np.stack((arr1, arr2), axis = 1)
print ("arr3: ", arr3)
print ("arr4: ", arr4)
print ("arr5: ", arr5)
print ("arr6: ", arr6)
arr3: [[2 3] [5 6] [1 2]] arr4: [[2 5 1] [3 6 2]] arr5: [2 5 1 3 6 2] arr6: [[2 3] [5 6] [1 2]]