有一個data形狀為 ndarray,例如(5, 10, 2); 和其他兩個串列,x1和x2。兩種尺寸10。我想data根據以下條件從中選擇子集,
Across the second dimension
If x1[i]<= data[j, i,0] <=x2[i], then we will select data[j, i,:]
我試過了selected = data[x1<=data[:,:,0]<=x2]。這沒用。我不清楚實作這種基于條件的選擇的有效(或矢量化)方法是什么。
uj5u.com熱心網友回復:
下面的代碼選擇data第三維為 0 的所有值(即每個值都有一些索引data[i, j, 0],并且該值 <= 比對應的x2和 >= 比對應的x1:
idx = np.where(np.logical_and(data[:, :, 0] >= np.array(x1), data[:, :, 0] <= np.array(x2)))
# data[idx] contains the full rows of length 2 rather than just the 0th column, so we need to select the 0th column.
selected = data[idx][:, 0]
代碼假定x1和x2是長度等于data的第二維(在本例中為 10)大小的串列。請注意,代碼僅回傳值,而不是值的索引。
如果您有任何問題,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/469037.html
標籤:熊猫 麻木的 多维数组 scipy numpy-ndarray
上一篇:使用df.at()編輯多個值
下一篇:從帶有刻度的價目表中查找價格
