替换 numpy 数组中的负值 [英] Replace negative values in an numpy array

查看:66
本文介绍了替换 numpy 数组中的负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以用 0 替换数组中的所有负值?

Is there a simple way of replacing all negative values in an array with 0?

关于如何使用 NumPy 数组进行操作,我有一个完整的模块.

I'm having a complete block on how to do it using a NumPy array.

例如

a = array([1, 2, 3, -4, 5])

我要回来

[1, 2, 3, 0, 5]

a <0 给出:

[False, False, False, True, False]

这就是我卡住的地方 - 如何使用这个数组来修改原始数组.

This is where I'm stuck - how to use this array to modify the original array.

推荐答案

您已经成功了一半.试试:

You are halfway there. Try:

In [4]: a[a < 0] = 0

In [5]: a
Out[5]: array([1, 2, 3, 0, 5])

这篇关于替换 numpy 数组中的负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆