在numpy的多维布尔数组索引

在numpy的多维布尔数组索引

本文介绍了在numpy的多维布尔数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个二维数组,数字之一,布尔值:

  X =
阵列([0,0,0,0,0,0,0,0,0,0],
       [1,1,1,1,1,1,1,1,1,1]
       [2,2,2,2,2,2,2,2,2,2]
       [3,3,3,3,3,3,3,3,3,3]
       [4,4,4,4,4,4,4,4,4,4]
       [5,5,5,5,5,5,5,5,5,5]
       [6,6,6,6,6,6,6,6,6,6]
       [7,7,7,7,7,7,7,7,7,7]
       [8,8,8,8,8,8,8,8,8,8]
       [9. 9. 9. 9. 9. 9. 9. 9. 9. 9.]])IDX =
阵列([假,假,假,假,假,假,假,假,假,假]
       [假,真,真,真,真,真,假,假,假,假]
       [假,真,真,真,真,真,假,假,假,假]
       [假,真,真,真,真,真,假,假,假,假]
       [假,假,假,真,真,真,真,假,假,假]
       [假,假,假,假,真,真,真,假,假,假]
       [假,假,假,假,假,假,真,假,假,假]
       [假,假,假,假,假,假,假,真,假,假]
       [假,假,假,假,假,假,假,假,假,假]
       [假,假,假,假,假,假,假,假,假,假],DTYPE =布尔)

在I指数则返回一维数组数组:

  X [IDX]
阵列([1,1,1,1,1,2,2,2,2,2,3,3,3,
    3,3,4,4,4,4,5,5,5,6,7])

我如何索引数组,并与预期输出返回一个二维数组:

  X [IDX]
阵列([1,1,1,1,1]
       [2,2,2,2,2]
       [3,3,3,3,3]
       [4,4,4,4]
       [5,5,5]
       [6]
       [7.]])


解决方案

您命令返回一维数组,因为这是不可能实现无(一)破坏柱结构,这通常是必要的。例如,在 7 您请求的输出原本属于7列,现在它在列0;和(b) numpy的不,据我所知,支持与同尺寸大小不同的高维数组。我的意思是,numpy的不能有一个数组,其前三行是长度为5,长度4等的第4行的 - 所有的行(相同尺寸)需要具有相同的长度

我想你可以期望的最好结果是数组(而不是二维数组)的数组。这是我会怎样构建的,虽然有可能是更好的方法,我不知道的:

 在[9]:从和itertools导入izip
在[11]:阵列([R [ridx]对于R,ridx在izip(X,IDX)如果ridx.sum()大于0])
出[11]:
阵列([阵列([1,1,1,1,1]),阵列([2,2,2,2,2]),
       阵列([3,3,3,3,3]),阵列([4,4,4,4]),
       阵列([5,5,5]),阵列([6]),阵列([7])],DTYPE =对象)

I have a two 2D arrays, one of numbers and one of boolean values:

x =
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.],
       [ 6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.],
       [ 8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.],
       [ 9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.]])

idx =
array([[False, False, False, False, False, False, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False, False, False],
       [False, False, False,  True,  True,  True,  True, False, False, False],
       [False, False, False, False,  True,  True,  True, False, False, False],
       [False, False, False, False, False, False,  True, False, False, False],
       [False, False, False, False, False, False, False,  True, False, False],
       [False, False, False, False, False, False, False, False, False, False],
       [False, False, False, False, False, False, False, False, False, False]], dtype=bool)

When I index the array it returns a 1D array:

x[idx]
array([ 1.,  1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,
    3.,  3.,  4.,  4.,  4.,  4.,  5.,  5.,  5.,  6.,  7.])

How do I index the array and return a 2D array with the expected output:

x[idx]
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.],
       [ 6.],
       [ 7.]])
解决方案

Your command returns a 1D array since it's impossible to fulfill without (a) destroying the column structure, which is usually needed. e.g., the 7 in your requested output originally belonged to column 7, and now it's on column 0; and (b) numpy does not, afaik, support high dimensional array with different sizes on the same dimension. What I mean is that numpy can't have an array whose first three rows are of length 5, 4th row of length 4, etc. - all the rows (same dimension) need to have the same length.

I think the best result you could hope for is an array of arrays (and not a 2D array). This is how I would construct it, though there are probably better ways I don't know of:

In [9]: from itertools import izip
In [11]: array([r[ridx] for r, ridx in izip(x, idx) if ridx.sum() > 0])
Out[11]:
array([array([ 1.,  1.,  1.,  1.,  1.]), array([ 2.,  2.,  2.,  2.,  2.]),
       array([ 3.,  3.,  3.,  3.,  3.]), array([ 4.,  4.,  4.,  4.]),
       array([ 5.,  5.,  5.]), array([ 6.]), array([ 7.])], dtype=object)

这篇关于在numpy的多维布尔数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:50