如何从排列的numpy数组中提取数组? [英] How to extract arrays from an arranged numpy array?

查看:118
本文介绍了如何从排列的numpy数组中提取数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是帖子

This is a relative question of the post How to extract rows from an numpy array based on the content?, and I used the following code to split rows based on the content in the column:

np.split(sorted_a,np.unique(sorted_a[:,1],return_index=True)[1][1:])

代码工作正常,但是后来我尝试将代码拆分为其他情况(如下所示),我发现可能有错误的结果(如案例1中所示).

the code worked fine, but later I tried the code to split other cases (as below), I found that there could be wrong results (as showed in CASE#1).

CASE#1
[[2748309, 246211, 1],
 [2748309, 246211, 2],
 [2747481, 246201, 54]]
OUTPUT#1
[]
[[2748309, 246211, 1],
 [2748309, 246211, 2],
 [2747481, 246201, 54]]
the result I want
[[2748309, 246211, 1],
 [2748309, 246211, 2]]
[[2747481, 246201, 54]]

我认为代码仅在数字少而数字少的情况下才可能成功分割行,而且我不知道如何解决上面案例1中显示的问题.因此,在这篇文章中,我有2个相对的小问题:

I think the code may successfully split rows only in the case with little numbers, which with less digits, and I don't know how to solve problems showed in CASE#1 above. So in this post, I have 2 little relative questions:

1.如何拆分行数更大的行? (如案例1所示)?

2.如何处理(拆分)两种情况的数据,包括第二列中具有相同元素但在第一列中不同的#1行,以及第一列中具有相同元素但第二列中具有不同元素的#2行? (也就是说,python可以同时考虑第一列和第二列中的内容来区分行吗?)

请随时给我建议,谢谢.

Feel free to give me suggestions, thank you.

更新#1

ravel_multi_index函数可以使用整数数组处理此类任务,但是如何处理包含float的数组呢?

The ravel_multi_index function could handle this kind of task with integer-arrays, but how to deal with arrays containing float?

推荐答案

这是一种考虑将每一行中的元素对作为索引元组的方法-

Here's an approach considering pair of elements from each row as indexing tuples -

# Convert to linear index equivalents
lidx = np.ravel_multi_index(arr[:,:2].T,arr[:,:2].max(0)+1)

# Get sorted indices of lidx. Using those get shifting indices.
# Split along sorted input array along axis=0 using those.
sidx = lidx.argsort()
out = np.split(arr[sidx],np.unique(lidx[sidx],return_index=1)[1][1:])

样品运行-

In [34]: arr
Out[34]: 
array([[2, 7, 5],
       [3, 4, 6],
       [2, 3, 5],
       [2, 7, 7],
       [4, 4, 7],
       [3, 4, 6],
       [2, 8, 5]])

In [35]: out
Out[35]: 
[array([[2, 3, 5]]), array([[2, 7, 5],
        [2, 7, 7]]), array([[2, 8, 5]]), array([[3, 4, 6],
        [3, 4, 6]]), array([[4, 4, 7]])]

有关将元素组转换为索引元组的详细信息,请参考 this post .

For a detailed info on converting group of elements as indexing tuple, please refer to this post.

这篇关于如何从排列的numpy数组中提取数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆