x考慮一個具有任意長度和任意列數的np 陣列串列。舉個例子:
import numpy as np
np.random.seed(0)
random = np.random.randint(0,3,100)
x = [random[0:20].reshape(10,2), random[20:30].reshape(10,1), random[30:60].reshape(10,3), random[60:70].reshape(10,1), random[70:90].reshape(10,2), random[90:100].reshape(10,1)]
從 x,我想創建一個串列,其中包含串列 x 中的物件數,作為具有總列長度的布爾陣列
y = [np.array([True, True, False, False, False, False, False, False, False, False]),
np.array([False, False, True, False, False, False, False, False, False, False]),
np.array([False, False, False, True, True, True, False, False, False, False]),
np.array([False, False, False, False, False, False, True, False, False, False]),
np.array([False, False, False, False, False, False, False, True, True, False]),
np.array([False, False, False, False, False, False, False, False, False, True])]
以便
X = np.concatenate(x, axis = 1)
(x[0] == X[:,y[0]]).all()
True
uj5u.com熱心網友回復:
您可以對中的列數使用累積總和x。然后,您y的創建與
xc = [0, *np.cumsum([i.shape[1] for i in x])]
y = [np.asarray([q >= xc[n] and q < xc[n 1] for q in range(xc[-1])]) for n in range(len(x))]
這只是檢查從 0 到您的總列數(累積和的最后一個條目)的每個值 q,如果它位于累積和的兩個條目之間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/461737.html
