ValueError resizing an ndarray

Question:

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:

  1. I only get this error when I use debugger.
  2. I have another script with identical code, and everything works fine. I checked this with debugger, all values, data types and so on are identical (except the memory addresses of course)

I have no idea what I could possibly do to resolve this.

Asked By: PKlumpp

||

Answers:

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.

Answered By: otus

Use

a = a.copy()

before resizing

Answered By: Shahnawaz Akhtar

maybe the bugger references the array to other place,and there is nothing while the code is running.you can change the code to:

diffvals.resize((len(points),8), refcheck = False)

when refcheck = False,when the fuction–resize works, reference count will not be checked, and then it is successful.

Answered By: thesoch
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.