Porting code for Numpy 1.13+ to get rid of "boolean index did not match indexed array along dimension 1" error
Dear ALL, I am trying to port an eigenalysis function that runs smoothly on Numpy 1.12 but fail miserably on Numpy 1.13 or higher with the dreadful error "boolean index did not match indexed array along dimension 1". Here is a fragment of the code, where the error occurrs: evals, evecs = np.linalg.eig(Syy) idx = evals.argsort()[::-1] evals = np.real(evals[idx]) U = np.real(evecs[:, idx]) evals = evals[evals > tolerance] U = U[:, evals > tolerance] # Here is where the error occurs So, I ask: is there a way out of this? Thanks in advance for any assistance you can provide.
It looks like your code is wrong, and numpy 1.12 happened to let you get
away with it
This line:
evals = evals[evals > tolerance]
Reduces the eigenvalues to only those which are greater than the tolerance
When you do U[:, evals > tolerance], evals > tolerance is just going
to be [True,
True, ...].
You need to swap the last two lines, to
U = U[:, evals > tolerance]
evals = evals[evals > tolerance]
Or better yet, introduce an intermediate variable:
keep = evals > tolerance
evals = evals[keep]
U = U[:, keep]
Eric
On Tue, 12 Feb 2019 at 15:16 Mauro Cavalcanti
Dear ALL,
I am trying to port an eigenalysis function that runs smoothly on Numpy 1.12 but fail miserably on Numpy 1.13 or higher with the dreadful error "boolean index did not match indexed array along dimension 1".
Here is a fragment of the code, where the error occurrs:
evals, evecs = np.linalg.eig(Syy) idx = evals.argsort()[::-1] evals = np.real(evals[idx]) U = np.real(evecs[:, idx]) evals = evals[evals > tolerance] U = U[:, evals > tolerance] # Here is where the error occurs
So, I ask: is there a way out of this?
Thanks in advance for any assistance you can provide. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Eric,
Implementing either of your suggestions (swapping the lines or using an
intermediate variable) worked fine under the latest Numpy (v1.16.1)!
Thanks a lot for your help!
Best regards,
Em ter, 12 de fev de 2019 às 23:06, Eric Wieser
It looks like your code is wrong, and numpy 1.12 happened to let you get away with it
This line:
evals = evals[evals > tolerance]
Reduces the eigenvalues to only those which are greater than the tolerance
When you do U[:, evals > tolerance], evals > tolerance is just going to be [True, True, ...].
You need to swap the last two lines, to
U = U[:, evals > tolerance] evals = evals[evals > tolerance]
Or better yet, introduce an intermediate variable:
keep = evals > tolerance evals = evals[keep] U = U[:, keep]
Eric
On Tue, 12 Feb 2019 at 15:16 Mauro Cavalcanti
wrote: Dear ALL,
I am trying to port an eigenalysis function that runs smoothly on Numpy 1.12 but fail miserably on Numpy 1.13 or higher with the dreadful error "boolean index did not match indexed array along dimension 1".
Here is a fragment of the code, where the error occurrs:
evals, evecs = np.linalg.eig(Syy) idx = evals.argsort()[::-1] evals = np.real(evals[idx]) U = np.real(evecs[:, idx]) evals = evals[evals > tolerance] U = U[:, evals > tolerance] # Here is where the error occurs
So, I ask: is there a way out of this?
Thanks in advance for any assistance you can provide. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
-- Dr. Mauro J. Cavalcanti E-mail: maurobio@gmail.com Web: http://sites.google.com/site/maurobio "Life is complex. It consists of real and imaginary parts."
participants (2)
-
Eric Wieser
-
Mauro Cavalcanti