Posts

Showing posts from February 18, 2019

Why does Python copy numpy arrays where the length of the dimensions are the same?

Image
6 1 I have a problem with referencing to a numpy array. I have an array of the form import numpy as np a = [np.array([0.0, 0.2, 0.4, 0.6, 0.8]), np.array([0.0, 0.2, 0.4, 0.6, 0.8]), np.array([0.0, 0.2, 0.4, 0.6, 0.8])] and if I now create a new variable b = np.array(a) and do b[0] += 1 print(a) then a is not changing. a = [array([0. , 0.2, 0.4, 0.6, 0.8]), array([0. , 0.2, 0.4, 0.6, 0.8]), array([0. , 0.2, 0.4, 0.6, 0.8])] But if I do the same thing with: a = [np.array([0.0, 0.2, 0.4, 0.6, 0.8]), np.array([0.0, 0.2, 0.4, 0.6, 0.8]), np.array([0.0, 0.2, 0.4, 0.6])] so I removed one number in the end of the last dimension. Then I do this again: b = np.array(a) b[0] += 1 print(a) Now a is changing, what I thought is the normal behavior in python....