Skip to content

Add a keepdims argument to linspace #14425

Closed

Description

@eric-wieser

Actually, I'm now wondering if we should add a keepdims=True to linspace, with semantics "replace the axisth axis with the new linspace and squeeze". @mhvk, do you have thoughts on this?

@eric-wieser - you mean something like the opposite of a reduction, where keepdims=True tells linspace that there is already a dimension of 1 inside start and stop? On the one hand, it doesn't quite feel as natural, since in that case one has to check that that dimension is indeed 1. On the other hand, I can see the nice aspect that then start and stop would broadcast with the result. Overall, the "other hand" wins for me...

Originally posted by @mhvk and @eric-wieser in #14209

Some justification for this - it would these to mean the same thing:

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

which is handy for algorithms that already compute the keepdims version of max and min.

Activity

  1. zjpoh commented on Nov 15, 2019

    @zjpoh
    Member

    Hey @eric-wieser, I'm interested to take a look at this.

    Just to make sure I understand the requirements.

    arr = np.arange(4).reshape((2,2))
    ax = 0
    arr_min = arr.min(axis=ax, keepdims=True)  # ndim = 2
    arr_max = arr.max(axis=ax, keepdims=True)  # ndim = 2
    np.linspace(arr_min, arr_max, axis=ax)  # ndim = 3 != 2
    

    So, we basically want to add a keep_dim parameter to np.linspace so that its output has the same dimension as arr_min and arr_max?

  2. zjpoh commented on Dec 11, 2019

    @zjpoh
    Member

    I agree with @efiring that the usage of this new keyword might not very common and so does not worth the complexity it adds.

    In addition, as pointed out by @shoyer, one can achieve the same goal with the following code:

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

    So, I'm closing this PR and the associated issue.

    Please refer to the PR for more comments.

  3. seberg commented on Dec 11, 2019

    @seberg
    Member

    Thanks for the effort ZJ! Sometimes its necessary to propose a solution to get a resolution unfortunately.

  4. zjpoh commented on Dec 11, 2019

    @zjpoh
    Member

    No worries. It's a good learning opportunity for me. 😄

  5. eric-wieser commented on Dec 11, 2019

    @eric-wieser
    MemberAuthor

    Sorry for the wild goose chase - I think the arguments made on the mailing list are pretty reasonable :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions