【问题标题】:count the number of array elements that belong to a given numerical range计算属于给定数值范围的数组元素的数量
【发布时间】:2020-02-19 22:12:44
【问题描述】:

给定一个 numpy 数组和给定的数值范围,例如 [0.2, 0.3]。是否有任何函数可以计算属于此数值范围的数组元素的计数?谢谢。

【问题讨论】:

标签: python-3.x numpy image-processing scipy


【解决方案1】:

就像here 发布的答案一样,您可以使用np.wherenp.logical_and 来获得您想要的结果,也可以将len() 应用于结果:

import numpy as np
a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])

b = np.where(np.logical_and(a>=6, a<=10))
# returns (array([3, 4, 5]),)

len(b)
# returns 3

【讨论】:

  • 嗨,Wavy,我尝试使用您的答案来测试我的代码之一,但它失败了。我在以下链接中打开了一个问题,stackoverflow.com/questions/58585533/… 你介意看一下吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 2023-02-22
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 2019-05-22
  • 1970-01-01
相关资源
最近更新 更多