How I can i conditionally change the values in a numpy array taking into account nan numbers?

£可爱£侵袭症+ 提交于 2019-12-03 00:46:31

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

If you want to replace your np.nans:

x[np.isnan(x)] = something_not_nan

More info on fancy indexing a tutorial and the NumPy documentation.

Try:

a[a>0] = 1
a[a<0] = -1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!