1
A
回答
0
我找到那種我解決方法有點麻煩:
import numpy as np
x = np.random.rand(4);
filter_bools = x < .5;
sorted_indices = np.argsort(x)
sorted_filter = filter_bools[sorted_indices]
filtered_sorted_indices = sorted_indices[sorted_filter == True]
print 'x:\t\t\t\t', x
print 'sorted indices:\t\t\t', sorted_indices
print 'indices for x < .5, sorted:\t', filtered_sorted_indices
print 'sorted, filtered x values:\t', x[filtered_sorted_indices]
輸出:
x: [ 0.8974009 0.30127187 0.71187137 0.04041124]
sorted indices: [3 1 2 0]
indices for x < .5, sorted: [3 1]
sorted, filtered x values: [ 0.04041124 0.30127187]
2
發現的x < 0.5
和x.argsort()
面具似乎是強制性這裏。一旦你有了這兩個,你可以使用排序索引對掩碼數組進行排序,並在排序索引上使用此掩碼來獲取與滿足掩碼條件的排序索引對應的索引。因此,你將被添加的一行代碼,像這樣 -
mask = x < 0.5
sort_idx = x.argsort()
out = sort_idx[mask[sort_idx]]
樣品一步一步運行 -
In [56]: x
Out[56]: array([ 0.8974009 , 0.30127187, 0.71187137, 0.04041124])
In [57]: mask
Out[57]: array([False, True, False, True], dtype=bool)
In [58]: sort_idx
Out[58]: array([3, 1, 2, 0])
In [59]: mask[sort_idx]
Out[59]: array([ True, True, False, False], dtype=bool)
In [60]: sort_idx[mask[sort_idx]]
Out[60]: array([3, 1])
2
屏蔽陣列簡明(但也許不是特別有效)
x = np.random.rand(4);
inverse_mask = x < 0.5
m_x = np.ma.array(x, mask=np.logical_not(inverse_mask))
sorted_indeces = m_x.argsort(fill_value=1)
filtered_sorted_indeces = sorted_indeces[:np.sum(inverse_mask)]
相關問題
- 1. 在Python3/Numpy中過濾數組並返回索引
- 2. numpy的排序函數返回無
- 3. numpy數組過濾兩次,並替換
- 4. 進行排序,並返回數組
- 5. numpy數組到base64並返回到Numpy數組 - Python
- 6. 過濾來自類數組
- 7. 返回numpy數組的最小X數並保持順序
- 8. 如何「zip排序」並行numpy數組?
- 9. Numpy:將函數應用於兩個numpy數組並返回兩個numpy數組
- 10. 來自多個來源的過濾,合併,排序和頁面數據
- 11. 過濾AJAX返回的數據並在
- 12. sitk.GetArrayFromImage返回空numpy數組?
- 13. Python,numpy排序數組
- 14. 排序多個numpy數組
- 15. 排序一個numpy數組
- 16. 來自HTTP調用返回的數據過濾 - Angular
- 17. 返回所有的列當用Numpy pythonly過濾矩陣
- 18. 過濾器numpy元組數組
- 19. CakePHP:分頁,排序,過濾來自緩存表的數據
- 20. javascript中返回過濾數組
- 21. 離子返回過濾JSON數組
- 22. 過濾numpy數組的行嗎?
- 23. 分組numpy的數組,並返回最小值
- 24. 想要過濾來自JSON的值並添加到數組
- 25. 返回排序後的數組
- 26. 排序經濾波的GridView
- 27. 返回來自我的StringRequest的數據 - 排序庫
- 28. 我該如何過濾一個數組並返回匹配項
- 29. Numpy:通過多維數組對多維數組排序
- 30. JavaScript過濾器方法返回過濾陣列和空數組
是否有任何解決方案適合您? – Divakar
嗨Divakar - 是的......好吧,好像我在所有的時候都處於正確的軌道上 - 煩人地混淆了同時追蹤指數和元素:P。感謝您發佈解決方案! – ConfusinglyCuriousTheThird