English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Common functions for connecting arrays are as follows:
Function | Description |
concatenate | Concatenate a sequence of arrays along an existing axis |
stack | Add a series of arrays along a new axis. |
hstack | Horizontally stack arrays in a sequence (column direction) |
vstack | Vertically stack arrays in a sequence (row direction) |
dstack | Stack along height, the height is the same as the depth |
Connection means placing the content of two or more arrays into a single array.
In SQL, we connect tables based on keys, while in NumPy, we connect arrays along an axis.
The numpy.concatenate function is used to concatenate two or more arrays of the same shape along a specified axis, format as follows:
numpy.concatenate((a1, a2, ...), axis)
Parameter description:
a1, a2, ...: Arrays of the same typeaxis: The axis along which the arrays are connected, default is 0
import numpy as np a = np.array([1,2,3,4,5],[3,4,5,6,7]]] print('First array:') print(a) print('\n') b = np.array([5,6,7,8,9],[7,8,9,10,11]]] print('Second array:') print(b) print('\n') # The dimensions of the two arrays are the same print ('Connect two arrays along axis 0: ') print (np.concatenate((a,b))) print('\n') print ('Along axis', 1 Connect two arrays: ' print (np.concatenate((a,b),axis = 1))
The output result is:
[[ 5 6 7 8 9] [ 7 8 9 10 11]] Connect two arrays along axis 0: [[ 1 2 3 4 5] [ 3 4 5 6 7] [ 5 6 7 8 9] [ 7 8 9 10 11]] Along the axis 1 Connect two arrays: [[ 1 2 3 4 5 5 6 7 8 9] [ 3 4 5 6 7 7 8 9 10 11]]
The numpy.stack function is used to concatenate an array sequence along a new axis, format as follows:
numpy.stack(arrays, axis)
Parameter description:
a sequence of arrays with the same shapeaxis: Returns the axis in the array, stacking the input array along it
import numpy as np a = np.array([1,2,3,4,5],[3,4,5,6,7]]] print('First array:') print(a) print('\n') b = np.array([5,6,7,8,9],[7,8,9,10,11]]] print('Second array:') print(b) print('\n') print ('Stack two arrays along axis 0: ') print (np.stack((a,b),0)) print('\n') print ('Along axis', 1 stack two arrays: ' print (np.stack((a,b),1))
The output result is as follows:
First array: [[1 2 3 4 5] [3 4 5 6 7]] Second array: [[ 5 6 7 8 9] [ 7 8 9 10 11]] Stack two arrays along axis 0: [[[ 1 2 3 4 5] [ 3 4 5 6 7]] [[ 5 6 7 8 9] [ 7 8 9 10 11]]] Along the axis 1 Stack two arrays: [[[ 1 2 3 4 5] [ 5 6 7 8 9]] [[ 3 4 5 6 7] [ 7 8 9 10 11]]]
numpy.hstack is a variant of the numpy.stack function, which generates an array by horizontal stacking.
import numpy as np a = np.array([1,2,3,4,5],[3,4,5,6,7]]] print('First array:') print(a) print('\n') b = np.array([5,6,7,8,9],[7,8,9,10,11]]] print('Second array:') print(b) print('\n') print('Horizontal stacking:') c = np.hstack((a, b)) print(c) print('\n')
The output result is as follows:
First array: [[1 2 3 4 5] [3 4 5 6 7]] Second array: [[ 5 6 7 8 9] [ 7 8 9 10 11]] Horizontal stacking: [[ 1 2 3 4 5 5 6 7 8 9] [ 3 4 5 6 7 7 8 9 10 11]]
numpy.vstack is a variant of the numpy.stack function, which generates an array by vertical stacking.
import numpy as np a = np.array([1,2,3,4,5],[3,4,5,6,7]]] print('First array:') print(a) print('\n') b = np.array([5,6,7,8,9],[7,8,9,10,11]]] print('Second array:') print(b) print('\n') print('Vertical stacking:') c = np.vstack((a, b)) print(c)
The output result is:
First array: [[1 2 3 4 5] [3 4 5 6 7]] Second array: [[ 5 6 7 8 9] [ 7 8 9 10 11]] Vertical stacking: [[ 1 2 3 4 5] [ 3 4 5 6 7] [ 5 6 7 8 9] [ 7 8 9 10 11]]
NumPy provides an auxiliary function: dstack() stacks along the height, which is the same as the depth.
import numpy as np a = np.array([1,2,3,4,5],[3,4,5,6,7]]] b = np.array([5,6,7,8,9],[7,8,9,10,11]]] arr = np.dstack((a, b)) print(arr)
The output result is:
[[[ 1 5] [ 2 6] [ 3 7] [ 4 8] [ 5 9]] [[ 3 7] [ 4 8] [ 5 9] [ 6 10] [ 7 11]]]