數據規整化之軸向連接concatenate

  • numpy.concatenate

 numpy.concatenate((a1a2...)axis=0out=None)

功能:沿着指定的軸向連接一系列數組 

參數:

  • (a1, a2, ...):在指定軸向有相同維度的ndarray
  • axis: 指定連接方向(0表示在列方向上相同, 1表示在行方向上相同)特別當axis 指定爲None時連接數組會平整化
  • out: (optional)如要指定需要確保該變量的維度與輸出維度相同

返回:

  • 連接成功後的ndarray

np.random.seed(1)
left = np.random.randn(3, 4)
right = np.random.randn(3, 6)


# axis = 1
output = np.concatenate((left, right), axis=1)
print(output.shape)

輸出: 

(3, 10)
up = np.random.randn(2,4)
bottom = np.random.randn(3, 4)


# axis = 0
output = np.concatenate((up, bottom))
print(output.shape)

 輸出: 

(5, 4)
# axis = None
output = np.concatenate((up, bottom), axis=None)
print(output)

 輸出:

[-0.69166075 -0.39675353 -0.6871727  -0.84520564 -0.67124613 -0.0126646
 -1.11731035  0.2344157   1.65980218  0.74204416 -0.19183555 -0.88762896
 -0.74715829  1.6924546   0.05080775 -0.63699565  0.19091548  2.10025514
  0.12015895  0.61720311]

  •  torch.cat

 torch.cat(tensorsdim=0out=None) → Tensor  基於tensor的軸向連接與numpy用法相似

left = torch.randn(3, 4)
right = torch.randn(3, 6)

# dim = 1
output = torch.cat((left, right), dim=1)
print(output.size())

  輸出:

torch.Size([3, 10])

 

up = torch.randn(2,4)
bottom = torch.randn(3, 4)

# dim = 0
output = torch.cat((up, bottom))
print(output.size())

 輸出: 

torch.Size([5, 4])

 


參考鏈接: 

numpy官方文檔

pytorch官方文檔

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章