如何在numpy数组上进行条件数组算术运算? [英] How do I do conditional array arithmetic on a numpy array?

查看:121
本文介绍了如何在numpy数组上进行条件数组算术运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地掌握numpy数组,所以我有一个关于它们的示例问题:

I'm trying to get a better grip on numpy arrays, so I have a sample question to ask about them:

说我有一个名为a的numpy数组.我想对进行操作,使其中的所有小于0的值递增,其余的保持不动.例如,如果我有:

Say I have a numpy array called a. I want to perform an operation on a that increments all the values inside it that are less than 0 and leaves the rest alone. for example, if I had:

a = np.array([1,2,3,-1,-2,-3])

我想返回:

([1,2,3,0,-1,-2])

最紧凑的语法是什么?

谢谢!

推荐答案

In [45]: a = np.array([1,2,3,-1,-2,-3])

In [46]: a[a<0]+=1

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

这篇关于如何在numpy数组上进行条件数组算术运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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