|
| 1 | +--- |
| 2 | +Title: '.squeeze()' |
| 3 | +Description: 'Removes dimensions of size 1 from an ndarray.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Math' |
| 10 | + - 'Methods' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +Numpy's **`.squeeze()`** is used to remove dimensions of size 1 from an array, returning a reshaped array without those singleton dimensions. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.squeeze(axis=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `axis`: Specifies which axis or axes to squeeze. If set to `None` (default), all dimensions of size 1 are removed. If any specified axis is not of size 1, it raises a `ValueError`. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +Returns a view of the input array with the specified singleton dimensions removed. |
| 32 | + |
| 33 | +## Example 1: Removing All Singleton Dimensions Using `.squeeze()` |
| 34 | + |
| 35 | +In this example, `.squeeze()` is used without the `axis` parameter to remove all dimensions of size 1 from a 3D array: |
| 36 | + |
| 37 | +```py |
| 38 | +import numpy as np |
| 39 | + |
| 40 | +np_array = np.array([[[1, 2, 3], [1, 2, 3]]]) |
| 41 | +print(np_array.shape) # (1, 2, 3) |
| 42 | + |
| 43 | +squeezed_array = np.squeeze(np_array) |
| 44 | +print(squeezed_array.shape) |
| 45 | +``` |
| 46 | + |
| 47 | +The output of this code is: |
| 48 | + |
| 49 | +```shell |
| 50 | +(2, 3) |
| 51 | +``` |
| 52 | + |
| 53 | +## Example 2: Removing a Specific Dimension Using `.squeeze()` |
| 54 | + |
| 55 | +In this example, `.squeeze(axis=0)` removes the first dimension explicitly from an array with shape `(1, 2, 3)`: |
| 56 | + |
| 57 | +```py |
| 58 | +import numpy as np |
| 59 | + |
| 60 | +np_array = np.array([[[1, 2, 3], [1, 2, 3]]]) |
| 61 | +squeezed_array = np.squeeze(np_array, axis=0) |
| 62 | + |
| 63 | +# Output shape after squeezing |
| 64 | +print(squeezed_array.shape) |
| 65 | +print(squeezed_array) |
| 66 | +``` |
| 67 | + |
| 68 | +The output of this code is: |
| 69 | + |
| 70 | +```shell |
| 71 | +(2, 3) |
| 72 | +[[1 2 3] |
| 73 | + [1 2 3]] |
| 74 | +``` |
| 75 | + |
| 76 | +Only axis 0 is removed since it has size 1, resulting in a 2D array. |
| 77 | + |
| 78 | +## Codebyte Example: Removing Multiple Dimensions With a Tuple Using `.squeeze()` |
| 79 | + |
| 80 | +In this example, `.squeeze(axis=(0, 2))` removes both the first and third dimensions from a shape `(1, 3, 1)`: |
| 81 | + |
| 82 | +```codebyte/python |
| 83 | +import numpy as np |
| 84 | +
|
| 85 | +array_of_zeros = np.zeros((1, 3, 1)) |
| 86 | +print(array_of_zeros.shape) |
| 87 | +
|
| 88 | +squeezed = np.squeeze(array_of_zeros, axis=(0, 2)) |
| 89 | +print(squeezed.shape) |
| 90 | +print(squeezed) |
| 91 | +``` |
| 92 | + |
| 93 | +Axes 0 and 2, both of size 1, will be removed, leaving a flat array of shape `(3,)`. |
0 commit comments