NumPy Array Concateneren

Verbind NumPy arrays

Verbinden betekent het plaatsen van de inhoud van twee of meer arrays in een enkele array.

In SQL verbinden we tabellen op basis van de sleutel, terwijl we in NumPy arrays op as verbinden.

We pass a series of arrays to be connected to the axis concatenate() De array van de functie. Als het as niet expliciet wordt doorgegeven, wordt het als 0 beschouwd.

Example

Verbind twee arrays:

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)

Run Example

Example

Verbind twee 2-D array's langs de rij (axis=1):

import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)

Run Example

Connecting arrays using the stacking function

Stacking is the same as concatenation, the only difference is that stacking is done along a new axis.

We can stack two one-dimensional arrays along the second axis, which will cause them to overlap with each other, that is, stacking (stacking).

We pass a series of arrays to be connected to the axis concatenate() An array of methods. If the axis is not explicitly passed, it is considered as 0.

Example

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)

Run Example

Stacked along rows

NumPy provides an auxiliary function:hstack() Stacked along rows.

Example

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr)

Run Example

Stacked along columns

NumPy provides an auxiliary function:vstack() Stacked along columns.

Example

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)

Run Example

Stacked along height (depth)

NumPy provides an auxiliary function:dstack() Stacked along height, the height is the same as the depth.

Example

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
print(arr)

Run Example