0

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.

  • [Why should I not upload images of code/data/errors](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – Alex P Nov 19 '22 at 15:30
  • You need to provide more information: for a start, what version is your numpy? You can check with `print(np.__version__)`. – Mercury Nov 19 '22 at 15:36
  • I have version 1.21.5 – Kathrin_L_A Nov 19 '22 at 15:43
  • 1
    `argmax` docs say `keepdims` was added in v 1.22 – hpaulj Nov 19 '22 at 15:49
  • I tried updating numpy now, but then I get an error saying that quantecon only is compatible with versions of numpy less than 1,21. – Kathrin_L_A Nov 19 '22 at 16:07
  • You can simply do argmax without using keepdims. Then in the next line, just add the dims back in with reshape or expand_dims. – Mercury Nov 19 '22 at 16:18
  • If you understand what `keepdims` is doing for you, you can compensate. After all, we used `argmax` without that parameter for many years. – hpaulj Nov 19 '22 at 16:52
  • I could do that but it would make my life much easier if I had both packages at the same time – Kathrin_L_A Nov 19 '22 at 17:38
  • FYI: The keepdims parameter expects a *boolean* argument. When you write `keepdims=4`, the `4` has no meaning other than being "truthy". I recommend changing that to `keepdims=True` to avoid confusing future readers of your code. – Warren Weckesser Nov 20 '22 at 01:24

1 Answers1

0

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.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214