Numpy Matrix Concatenation

1. Matrix splicing

Common methods for numpy matrix splicing:

  • np.append(arr,values,axis)
  • np.concatenate(arrays,axis,out=None)
  • np.stack(arrays,axis,out=None)
  • np.hstack(tup)
  • np.vstack(tup)

① np.append(arr,values,axis)

The concatenation of arrays and arrays or arrays and numbers is supported, but the splicing parameters of three or more arrays are not supported
:

  • arr : the array of values ​​to be added
  • values ​​: the values ​​added to the array arr
  • axis : optional parameter, the default value is None.

Note:
1. If axis is not given , the default axis=None , arr, values ​​will be flattened into a one-dimensional array first .
2. If axis is specified , arr and values ​​must be the same one-dimensional array or have the same shape , otherwise an error will be reported: ValueError: arrays must have the same number of dimensions
3. The maximum value of axis is the dimension of array arr -1 , if the dimension of arr is equal to 1, the maximum value of axis is 0; the dimension of arr is equal to 2, the maximum value of axis is 1, and so on.
4. When the dimension of arr is 2 (interpreted as a single-channel graph ), axis=0 means adding values ​​along the direction of row growth; axis=1 means adding values ​​along the direction of column growth
5, when the dimension of arr is 3 ( understood as a multi-channel map ), axis=0, axis=1 is the same as above; axis=2 means adding along the direction of image depth growthvalues

import numpy as np
a = [[1,2,3,4]]
b = 5
c = [[5,6,7,8]]
d = np.append(a,b) # 数组和数拼接,默认axis=None
e = np.append(a,c) # 数组和数组拼接,默认axis=None
f = np.append(a,c,axis=0) # 按行增长方向拼接(垂直拼接)
g = np.append(a,c,axis=1) # 按列增长方向拼接(水平拼接) 
print(d)
print(e)
print(f)
print(g)

output:
insert image description here

② np.concatenate(arrays,axis,out=None)

The function is similar to np.append(), but it supports splicing of multiple arrays .
parameter:

  • arrays : A tuple containing the arrays that need to be combined . The requirements that these arrays need to meet are: (1) The number of dimensions is the same (2) The number of elements in other dimensions except the dimension specified by axis is correspondingly equal
  • aixs : dimension, specify the direction of the array combination , the default is 0, that is, vertical splicing
  • out : An optional parameter, which is a multidimensional array. If this parameter is provided, the result returned by the function will be saved in out. Of course, the shape of out needs to be equal to the result
import numpy as np
a = [[1,2,3,4]]
b = 5
c = [[5,6,7,8]]
d = np.concatenate((a,b),axis=None) # 数组和数展平成一维数组拼接
e = np.concatenate((a,c)) # 数组和数组拼接,默认axis=0,按行增长方向拼接(垂直拼接)
f = np.concatenate((a,c),axis=1) # 数组和数组拼接,按列增长方向拼接(水平拼接) 
print(d)
print(e)
print(f)

output:
insert image description here

③ np.stack(arrays,axis,out=None)

It also supports multi-matrix splicing . The difference is that stack will add a new dimension in the direction of the specified axis . The default value of axis is 0
parameter:

  • arrays : A tuple containing arrays that need to be combined . These arrays need to meet the following requirements : (1) The same number of dimensions (2) The number of elements in each dimension is correspondingly equal (that is, the shape is equal)
  • aixs : Dimensions, specify which dimension the array increases, and the direction of the combination . The default value of axis is 0, and the zero axis is added by default, and combined according to the direction of the zero axis.
  • out : An optional parameter, which is a multidimensional array. If this parameter is provided, the result returned by the function will be saved in out. Of course, the shape of out needs to be equal to the result
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
b = np.array([[13,14,15,16],[17,18,19,20],[21,22,23,24]])
print(a.shape)
print(b.shape)
c = np.stack((a,b)) # 默认axis=0,数组和数组在0轴拼接,并在该纬度增加一维
d = np.stack((a,b),axis=1) # axis=1,数组和数组在1轴拼接,并在该纬度增加一维
e = np.stack((a,b),axis=2) # axis=2,数组和数组在2轴拼接,并在该纬度增加一维
print(c, c.shape)
print(d, d.shape)
print(e, e.shape)

output:
insert image description here

④ np.hstack(tup)

Horizontal stacking , for multidimensional arrays, horizontal stacking is equivalent to concatenation in the second dimension

import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
b = np.array([[13,14,15,16],[17,18,19,20],[21,22,23,24]])
c = np.hstack((a,b))
print(a, a.shape)
print(b, b.shape)
print(c, c.shape)

output:
insert image description here

⑤ np.vstack(tup)

Vertical stacking , for multidimensional arrays, vertical stacking is equivalent to doing concatenation in the first dimension

import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
b = np.array([[13,14,15,16],[17,18,19,20],[21,22,23,24]])
c = np.vstack((a,b))
print(a, a.shape)
print(b, b.shape)
print(c, c.shape)

output:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44842318/article/details/129783803