在numpy的阵列中的特定列替换值 [英] Replace values in specific columns of a numpy array

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

问题描述

我有一个N×M个numpy的阵列(矩阵)。这里是一个3×5阵列的例子:

I have a N x M numpy array (matrix). Here is an example with a 3 x 5 array:

x = numpy.array([[0,1,2,3,4,5],[0,-1,2,3,-4,-5],[0,-1,-2,-3,4,5]])

我想扫描 X 所有列和替换每个列的值是否相等为特定值。
此code例如旨在替换所有负值(其中值等于列数),以100:

I'd like to scan all the columns of x and replace the values of each column if they are equal to a specific value. This code for example aims to replace all the negative values (where the value is equal to the column number) to 100:

for i in range(1,6):
    x[:,i == -(i)] = 100 

这code得到此警告:

This code obtains this warning:

DeprecationWarning: using a boolean instead of an integer will result in an error in the future

我使用numpy的1.8.2。我如何才能避免降级没有这个numpy的警告?

I'm using numpy 1.8.2. How can I avoid this warning without downgrade numpy?

推荐答案

我不明白你的code正在试图做的事:

I don't follow what your code is trying to do:

我== - (一)

将评估,以这样的:

x[:, True]
x[:, False]

我不认为这是你想要的。你应该尝试这样的事:

I don't think this is what you want. You should try something like this:

for i in range(1, 6):
    mask = x[:, i] == -i
    x[:, i][mask] = 100

在创建整列口罩,并用它来改变值。

Create a mask over the whole column, and use that to change the values.

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

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