gpt4 book ai didi

python - 在范围之间重新分类python中的numpy数组

转载 作者:太空狗 更新时间:2023-10-29 23:58:11 25 4
gpt4 key购买 nike

我在 Python 中有一个 numpy 数组,我需要在一个值范围(>= 2 到 < 5 = 100)之间进行分类。我收到一条错误消息,我不明白 a.any() 或 a.all()

的用法
    import numpy as np
myarray = np.array([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]])
myarray[myarray >= 2 and myarray < 5] = 100

Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

最佳答案

你们是如此亲密。

>>> myarray[(myarray >= 2) & (myarray < 5)] = 100
>>> myarray
array([[ 1, 100, 100, 100, 5],
[ 1, 100, 100, 100, 5],
[ 1, 100, 100, 100, 5]])

关于python - 在范围之间重新分类python中的numpy数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34733291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com