gpt4 book ai didi

python - numpy where() 函数只工作一次

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:35 25 4
gpt4 key购买 nike

import numpy as np
myList = [[1, 2, 3, 4],[3, 4, 5, 6], [5, 6, 7, 8]]
arr = np.array(myList, dtype='float')
arr[1,1] = np.nan
arr[1,2] = np.inf
arr = np.where(arr==np.inf, -1, arr)
arr = np.where(arr==np.nan, -1, arr)
print(arr)

当我尝试替换 nan 和 inf 时,只有第一个 where 函数起作用。如果它们被切换,nan 将代替 inf 工作

最佳答案

你不能检查 == np.nan,因为 NaN 不等于 NaN,事实上:

>>> np.nan == np.nan
False

您可以使用 isnan(..) function [numpy-doc]为此:

arr = np.where(<b>np.isnan(</b>arr<b>)</b>, -1, arr)

关于python - numpy where() 函数只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58574860/

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