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 in version 1.10.0 onwards.

numpy.stack(arrays, axis)

Where,

Sr.No. Parameter & Description
1

arrays

Sequence of arrays of the same shape

2

axis

Axis in the resultant array along which the input arrays are stacked

Example

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]]]
numpy_array_manipulation.htm
Advertisements