np.concatenate()

提到numpy的数组操作,我们就不得不说到np.concatenate()函数,concatenate在英文中是级联的意思,我们可以简单地理解为连接。代码如下:

# -*- coding: utf-8 -*-
import numpy as np


class Debug:
    def __init__(self):
        self.x = np.array([1, 2, 3])
        self.y = np.array([4, 5, 6])
        self.x1 = np.array([[1],[2],[3]])
        self.y1 = np.array([[4],[5],[6]])
        
    def mainProgram(self):
        z = np.concatenate((self.x, self.y))
        z1 = np.concatenate((self.x1, self.y1))
        print("The value of z is: ")
        print(z)
        print("The value of z1 is: ")
        print(z1)
        

if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
The value of z is: 
[1 2 3 4 5 6]
The value of z1 is: 
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]
"""

我们可以看到,对于结果znp.concatenate()完成的操作类似于np.hstack()函数(超链接点击跳转),沿着x轴进行数组堆叠。对于结果z1np.concatenate()完成的操作类似于np.vstack()函数(超链接点击跳转),沿着y轴进行数组堆叠。我们知道这里是一维情况,产生这种结果的原因是np.concatenate()函数默认的连接方向是与被连接的数组本身的坐标轴方向是一致的。因为self.xself.y均为横向数组,所以沿着横向连接。同理self.x1self.y1均为纵向数组,所以沿着纵向连接。那么可不可能把一个横向数组和一个纵向数组连接起来呢?答案是否定的,可以自行尝试,比如将这里的self.xself.y1连接起来,会得到一个错误。

既然对于一维数组是可以进行连接的,那么二维数组呢?接下来我们研究一下二维数组。代码如下:

# -*- coding: utf-8 -*-
import numpy as np


class Debug:
    def __init__(self):
        self.x = np.array([[1, 2], [3, 4]])
        self.y = np.array([[5, 6], [7, 8]])

        
    def mainProgram(self):
        z = np.concatenate((self.x, self.y), axis=0)
        z1 = np.concatenate((self.x, self.y), axis=1)
        print("The value of z is: ")
        print(z)
        print("The value of z1 is: ")
        print(z1)
        

if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
The value of z is: 
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
The value of z1 is: 
[[1 2 5 6]
 [3 4 7 8]]
"""

我们可以从z的结果中得出,此时np.concatenate()完成的操作类似于np.vstack()函数, 沿着y轴进行数组堆叠。从z1的结果中我们可以看到,np.concatenate()完成的操作类似于np.hstack()函数(超链接点击跳转),沿着x轴进行数组堆叠。如我们之前讨论过的坐标轴问题,类似于np.repeat()的坐标轴问题。二维情况下,从左向右,axis=0指的就是y轴,axis=1指的就是y轴。

那么np.concatenate()函数对于一维,二维均是起作用的,那么对于三维数组,它可以使用吗?答案是肯定的,代码如下:

# -*- coding: utf-8 -*-
import numpy as np


class Debug:
    def __init__(self):
        self.x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
        self.y = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

        
    def mainProgram(self):
        z = np.concatenate((self.x, self.y), axis=0)
        z1 = np.concatenate((self.x, self.y), axis=1)
        z2 = np.concatenate((self.x, self.y), axis=2)
        print(self.x.shape)
        print("The value of z is: ")
        print(z)
        print("The value of z1 is: ")
        print(z1)
        print("The value of z2 is: ")
        print(z2)
        

if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
The value of z is: 
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]

 [[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
The value of z1 is: 
[[[1 2]
  [3 4]
  [1 2]
  [3 4]]

 [[5 6]
  [7 8]
  [5 6]
  [7 8]]]
The value of z2 is: 
[[[1 2 1 2]
  [3 4 3 4]]

 [[5 6 5 6]
  [7 8 7 8]]]
"""

我们可以看到结果完全符合我们的预期,至于坐标轴问题,请查看np.repeat()的坐标轴问题。至此,np.concatenate()函数的研究就告一段落了。

如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/109095989
np