I have a small python script, and I always run into an error:
ValueError: cannot resize an array references or is referenced
by another array in this way. Use the resize function
Code:
points = comp.findall('Points') # comp is a parsed .xml
diffvals = np.arange(10, dtype=float)
diffvals.resize(len(points),8)
But there are two things I do not understand:
I have no idea what I could possibly do to resolve this.
You cannot resize NumPy arrays that share data with another array in-place using the resize
method by default. Instead, you can create a new resized array using the np.resize
function:
np.resize(a, new_shape)
or you can disable reference checking using:
a.resize(new_shape, refcheck=False)
The likely reason you are only seeing it with a debugger is that the debugger references the array to e.g. print it. Also, the debugger may not store references to temporary arrays before you assign them into a variable, which probably explains why the other script works.
Use
a = a.copy()
before resizing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With