How to concatenate arrays in NumPy

You can concatenate arrays in the NumPy with the following code. If you want to learn Python, I highly recommend reading This Book.

How to concatenate arrays in NumPy
How to concatenate arrays in NumPy

Example 1

import numpy as np
a = np.array([[5, 10], [15, 20]])
b = np.array([[50, 100]])
print(np.concatenate((a, b), axis=0))

Output

[[  5  10]
 [ 15  20]
 [ 50 100]]

Example 2

import numpy as np
a = np.array([[5, 10], [15, 20]])
b = np.array([[50, 100]])
print(np.concatenate((a, b.T), axis=1))

Output

[[  5  10  50]
 [ 15  20 100]]

Example 3

import numpy as np
a = np.array([[5, 10], [15, 20]])
b = np.array([[50, 100]])
print(np.concatenate((a, b), axis=None))

Output

[  5  10  15  20  50 100]

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.