numpy.concatenate()函数的使用

参考链接: numpy.concatenate

numpy.concatenate()函数接受一个数组的序列,其中的数组除了在拼接上的维度,其他的维度的形状必须相同.

在这里插入图片描述实验:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> a
array([[1, 2],
       [3, 4]])
>>> b
array([[5, 6]])
>>> a.shape
(2, 2)
>>> b.shape
(1, 2)
>>> 
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b), axis=0).shape
(3, 2)
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])
>>> 
>>> # 如果传入的参数axis=None,那么表示先展成一维再做拼接
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
>>> 
>>> 
>>> a = np.zeros((2,3,4,5,6))
>>> b = np.zeros((2,3,4,7,6))
>>> a.shape
(2, 3, 4, 5, 6)
>>> b.shape
(2, 3, 4, 7, 6)
>>> np.concatenate((a, b), axis=3).shape
(2, 3, 4, 12, 6)
>>> np.concatenate((a, b), axis=2)
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    np.concatenate((a, b), axis=2)
  File "<__array_function__ internals>", line 6, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 3, the array at index 0 has size 5 and the array at index 1 has size 7
>>> 
>>> # 除了拼接的部分,其余部分的形状必须相同
>>> 
>>> 
>>> 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值