We have done some basic operations on a array, now, let’s see the combination of two arrays.
Firstly, as usual, make two arrays
| 1 | A = np.array([1,1,1]) | 
Vertical stack
It is pretty easy to combinate them in an array on vertical ,
| 1 | C = np.vstack((A,B)) | 
And let’s see the shape of it,
| 1 | C.shape | 
Horizontal stack
No words…
| 1 | D = np.hstack((A,B)) | 
And shape,
| 1 | D.shape | 
Now, there is a challenge, we wanna get a special array, like this
| 1 | array([[1], | 
We have known we can transpose an array
| 1 | A.T | 
But, the type of it…
| 1 | A.T.shape | 
Same as before, now, we have a new way
| 1 | A[np.newaxis,:] | 
It seems good, and see the type
| 1 | A[np.newaxis,:].shape | 
Successfully, we do half of that special array.
We change A and B to this type
| 1 | A = A[:,np.newaxis] | 
Can we combinate via last two ways? Absolutely, yes.
Let’s try.
| 1 | np.hstack((A,B)) | 
Yes, we get that special array type on vertical way.
Is it available for it to combinate more than two arrays?
We have not gotten that special array. Let’s try.
| 1 | np.vstack((A,A,B,B)) | 
We get it!!!
Now, let’s see another way, use the function concatenate
Concatenate
We can use this function to do both hstack and vstack that they can do.
| 1 | np.concatenate((A,A,B,B), axis=0) | 
output:
| 1 | array([[1], | 
The 0 is represented as vertical.
If axis = 1,
| 1 | np.concatenate((A,A,B,B), axis=1) | 
output:
| 1 | array([[1, 1, 2, 2], | 
It is equal to hstack.
So, if we memory this function, we can do same function as both of hstack and vstack.
Thanks for your reading, I hope it is useful for you.
End
Illustrator / Cagy
Text / Cagy
Editor / Cagy
Design / Cagy
