NumPy – stack

NumPy - stack

In this chapter, we will discuss about NumPy – stack. This function joins the sequence of arrays along a new axis. This function has been added since NumPy version 1.10.0. Following parameters need to be provided.

Note − This function is available from version 1.10.0 onwards.

numpy.stack(arrays, axis)

Where,

Sr.No.Parameter & Description
1array sequence of arrays of the same shape
2axisAxis in the resultant array along which the input arrays are stacked

Example Of NumPy stack

import numpy as np 
a = np.array([[1,2],[3,4]]) 

print 'First Array:' 
print a 
print '\n'
b = np.array([[5,6],[7,8]]) 

print 'Second Array:' 
print b 
print '\n'  

print 'Stack the two arrays along axis 0:' 
print np.stack((a,b),0) 
print '\n'  

print 'Stack the two arrays along axis 1:' 
print np.stack((a,b),1)

It should produce the following output −

First array:
[[1 2]
 [3 4]]

Second array:
[[5 6]
 [7 8]]

Stack the two arrays along axis 0:
[[[1 2]
 [3 4]]
 [[5 6]
 [7 8]]]

Stack the two arrays along axis 1:
[[[1 2]
 [5 6]]
 [[3 4]
 [7 8]]]

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply