Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Mention that expand_dims and squeeze are inverses #8737

Merged
merged 1 commit into from Apr 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion numpy/core/fromnumeric.py
Expand Up @@ -1162,14 +1162,30 @@ def squeeze(a, axis=None):
dimensions of length 1 removed. This is always `a` itself
or a view into `a`.

Raises
------
ValueError
If `axis` is not `None`, and an axis being squeezed is not of length 1

See Also
--------
expand_dims : The inverse operation, adding singleton dimensions
reshape : Insert, remove, and combine dimensions, and resize existing ones

Examples
--------
>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=(2,)).shape
>>> np.squeeze(x, axis=0).shape
(3, 1)
>>> np.squeeze(x, axis=1).shape
Traceback (most recent call last):
...
ValueError: cannot select an axis to squeeze out which has size not equal to one
>>> np.squeeze(x, axis=2).shape
(1, 3)

"""
Expand Down
2 changes: 2 additions & 0 deletions numpy/lib/shape_base.py
Expand Up @@ -255,6 +255,8 @@ def expand_dims(a, axis):

See Also
--------
squeeze : The inverse operation, removing singleton dimensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, "removes all singleton dimensions".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean here is that np.squeeze(arr, axis=a) and np.expand_dims(arr, axis=a) are inverses. I guess this only applies to one of the two overloads

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also mention reshape in both contexts as it allows adding and removing axis.

Copy link
Member

@seberg seberg Mar 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like indexing a lot for this both of this too, unless it gets very clunky with counting :. EDIT: Though I guess I admit that for squeezing, indexing does not give the information that there really is only one element.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charris: Added comments about reshape

reshape : Insert, remove, and combine dimensions, and resize existing ones
doc.indexing, atleast_1d, atleast_2d, atleast_3d

Examples
Expand Down