【问题标题】:Numpy all or any is not giving me the desired resultNumpy all 或 any 没有给我想要的结果
【发布时间】:2020-08-06 07:44:33
【问题描述】:

我正在努力实现以下目标。

  1. 我想确保在下面的数组中,我想检索索引 0,1 处的所有值都小于 2000 并且索引 3-5 处的任何值都大于 10,000 的子数组。根据这个我应该检索这个二维数组的第一个数组。但是我得到一个空数组。有谁知道为什么?

  2. 您知道我如何测试这个条件是否已从结果数组 x 中正确应用吗?

    test = np.array([[1000,1500,1000,7000,200,40000], [1000,2200,5000,7000,200,4000], [1000,2200,5000,7000,200,40000]])
    x = np.where(np.all(test[0:2]<2000) & np.any(test[3:6]>10000))
    print(x) 
    

【问题讨论】:

    标签: python pandas numpy scipy numpy-ndarray


    【解决方案1】:

    你的意思是:

    mask = (test[:,:2] < 2000).all(1) & (test[:,3:6] > 10000).any(1)
    
    test[mask]
    # array([[ 1000,  1500,  1000,  7000,   200, 40000]])
    
    np.where(mask)
    # (array([0], dtype=int64),)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 2021-03-02
      • 1970-01-01
      • 2022-09-29
      相关资源
      最近更新 更多