`keepdims=True` for argmin/argmx and C-API `PyArray_ArgMaxWithKeepdims`
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
Hi all, The PR https://github.com/numpy/numpy/pull/19211 proposes to extend argmin and argmax with a `keepdims=False` keyword-only argument. This is a standard argument in NumPy, so it is a small API addition. The PR also proposes to add: * `PyArray_ArgMinWithKeepdims` * `PyArray_ArgMaxWithKeepdims` in the C-API. We have barely extended the C-API in a very long time, so if anyone has concerns, we could pull that out again [1]. Otherwise, this should go in soon, and we will have `keepdims` for both of those functions in the next release :). Cheers, Sebastian [1] I do not see this is much of a maintenance concern, since the original function is just a one-line wrapper of the new one. The API is fairly large and it probably is not used much. So it doesn't feel important to add to me. Overally, I just don't have a preference.
![](https://secure.gravatar.com/avatar/d9ac9213ada4a807322f99081296784b.jpg?s=120&d=mm&r=g)
Hi Sebastian, On Wed, Jun 30, 2021, at 18:23, Sebastian Berg wrote:
The PR https://github.com/numpy/numpy/pull/19211 proposes to extend argmin and argmax with a `keepdims=False` keyword-only argument.
This seems consistent with existing APIs, so I'm not concerned. For those wondering, `keepdims` preserves the number of dimensions of the original array in a reduction operation like `sum`: In [1]: X = np.random.random((10, 15)) In [2]: np.sum(X).shape Out[2]: () In [3]: np.sum(X, keepdims=True).shape Out[3]: (1, 1) This is sometimes useful for broadcasting.
The PR also proposes to add:
* `PyArray_ArgMinWithKeepdims` * `PyArray_ArgMaxWithKeepdims`
I am curious whether this is our general pattern for adding keyword argument functionality to functions in the C-API. It seems a bit excessive! Stéfan
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
On Thu, 2021-07-01 at 00:39 -0700, Stefan van der Walt wrote:
Hi Sebastian,
On Wed, Jun 30, 2021, at 18:23, Sebastian Berg wrote:
The PR https://github.com/numpy/numpy/pull/19211 proposes to extend argmin and argmax with a `keepdims=False` keyword-only argument.
This seems consistent with existing APIs, so I'm not concerned.
For those wondering, `keepdims` preserves the number of dimensions of the original array in a reduction operation like `sum`:
In [1]: X = np.random.random((10, 15))
In [2]: np.sum(X).shape Out[2]: ()
In [3]: np.sum(X, keepdims=True).shape Out[3]: (1, 1)
This is sometimes useful for broadcasting.
The PR also proposes to add:
* `PyArray_ArgMinWithKeepdims` * `PyArray_ArgMaxWithKeepdims`
I am curious whether this is our general pattern for adding keyword argument functionality to functions in the C-API. It seems a bit excessive!
True, I am now tending a bit towards delaying this until someone actually asks for it... In most use-cases just using the Python API is likely only a small overhead anyway if done right. I do not think we have a pattern. We do have some functions with the pattern of `With...And...` to allow signatures of different complexity. But very few of this type of python additions ever made it into the C- API. For `Reshape`, `order=` was added by introducing `NewShape`. I have some hope that very long-term, HPy might solve this for us... Cheers, Sebastian
Stéfan _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/2e1cb59230b4e566e6378acc92f65bcb.jpg?s=120&d=mm&r=g)
Hi, So should I remove these new functions from public C-API? Let me know. I will do that. On Thu, 1 Jul, 2021, 10:02 pm Sebastian Berg, <sebastian@sipsolutions.net> wrote:
On Thu, 2021-07-01 at 00:39 -0700, Stefan van der Walt wrote:
Hi Sebastian,
On Wed, Jun 30, 2021, at 18:23, Sebastian Berg wrote:
The PR https://github.com/numpy/numpy/pull/19211 proposes to extend argmin and argmax with a `keepdims=False` keyword-only argument.
This seems consistent with existing APIs, so I'm not concerned.
For those wondering, `keepdims` preserves the number of dimensions of the original array in a reduction operation like `sum`:
In [1]: X = np.random.random((10, 15))
In [2]: np.sum(X).shape Out[2]: ()
In [3]: np.sum(X, keepdims=True).shape Out[3]: (1, 1)
This is sometimes useful for broadcasting.
The PR also proposes to add:
* `PyArray_ArgMinWithKeepdims` * `PyArray_ArgMaxWithKeepdims`
I am curious whether this is our general pattern for adding keyword argument functionality to functions in the C-API. It seems a bit excessive!
True, I am now tending a bit towards delaying this until someone actually asks for it... In most use-cases just using the Python API is likely only a small overhead anyway if done right.
I do not think we have a pattern. We do have some functions with the pattern of `With...And...` to allow signatures of different complexity. But very few of this type of python additions ever made it into the C- API. For `Reshape`, `order=` was added by introducing `NewShape`.
I have some hope that very long-term, HPy might solve this for us...
Cheers,
Sebastian
Stéfan _______________________________________________ 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
![](https://secure.gravatar.com/avatar/2e1cb59230b4e566e6378acc92f65bcb.jpg?s=120&d=mm&r=g)
Hi, I have removed the two new C functions <https://github.com/numpy/numpy/pull/19211/commits/4be86dd0400d4f52fd72dbc312d69942fd5f7c73> from public C-API. Let me know if anything else is needed. Thanks. On Fri, Jul 2, 2021 at 2:10 AM Matti Picus <matti.picus@gmail.com> wrote:
On 1/7/21 7:49 pm, Gagandeep Singh wrote:
Hi,
So should I remove these new functions from public C-API? Let me know. I will do that.
Yes please. If needed we can add them, but once in we cannot remove them.
Matti
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
participants (4)
-
Gagandeep Singh
-
Matti Picus
-
Sebastian Berg
-
Stefan van der Walt