如何考虑到nan数,有条件地更改numpy数组中的值? [英] How I can i conditionally change the values in a numpy array taking into account nan numbers?
本文介绍了如何考虑到nan数,有条件地更改numpy数组中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数组是一个2D矩阵,除了负值和正值外,它还具有numpy.nan值:
My array is a 2D matrix and it has numpy.nan values besides negative and positive values:
>>> array
array([[ nan, nan, nan, ..., -0.04891211,
nan, nan],
[ nan, nan, nan, ..., nan,
nan, nan],
[ nan, nan, nan, ..., nan,
nan, nan],
...,
[-0.02510989, -0.02520096, -0.02669156, ..., nan,
nan, nan],
[-0.02725595, -0.02715945, -0.0286231 , ..., nan,
nan, nan],
[ nan, nan, nan, ..., nan,
nan, nan]], dtype=float32)
我想用一个数字替换所有正数,用另一个数字替换所有负数.
And I want to replace all the positive numbers with a number and all the negative numbers with another number.
如何使用python/numpy执行该操作?
How can I perform that using python/numpy?
(根据记录,矩阵是地理图像的结果,我要对其进行分类)
(For the record, the matrix is a result of geoimage, which I want to perform a classification)
推荐答案
数组中有np.nan
的事实无关紧要.只需使用花式索引:
The fact that you have np.nan
in your array should not matter. Just use fancy indexing:
x[x>0] = new_value_for_pos
x[x<0] = new_value_for_neg
如果要替换您的np.nans
:
x[np.isnan(x)] = something_not_nan
有关花式索引的更多信息教程和
More info on fancy indexing a tutorial and the NumPy documentation.
这篇关于如何考虑到nan数,有条件地更改numpy数组中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文