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