Sunday, May 31, 2020

Numpy array join operations

Numpy ndarray Join Operations

Join operations allows us to combine two or more Numpy ndarrays together The arrays are joined with the help of the axis.

numpy.concatenate()


The easiest way to join two or more than two arrays of the same shape along the specified axis is by using the concatenate() method.

The syntax of this method is,

numpy.concatenate((a1, a2, ...), axis)
Axie is set by default to 0.

For example,

import numpy as np

nums1 = np.array([10, 20, 30])
nums2 = np.array([40, 50, 60])
nums = np.concatenate((nums1, nums2))

print(nums)

#Output
[10 20 30 40 50 60]
Using Axis, example
import numpy as np

nums1 = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
nums2 = np.array([[5, 15, 25],[35, 45, 55],[65, 75, 85]])
nums = np.concatenate((nums1, nums2),axis=0) #Axis=0

print('======for Axis=0====\n',nums)

nums = np.concatenate((nums1, nums2),axis=1) #Axis=1

print('======for Axis=1=====\n',nums)

#Output

======for Axis=0====
 [[10 20 30]
 [40 50 60]
 [70 80 90]
 [ 5 15 25]
 [35 45 55]
 [65 75 85]]
======for Axis=1=====
 [[10 20 30  5 15 25]
 [40 50 60 35 45 55]
 [70 80 90 65 75 85]]

The dimensions for the concatenating axis must be equal, otherwise, ValueError is raised.
For example,

import numpy as np

nums1 = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
nums2 = np.array([[5, 15, 25],[35, 45, 55]])
nums = np.concatenate((nums1, nums2),axis=0) #Axis=0

print('======for Axis=0====\n',nums)

nums = np.concatenate((nums1, nums2),axis=1) #Axis=1 #VALUE ERROR
#Dimensions does not match
print('======for Axis=1=====\n',nums)
#Output

======for Axis=0====
 [[10 20 30]
 [40 50 60]
 [70 80 90]
 [ 5 15 25]
 [35 45 55]]
Traceback (most recent call last):
  File "/tmp/sessions/19fe97f1e4a9049a/main.py", line 9, in <module>
    nums = np.concatenate((nums1, nums2),axis=1) #Axis=1
ValueError: all the input array dimensions except for
 the concatenation axis must match exactly

Join Arrays using stack functions

numpy.stack()


The stack operation is very much the same as the concatenate operation, except the concatenation is done along a new axis.

The syntax for this method is 

numpy.stack(arrays, axis)
 
For example,'

import numpy as np

nums1 = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
nums2 = np.array([[5, 15, 25],[35, 45, 55],[65, 75, 85]])
nums = np.stack((nums1, nums2),axis=0) #Axis=0

print('======for Axis=0====\n',nums)

nums = np.stack((nums1, nums2),axis=1) #Axis=1

print('======for Axis=1=====\n',nums)

#Output
======for Axis=0====
 [[[10 20 30]
  [40 50 60]
  [70 80 90]]

 [[ 5 15 25]
  [35 45 55]
  [65 75 85]]]
======for Axis=1=====
 [[[10 20 30]
  [ 5 15 25]]

 [[40 50 60]
  [35 45 55]]

 [[70 80 90]
  [65 75 85]]]

numpy.hstack()


This method can be considered as a variant of the stack() method. The arrays can be stacked row-wise(horizontally) using this method.

import numpy as np

nums1 = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
nums2 = np.array([[5, 15, 25],[35, 45, 55],[65, 75, 85]])
nums = np.hstack((nums1, nums2)) #horizontally stacked

print('================\n',nums)

#output
================
 [[10 20 30  5 15 25]
 [40 50 60 35 45 55]
 [70 80 90 65 75 85]]


numpy.vstack()


This is another variant of stack() method that can be used to concatenate the arrays column-wise or vertically.

import numpy as np

nums1 = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
nums2 = np.array([[5, 15, 25],[35, 45, 55],[65, 75, 85]])
nums = np.vstack((nums1, nums2)) #vertically stacked

print('======vstack=======\n',nums)

#Output
======vstack=======
 [[10 20 30]
 [40 50 60]
 [70 80 90]
 [ 5 15 25]
 [35 45 55]
 [65 75 85]]

numpy.dstack()


This is a helper function and a variant of the stack function, and it is used to stack along with the height, which is the same as depth.

import numpy as np

nums1 = np.array([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
nums2 = np.array([[5, 15, 25],[35, 45, 55],[65, 75, 85]])
nums = np.dstack((nums1, nums2)) #horizontally stacked

print('======dstack=======\n',nums)

#Output
======dstack=======
 [[[10  5]
  [20 15]
  [30 25]]

 [[40 35]
  [50 45]
  [60 55]]

 [[70 65]
  [80 75]
  [90 85]]]