替换numpy数组值python [英] Replacing numpy array values python
问题描述
我在尝试替换numpy数组中的值时遇到了可怕的时光,遇到了我希望有人能解释的非常奇怪的行为.本质上,我想在遗传算法中进行交叉操作.这是一个简单的例子.我有一个2 X 10的数组,并希望将第1行直到第5列中的所有值与第2行直到第5列中的值交换.这是代码:
I am having a terrible time trying to replace values in a numpy array and running up against a very strange behavior I was hoping someone could explain. Essentially I want to do a crossing over operation in a genetic algorithm. Here's a simple example. I have a 2 X 10 array, and want all the values in row 1 up to column 5 to be swapped with the values in row 2 up to column 5. Here's the code:
z=np.random.uniform(low=0,high=1,size=(2,10))
zcopy = z
print z
[[ 0.77488523 0.39966358 0.63233664 0.77093136 0.04102615 0.98984184
0.43402537 0.0910648 0.28037032 0.76654885]
[ 0.49980878 0.28161905 0.71972029 0.01208004 0.87851569 0.16853681
0.96325992 0.90886083 0.12344231 0.83665396]]
z[1,range(4)] = zcopy[0,range(4)]
print z
[[ 0.77488523 0.39966358 0.63233664 0.77093136 0.04102615 0.98984184
0.43402537 0.0910648 0.28037032 0.76654885]
[ 0.77488523 0.39966358 0.63233664 0.77093136 0.87851569 0.16853681
0.96325992 0.90886083 0.12344231 0.83665396]]
如您所见,它只是将第1行的所有内容复制到两行中.但是,如果我不指定另一个数组的子集,而只是给它说整数,那么它会完美地工作
As you can see it's just copied all of row 1 into both rows. But, if I don't specify a subset of another array but just give it say integers it works perfectly
z[1,range(4)] = range(4)
print z
[[ 0.77488523 0.39966358 0.63233664 0.77093136 0.04102615 0.98984184
0.43402537 0.0910648 0.28037032 0.76654885]
[ 0. 1. 2. 3. 0.87851569 0.16853681
0.96325992 0.90886083 0.12344231 0.83665396]]
我很困惑.有谁知道如何解决这个问题?
I'm rather perplexed. Does anyone have any idea how to work around this?
推荐答案
这里似乎有两个问题.
There seem to be two questions here.
- 为什么
zcopy = z
不进行复制?" - 为什么
z[1,range(4)] = zcopy[0,range(4)]
不交换前四列中的值?"
- "Why doesn't
zcopy = z
make a copy?" - "Why doesn't
z[1,range(4)] = zcopy[0,range(4)]
swap the values in the first four columns?"
第一个问题的答案是,在Python中为变量名称分配值不会产生副本. Python中的变量只是对象的标签;给对象一个新的标签根本不会改变对象本身.如果要复制numpy数组,则可以使用copy
方法,该方法将返回该数组的新副本.如:
The answer to the first question is that assigning a value to a variable name in Python doesn't make a copy. A variable in Python is just a label for an object; giving the object a new label doesn't change the object itself at all. If you want to make a copy of a numpy array, specifically, you can use the copy
method, which returns a new copy of the array. As in:
zcopy = z.copy()
第二个问题的答案是您的代码仅分配一组值:z[1, range(4)]
.如果要更改两行,则必须分配给两行!在许多语言中,您都可以使用tmp
变量来执行此操作,但是python提供了一种无需临时变量即可交换值的绝佳方法:
The answer to the second question is that your code only assigns one set of values: z[1, range(4)]
. If you want to change both rows, you have to assign to both rows! In many languages you'd do this with a tmp
variable, but python provides an elegant way to swap values without needing temporary variables:
>>> z[1, range(4)], z[0, range(4)] = z[0, range(4)], z[1, range(4)]
nye17 的答案比较简洁,但它的作用相同.
nye17's answer is a bit cleaner, but it does the same thing.
这篇关于替换numpy数组值python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!