python-scipyHow do I join two arrays in Python using NumPy?
Joining two arrays in Python using NumPy is a fairly straightforward process. It can be done using the np.concatenate() function. This function takes a tuple or list of arrays to join together.
For example, if we have two NumPy arrays a and b, we can join them together using np.concatenate() like this:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a, b))
print(c)
The output of the above code would be:
[1 2 3 4 5 6]
The np.concatenate() function takes the following parameters:
tupleorlistof arrays to join togetheraxis: The axis along which the arrays will be joined. If it is not specified, it defaults to 0.
Code explanation
import numpy as np: Imports the NumPy library.a = np.array([1, 2, 3]): Creates a NumPy arrayawith the values[1, 2, 3].b = np.array([4, 5, 6]): Creates a NumPy arraybwith the values[4, 5, 6].c = np.concatenate((a, b)): Joins the arraysaandbtogether usingnp.concatenate().print(c): Prints the resulting arrayc.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use Scipy zeros in Python?
- How can I use Python Scipy to zoom in on an image?
- How can I use Python and Numpy to parse XML data?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use Python Scipy to generate a PDF?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and Numpy to zip files?
- How do I calculate the cross-correlation of two arrays using Python and NumPy?
See more codes...