Python: Are numpy arrays linked when one array is transposed?

I am currently working on a python script that extracts measurement data from a text file. I am working with iPython Notebook and Python 2.7

Now I am faced with some odd behavior when working with numpy arrays. I have no explanation.

myArray = numpy.zeros((4,3))
myArrayTransposed = myArray.transpose()

for i in range(0,4):
    for j in range(0,3):
        myArray[i][j] = i+j

print myArray
print myArrayTransposed

leads to:

[[ 0.  1.  2.]
 [ 1.  2.  3.]
 [ 2.  3.  4.]
 [ 3.  4.  5.]]
 [[ 0.  1.  2.  3.]
 [ 1.  2.  3.  4.]
 [ 2.  3.  4.  5.]]

Thus, without working with the transposed array, the values ​​are updated in this array.

How is this possible?

+4
source share
1 answer

From http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html :

ndarrays , , ndarray, . , ndarray "" ndarray, , , "" ndarray. ndarrays , Python, .

transpose(), "" ndarray. , :

, N- . Numpy , ndarray .

ndarray, numpy.array():

myArrayTransposed = myArray.transpose().copy()
+4

Source: https://habr.com/ru/post/1625487/


All Articles