1.

How is vstack() different from hstack() in NumPy?

Answer»

Both methods are used for combining the NumPy arrays. The main difference is that the hstack method combines arrays horizontally whereas the vstack method combines arrays vertically.
For example, consider the below code.

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

# vstack arrays
c = np.vstack((a,b))
print("After vstack: \n",c)
# hstack arrays
d = np.hstack((a,b))
print("After hstack: \n",d)

The output of this code would be:

After vstack:
[[1 2 3]
[4 5 6]]
After hstack:
[1 2 3 4 5 6]

Notice how after the vstack method, the arrays were combined vertically along the column and how after the hstack method, the arrays were combined horizontally along the row.




Discussion

No Comment Found