簡體   English   中英

結合 2 numpy arrays

[英]combining 2 numpy arrays

我有 2 個 numpy arrays:

其中一個形狀(753,8,1)表示客戶的 8 個連續動作,另一個形狀(753,10)表示訓練樣本的 10 個特征。

我怎樣才能將這兩者結合起來:

所有 10 個特征都附加到訓練樣本的 8 個順序動作中的每一個上,也就是說,組合的最終數組的形狀應為(753,8,11)

也許是這樣的:

import numpy as np

# create dummy arrays
a = np.zeros((753, 8, 1))
b = np.arange(753*10).reshape(753, 10)

# make a new axis for b and repeat the values along axis 1
c = np.repeat(b[:, np.newaxis, :], 8, axis=1)
c.shape
>>> (753, 8, 10) 

# now the first two axes of a and c have the same shape
# append the values in c to a along the last axis
result = np.append(a, c, axis=2)

result.shape
>>> (753, 8, 11)

result[0]
>>> array([[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])

# values from b (0-9) have been appended to a (0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM