Categories: NumpyPython

Joining Numpy arrays using stacking operations in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

Two or more arrays with at least one dimension has the same shape, can be joined together to formulate a new array. These kinds of operations are called stacking. Stacking can be performed either row-wise or column-wise. vstack() is used to stack arrays vertically, such that the resulting array combines all the inputting arrays by rows. hstack() does the similar job, stacking arrays horizontally, i.e. the resulting arrays combines inputting arrays by columns.

#Import Numpy module
import numpy as np
#create two array, of shape (4,2)
C1 = np.ones((4, 2))
C1
#output
array([[1., 1.],
       [1., 1.],
       [1., 1.],
       [1., 1.]])
C2 = np.zeros((4, 2))
C2
#output
array([[0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.]])
#vstack() arrays
np.vstack((C1, C2))
#result is an array of shape (8,2)
array([[1., 1.],
       [1., 1.],
       [1., 1.],
       [1., 1.],
       [0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.]])
#np.hstack((C1, C2))
#result is an array of shape (4,4)
array([[1., 1., 0., 0.],
       [1., 1., 0., 0.],
       [1., 1., 0., 0.],
       [1., 1., 0., 0.]])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 month ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

2 months ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

2 months ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

3 months ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

3 months ago

Download R Course source files

Click here to download R Course source files !

12 months ago