NumPy – hstack

NumPy - hstack

In this chapter, we will discuss about NumPy – hstack. Variants of numpy.stack function to stack so as to make a single array horizontally.

Example Of NumPy hstack

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 'Horizontal stacking:' 
c = np.hstack((a,b)) 
print c 
print '\n'

It would produce the following output −

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

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

Horizontal stacking:
[[1 2 5 6]
 [3 4 7 8]]

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply