Remove axes of length one from `a`. Parameters ---------- a : array_like Input data. axis : None or int or tuple of ints, optional .. versionadded:: 1.7.0 Selects a subset of the entries of length one in the shape. If an axis is selected with sh
(a, axis=None)
| 1490 | |
| 1491 | @array_function_dispatch(_squeeze_dispatcher) |
| 1492 | def squeeze(a, axis=None): |
| 1493 | """ |
| 1494 | Remove axes of length one from `a`. |
| 1495 | |
| 1496 | Parameters |
| 1497 | ---------- |
| 1498 | a : array_like |
| 1499 | Input data. |
| 1500 | axis : None or int or tuple of ints, optional |
| 1501 | .. versionadded:: 1.7.0 |
| 1502 | |
| 1503 | Selects a subset of the entries of length one in the |
| 1504 | shape. If an axis is selected with shape entry greater than |
| 1505 | one, an error is raised. |
| 1506 | |
| 1507 | Returns |
| 1508 | ------- |
| 1509 | squeezed : ndarray |
| 1510 | The input array, but with all or a subset of the |
| 1511 | dimensions of length 1 removed. This is always `a` itself |
| 1512 | or a view into `a`. Note that if all axes are squeezed, |
| 1513 | the result is a 0d array and not a scalar. |
| 1514 | |
| 1515 | Raises |
| 1516 | ------ |
| 1517 | ValueError |
| 1518 | If `axis` is not None, and an axis being squeezed is not of length 1 |
| 1519 | |
| 1520 | See Also |
| 1521 | -------- |
| 1522 | expand_dims : The inverse operation, adding entries of length one |
| 1523 | reshape : Insert, remove, and combine dimensions, and resize existing ones |
| 1524 | |
| 1525 | Examples |
| 1526 | -------- |
| 1527 | >>> x = np.array([[[0], [1], [2]]]) |
| 1528 | >>> x.shape |
| 1529 | (1, 3, 1) |
| 1530 | >>> np.squeeze(x).shape |
| 1531 | (3,) |
| 1532 | >>> np.squeeze(x, axis=0).shape |
| 1533 | (3, 1) |
| 1534 | >>> np.squeeze(x, axis=1).shape |
| 1535 | Traceback (most recent call last): |
| 1536 | ... |
| 1537 | ValueError: cannot select an axis to squeeze out which has size not equal to one |
| 1538 | >>> np.squeeze(x, axis=2).shape |
| 1539 | (1, 3) |
| 1540 | >>> x = np.array([[1234]]) |
| 1541 | >>> x.shape |
| 1542 | (1, 1) |
| 1543 | >>> np.squeeze(x) |
| 1544 | array(1234) # 0d array |
| 1545 | >>> np.squeeze(x).shape |
| 1546 | () |
| 1547 | >>> np.squeeze(x)[()] |
| 1548 | 1234 |
| 1549 |