【问题标题】:"The truth value of an array with more than one element is ambiguous" error [Python]“具有多个元素的数组的真值不明确”错误 [Python]
【发布时间】:2021-04-19 22:31:20
【问题描述】:

index = np.where(slopes > mean - 2 * sd and slopes < mean + 2 * sd)[0]

返回此错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

如果我改为写idx = np.where(slopes < mean + 2 * sd)[0]idx = np.where(slopes > mean - 2 * sd)[0],我会得到正确的索引。为什么我不能同时结合这两个条件?

【问题讨论】:

标签: python numpy


【解决方案1】:

而不是使用 boolean 'and'

index = np.where((slopes > mean - 2 * sd) and (slopes < mean + 2 * sd))[0]

bitwise '&' 试试你的代码:

index = np.where((slopes > mean - 2 * sd) & (slopes < mean + 2 * sd))[0]

注意:有关boolean 'and'bitwise '&'的更多信息,您可以参考this Question

【讨论】:

  • 宾果游戏。谢谢!
猜你喜欢
  • 2015-01-06
  • 2019-08-21
  • 2019-03-19
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
相关资源
最近更新 更多