数组(numpy)的拼接函数
参考
concatenate()函数
形式:np.concatenate((a, b), axis=0)
其中a,b是需要拼接的值,axis是拼接的维度,0是按列拼接,1是按行拼接
import pandas as pd
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6],[7,8]])
c=np.concatenate((a, b), axis=0)
d=np.concatenate((a, b), axis=1)
print(c)
print(d)
结果
[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]
列表(list)的拼接函数
参考
1.使用+号列表脚本操作符
将列表B中的元素作为叶子插入到列表A
注:+号只能连接列表和列表,无法连接列表和字符串
listA=['A_h', 'A_d', 'A_c', 'A_s']
listB=['B_h', 'B_d', 'B_c', 'B_s']
listC=listA+listB
print(listC)
['A_h', 'A_d', 'A_c', 'A_s', 'B_h', 'B_d', 'B_c', 'B_s']
append函数(最常用)
listA=['A_h', 'A_d', 'A_c', 'A_s']
listB=['B_h', 'B_d', 'B_c', 'B_s']
listC=[]
listA.append('new element')
print(listA)
listA.append(listB)
print(listA)
listC.append(listB)
print(listC)
['A_h', 'A_d', 'A_c', 'A_s', 'new element']
['A_h', 'A_d', 'A_c', 'A_s', 'new element', ['B_h', 'B_d', 'B_c', 'B_s']]
[['B_h', 'B_d', 'B_c', 'B_s']]
注:想要listC拼接成[‘B_h’, ‘B_d’, ‘B_c’, ‘B_s’],可以取出每一个listB中的值,在拼接。
extend函数
将列表B中的元素 或 字符串的单个字符 (包括空格) 作为叶子插入到列表A:
注:效果与之前的+号列表脚本操作符效果一样,但可以连接列表和字符串
listA=['A_h', 'A_d', 'A_c', 'A_s']
listB=['B_h', 'B_d', 'B_c', 'B_s']
listA.extend(listB)
print(listA)
listA.extend('new element')
print(listA)
['A_h', 'A_d', 'A_c', 'A_s', 'B_h', 'B_d', 'B_c', 'B_s']
['A_h', 'A_d', 'A_c', 'A_s', 'B_h', 'B_d', 'B_c', 'B_s', 'n', 'e', 'w', ' ', 'e', 'l', 'e', 'm', 'e', 'n', 't']
tensor的拼接方式
参考
torch.cat()与torch.stack()——数组的拼接
1.torch.cat()
torch.cat(tensors, dim=0, *, out=None) → Tensor
官方解释:利用给定的维度连接给定的数组序列(cat代表concatenate),所有数组必须具有相同的形状(连接维度除外)或为空。
相当于按指定维度将数组进行拼接
参数解释:
tensors:要连接的数组序列(元组tuple或者列表list)
dim:数组连接的维度
out:输出数组(一般用不到,如果有输出,则可以直接进行赋值操作)
注意:
①tensors输入的必须是数组序列,不能是单个数组;
②输入的数组序列除了dim维度,其他维度必须形状相同。
import torch
a=torch.arange(6).reshape(2,3)
b=torch.arange(12)
c=torch.cat((a,b.reshape(4,3)),dim=0)
# 沿第0维度进行拼接,也就是按行拼接(竖着拼)
d=torch.cat((a,b.reshape(2,6)),dim=1)
# 沿第1维度进行拼接,也就是按列拼接(横着拼)
print(c)
print(c.shape)
print(d)
print(d.shape)
tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
torch.Size([6, 3])
tensor([[ 0, 1, 2, 0, 1, 2, 3, 4, 5],
[ 3, 4, 5, 6, 7, 8, 9, 10, 11]])
torch.Size([2, 9])
上述例子在卷积神经网络中常用于特征图的堆叠。
2.torch.stack()
torch.stack(tensors, dim=0, *, out=None) → Tensor
官方解释:沿着新的维度连接一系列数组,所有的数组都需要具有相同的大小。
相当于先将多个n维数组进行扩维操作,然后再拼接为一个n+1维的数组
参数解释:
tensors:要连接的数组序列(元组tuple或者列表list)
dim:要插入的维度,大小必须介于0和需要拼接的数组维数之间(dim最大不超过数组的维数)
out:输出数组(与cat()类似,一般用不到)
注意:
①与cat类似,必须输入数组序列,不能是单个数组;
②输入的所有数组序列形状(尺寸)必须一致(这里与cat有区别)。
import torch
a=torch.arange(12).reshape(3,4)
b=torch.ones(12).reshape(3,4)
c=torch.stack((a,b),dim=0)
d=torch.stack((a,b),dim=1)
e=torch.stack((a,b),dim=2)
# dim最大可到输入数组的维数,即a、b的维数
print(c)
print(c.shape)
print(d)
print(d.shape)
print(e)
print(e.shape)
tensor([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]])
torch.Size([2, 3, 4])
tensor([[[ 0., 1., 2., 3.],
[ 1., 1., 1., 1.]],
[[ 4., 5., 6., 7.],
[ 1., 1., 1., 1.]],
[[ 8., 9., 10., 11.],
[ 1., 1., 1., 1.]]])
torch.Size([3, 2, 4])
tensor([[[ 0., 1.],
[ 1., 1.],
[ 2., 1.],
[ 3., 1.]],
[[ 4., 1.],
[ 5., 1.],
[ 6., 1.],
...
[ 9., 1.],
[10., 1.],
[11., 1.]]])
torch.Size([3, 4, 2])
仔细观察上个案例维度的变化,可以发现当输入为两组数组时,dim定为几,拼接后哪个维度就是2(有两个输入数组),相当于做了一个扩维拼接操作。首先按dim增加一个维度,然后再从该维度上进行拼接操作。
cat与stack的区别
torch.cat()是直接在原数组数据上进行拼接,不会改变维数大小;torch.stack首先进行扩维,然后再进行拼接,会将维数增大一个单位。
append
可以使用 Python 中的列表(list)来记录多个 Tensor 值。以下是一个简单的程序,用于将多个 Tensor 值添加到列表中,并输出列表元素的数量:
import torch
#创建三个 Tensor 值
a = torch.tensor(0.0424)
b = torch.tensor(1.2345)
c = torch.tensor(9.8765)
#创建一个空列表
tensor_list = []
#将三个 Tensor 值依次添加到列表中
tensor_list.append(a)
tensor_list.append(b)
tensor_list.append(c)
#输出列表元素的数量和每个元素的值
print(f"列表元素数量:{len(tensor_list)}")
print(tensor_list)
for i, tensor in enumerate(tensor_list):
print(f"第{i+1}个元素的值为:{tensor}")
列表元素数量:3
[tensor(0.0424), tensor(1.2345), tensor(9.8765)]
第1个元素的值为:0.042399998754262924
第2个元素的值为:1.2345000505447388
第3个元素的值为:9.876500129699707
在上面的程序中,我们首先创建了三个 Tensor 值 a、b 和 c,然后创建了一个空列表 tensor_list。接着,我们依次使用 append() 方法将三个 Tensor 值添加到列表中。最后,我们通过循环遍历列表元素并输出每个元素的值来验证程序是否正确地记录了三个 Tensor 值。
需要注意的是,上述程序中所创建的 Tensor 值都是标量(只有一个值的 Tensor)。如果需要记录多个 Tensor 值,且这些 Tensor 值的形状(shape)不同,那么可以考虑使用字典(dict)来记录它们,其中每个 Tensor 对应一个键(key)。