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]]
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?