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

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

问题描述

有人能建议一种简单的方法将数组中的所有负值替换为0吗?

Can anyone advise 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天全站免登陆