Adding keepdims to linspace / logspace / geomspace

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding keepdims to linspace / logspace / geomspace

Zijie Poh
Hi all,

We've created a PR (#14922) on adding keepdims to linspace / logspace / geomspace, which enables linspace to directly take the output of min and max with keepdims = True as the start and stop arguments. That is, the following two linspace calls return the same result.

np.linspace(
    arr.min(axis=ax), 
    arr.max(axis=ax), 
    axis=ax
)

np.linspace(
    arr.min(axis=ax, keepdims=True), 
    arr.max(axis=ax, keepdims=True), 
    axis=ax, keepdims=True
)

Please let me know if you have any questions / suggestions.

Regards,
ZJ

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Adding keepdims to linspace / logspace / geomspace

Sebastian Berg
On Tue, 2019-12-10 at 10:43 -0800, Zijie Poh wrote:

> Hi all,
>
> We've created a PR (#14922) on
> adding keepdims to linspace / logspace / geomspace, which
> enables linspace to directly take the output
> of min and max with keepdims = True as the start and stop arguments.
> That is, the following two linspace calls return the same result.
>
> np.linspace(
>     arr.min(axis=ax),
>     arr.max(axis=ax),
>     axis=ax
> )
>
> np.linspace(
>     arr.min(axis=ax, keepdims=True),
>     arr.max(axis=ax, keepdims=True),
>     axis=ax, keepdims=True
> )
>
I am a bit hesitant about the name `keepdims` being the best one. I
realize it is nice to have the pattern to use the same name for
symmetry. But on the other hand, there is no axes being "kept" here. In
fact, `keepdims=True` returns fewer dims than `keepdims=False` :).

Not sure I have a better idea though, `expand_axis` (or axis) might be
closer to what happens?

`keepdims` is currently used entirely for reduction-like operations
(including complex reduce-like behaviour in `percentile`). However, the
closest to an opposite of reduce-like operations are maybe broadcasts
(they expand axis), but I cannot think of a way to use that for a
parameter name ;).

The change itself is small enough and I am good with adding it, I have
some doubts it will be used much. But it is like a very natural thing
to  give input with the same number of dimensions/axis as the output
will have.

Maybe we should have had `new_axis=` and `expand_axis=` and you can
only use one ;).

- Sebastian


> Please let me know if you have any questions / suggestions.
>
> Regards,
> ZJ
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Adding keepdims to linspace / logspace / geomspace

Stephan Hoyer-2
I'm not sure I understand the motivation here. Is the idea that you want to reuse min/max values computed with keepdims that you also want to use for other purposes?

If so, this could be achieved with squeeze(), e.g.,

np.linspace(
    arr.min(axis=ax, keepdims=True).squeeze(ax), 
    arr.max(axis=ax, keepdims=True).squeeze(ax), 
    axis=ax,
)

This is not the most elegant code, but it seems better than adding a new keyword argument, and the resulting code is likely about as efficient.


On Tue, Dec 10, 2019 at 11:54 AM Sebastian Berg <[hidden email]> wrote:
On Tue, 2019-12-10 at 10:43 -0800, Zijie Poh wrote:
> Hi all,
>
> We've created a PR (#14922) on
> adding keepdims to linspace / logspace / geomspace, which
> enables linspace to directly take the output
> of min and max with keepdims = True as the start and stop arguments.
> That is, the following two linspace calls return the same result.
>
> np.linspace(
>     arr.min(axis=ax),
>     arr.max(axis=ax),
>     axis=ax
> )
>
> np.linspace(
>     arr.min(axis=ax, keepdims=True),
>     arr.max(axis=ax, keepdims=True),
>     axis=ax, keepdims=True
> )
>

I am a bit hesitant about the name `keepdims` being the best one. I
realize it is nice to have the pattern to use the same name for
symmetry. But on the other hand, there is no axes being "kept" here. In
fact, `keepdims=True` returns fewer dims than `keepdims=False` :).

Not sure I have a better idea though, `expand_axis` (or axis) might be
closer to what happens?

`keepdims` is currently used entirely for reduction-like operations
(including complex reduce-like behaviour in `percentile`). However, the
closest to an opposite of reduce-like operations are maybe broadcasts
(they expand axis), but I cannot think of a way to use that for a
parameter name ;).

The change itself is small enough and I am good with adding it, I have
some doubts it will be used much. But it is like a very natural thing
to  give input with the same number of dimensions/axis as the output
will have.

Maybe we should have had `new_axis=` and `expand_axis=` and you can
only use one ;).

- Sebastian


> Please let me know if you have any questions / suggestions.
>
> Regards,
> ZJ
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Adding keepdims to linspace / logspace / geomspace

Eric Firing
In reply to this post by Zijie Poh
I wasn't sure whether the discussion would be on the list or on Github,
so I posted to the latter:

https://github.com/numpy/numpy/pull/14922#issuecomment-564211192

Eric

On 2019/12/10 8:43 AM, Zijie Poh wrote:

> Hi all,
>
> We've created a PR (#14922 <https://github.com/numpy/numpy/pull/14922>)
> on adding keepdims to linspace / logspace / geomspace, which enables
> linspace to directly take the output of min and max with keepdims =
> True as the start and stop arguments. That is, the following two
> linspace calls return the same result.
>
> np.linspace(
>      arr.min(axis=ax),
>      arr.max(axis=ax),
>      axis=ax
> )
>
> np.linspace(
>      arr.min(axis=ax, keepdims=True),
>      arr.max(axis=ax, keepdims=True),
>      axis=ax, keepdims=True
> )
>
> Please let me know if you have any questions / suggestions.
>
> Regards,
> ZJ
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
>

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Adding keepdims to linspace / logspace / geomspace

Zijie Poh
I agree with Stephan and Eric especially it when can be done with existing method. I'll add it to the PR and close it. Thanks for the suggestions / comments!

Regards,
ZJ

On Tue, Dec 10, 2019 at 2:05 PM Eric Firing <[hidden email]> wrote:
I wasn't sure whether the discussion would be on the list or on Github,
so I posted to the latter:

https://github.com/numpy/numpy/pull/14922#issuecomment-564211192

Eric

On 2019/12/10 8:43 AM, Zijie Poh wrote:
> Hi all,
>
> We've created a PR (#14922 <https://github.com/numpy/numpy/pull/14922>)
> on adding keepdims to linspace / logspace / geomspace, which enables
> linspace to directly take the output of min and max with keepdims =
> True as the start and stop arguments. That is, the following two
> linspace calls return the same result.
>
> np.linspace(
>      arr.min(axis=ax),
>      arr.max(axis=ax),
>      axis=ax
> )
>
> np.linspace(
>      arr.min(axis=ax, keepdims=True),
>      arr.max(axis=ax, keepdims=True),
>      axis=ax, keepdims=True
> )
>
> Please let me know if you have any questions / suggestions.
>
> Regards,
> ZJ
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
>

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion