【问题标题】:Inner workings of np.where() and how to check for emptiness/Nonenessnp.where() 的内部工作原理以及如何检查空虚/虚无
【发布时间】:2020-01-02 22:14:03
【问题描述】:

这是一个与基本原理相关的问题,因此对其他人来说可能看起来很愚蠢,但在这里: 通过阅读the docsthis post,我了解到如果传递的参数数组是一维数组,np.where() 会为y 返回一个空数组。但是如何检查它是否实际上是一个空数组,而不是NoneNaN

我已经检查了this post 以检查Noneness,但如果我一开始就无法访问返回值,我会不知道该怎么做。我试图访问这个值(见下面的代码),但我得到一个 IndexError

此外,我对numpy.where() 的内部运作有些困惑。从the docs 它说 np.where() 返回一个 ndarray 但如果我在 Jupyter 上运行它,它会返回一个 tuple

以下是我使用的代码。 (numpy 版本:numpy==1.15.4)

test_array = np.array([4, 5, 6]) # 1-D array
print(type(test_array)) 

>>> <class 'numpy.ndarray'>

thres = 6
result = np.where(test_array > thres)
print(type(result))

>>> <class 'tuple'>

与上面的问题有关,如果我尝试访问结果值,

result
>>> (array([], dtype=int64),)

result[0]
>>> array([], dtype=int64)

result[1]
>>> IndexError: tuple index out of range

如果我遗漏了什么,请告诉我!

提前致谢!

【问题讨论】:

  • 你看到Note了吗?您在单输入模式下使用where。为此,它将我们推荐给np.nonzero。我和大多数 SO 用户都对在这种 nonzero 模式下使用 where 感到内疚。
  • 哦,这很有趣 - 我肯定忽略了 Note 并且肯定在没有意识到的情况下使用了 np.nonzero...感谢您的洞察力!

标签: python arrays numpy data-structures tuples


【解决方案1】:

你的测试数组:

In [57]: arr = np.array([4,5,6])                                                                             
In [58]: arr                                                                                                 
Out[58]: array([4, 5, 6])

测试产生一个布尔数组:

In [59]: arr>6                                                                                               
Out[59]: array([False, False, False])

在该数组中搜索非零值True - 没有。根据文档,结果是一个元组,输入的每个维度一个数组:

In [60]: np.nonzero(arr>6)                                                                                   
Out[60]: (array([], dtype=int64),)
In [61]: _[0]                                                                                                
Out[61]: array([], dtype=int64)

Out[61].size 是 0。Out[61].shape(0,)

一个更有趣的阈值:

In [62]: np.where(arr>4)                                                                                     
Out[62]: (array([1, 2]),)
In [63]: np.nonzero(arr>4)                                                                                   
Out[63]: (array([1, 2]),)

这个元组可以直接用来索引原始数组:

In [64]: arr[_]                                                                                              
Out[64]: array([5, 6])

Out[69] 也是一个有效的索引元组。

当我们处理 2 维或 3 维数组时,结果的元组性质变得更加有趣和有用。

例如,二维数组中 3 的倍数:

In [65]: arr = np.arange(12).reshape(3,4)                                                                    
In [66]: arr                                                                                                 
Out[66]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [67]: (arr % 3)==0                                                                                        
Out[67]: 
array([[ True, False, False,  True],
       [False, False,  True, False],
       [False,  True, False, False]])
In [68]: np.nonzero(_)                                                                                       
Out[68]: (array([0, 0, 1, 2]), array([0, 3, 2, 1]))
In [69]: arr[_]                                                                                              
Out[69]: array([0, 3, 6, 9])

【讨论】:

    【解决方案2】:

    我认为你需要检查result[0]的值。

    print(type(result[0]))
    

    【讨论】:

    • 我在 Jupyter 上试过这个,但是对于 print(type(result[0])),它给了我一个 ndarray,但对于 print(type(result[1])),它仍然给了我 IndexError: tuple index out of range
    猜你喜欢
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2011-08-29
    • 2015-08-21
    相关资源
    最近更新 更多