过滤1-D阵列后获取剩余向量的索引

发布于 2025-02-10 22:01:44 字数 974 浏览 2 评论 0原文

我有两个(1维)np.array变量:_nums& _data。两者都有相同的形状。

_nums将被操纵&过滤,因此最终它的值将比_DATA少。

>>> ls1 = [
    ['q1', 'q2', 'q3'],  
    ['w1', 'w2', 'w3'],  
    ['e1', 'e2', 'e3'],   
    ['r1', 'r2', 'r3'],   
    ['t1', 't2', 't3'],   
    ['y1', 'y2', 'y3']
]
>>> _data = np.array(ls1)  # This is optional.

>>> ls2 = [  
    [11, 22, 33],   
    [44, 55, 66],   
    [77, 88, 99],   
    [00, 111, 222],   
    [333, 444, 555],   
    [666, 777, 888]
]
>>> _nums = np.array(ls2, dtype=DTYPE)

>>> # _data & _nums have same shape

>>> _nums = _nums[_nums['param1'] >= X]
[[44, 55, 66], [333, 444, 555]]

所见

如您 在 ls2 中,这些值将在索引1和索引4中, _data ls1

是否有一种数字来获取这些索引?因此,n i可以使用 _nums ...提供的索引在 _data 中提取相应的值?

I have two (1 dimensional) np.array variables: _nums & _data . Both have the same shape.

_nums is going to be manipulated & filtered so in the end it's going to have less values than _data.

>>> ls1 = [
    ['q1', 'q2', 'q3'],  
    ['w1', 'w2', 'w3'],  
    ['e1', 'e2', 'e3'],   
    ['r1', 'r2', 'r3'],   
    ['t1', 't2', 't3'],   
    ['y1', 'y2', 'y3']
]
>>> _data = np.array(ls1)  # This is optional.

>>> ls2 = [  
    [11, 22, 33],   
    [44, 55, 66],   
    [77, 88, 99],   
    [00, 111, 222],   
    [333, 444, 555],   
    [666, 777, 888]
]
>>> _nums = np.array(ls2, dtype=DTYPE)

>>> # _data & _nums have same shape

>>> _nums = _nums[_nums['param1'] >= X]
[[44, 55, 66], [333, 444, 555]]

As you can see _nums shape is now different from _data or ls1...

If I manually find _nums values in ls2 those values would be at index 1 and at index 4 in _data or ls1.

Is there a numpy way to get those indexes? So the n I can extract the corresponding values in _data using the indexes provides by _nums...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2025-02-17 22:01:44

您可以使用 numpy broadcasting 带有 np.np.where 这样做。由于复杂性和性能的不同,许多其他选项是可能的,但是这种方法似乎很简单。

nums = np.array(ls2, dtype=DTYPE)

_nums = nums[nums['param1'] >= X]
 # [[44, 55, 66], [333, 444, 555]]

ind = np.where((nums==_nums[:,None]).all(-1))[1]
 # [1 4]

_data[ind,:]   # OR: [ls1[i] for i in ind]
 # [['w1' 'w2' 'w3']
 #  ['t1' 't2' 't3']]

You can use NumPy broadcasting with np.where to do this. Many other options are possible with varying complexity and performance but this method seems straightforward.

nums = np.array(ls2, dtype=DTYPE)

_nums = nums[nums['param1'] >= X]
 # [[44, 55, 66], [333, 444, 555]]

ind = np.where((nums==_nums[:,None]).all(-1))[1]
 # [1 4]

_data[ind,:]   # OR: [ls1[i] for i in ind]
 # [['w1' 'w2' 'w3']
 #  ['t1' 't2' 't3']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文