error using np.argmax when applying keepdims

338 Views Asked by At

I am running my Python code and recieving this error on keepdims:

enter image description here

This is the code:

enter image description here

It worked fine to run this command on my computer a few days ago but I have ran other codes etc after that might have done something.

It works to write keepdims on amax, just not on argmax. My friend ran the same on her computer now, and this error did not show up even though the code were identical. I tried uninstalling and reinstalling anaconda but it dit not change it. Not sure if there is something else I have to download or what has happened.

1

There are 1 best solutions below

0
Warren Weckesser On

For an array x, a simple way to replicate the behavior of np.argmax(x, axis=0, keepdims=True) is np.argmax(x, axis=0)[np.newaxis, ...]. Note that this is specifically for the case axis=0.

Other alternatives include np.expand_dims(np.argmax(x, axis=0), 0) and np.argmax(x, axis=0).reshape((1,) + x.shape[1:]).

For an arbitrary axis k, np.expand_dims(np.argmax(x, axis=k), k) will work.