Trim boundaries off of array >>> x = np.arange(24).reshape((4, 6)) >>> trim(x, axes={0: 0, 1: 1}) array([[ 1, 2, 3, 4], [ 7, 8, 9, 10], [13, 14, 15, 16], [19, 20, 21, 22]]) >>> trim(x, axes={0: 1, 1: 1}) array([[ 7, 8, 9, 10],
(x, axes=None)
| 149 | |
| 150 | |
| 151 | def trim(x, axes=None): |
| 152 | """Trim boundaries off of array |
| 153 | |
| 154 | >>> x = np.arange(24).reshape((4, 6)) |
| 155 | >>> trim(x, axes={0: 0, 1: 1}) |
| 156 | array([[ 1, 2, 3, 4], |
| 157 | [ 7, 8, 9, 10], |
| 158 | [13, 14, 15, 16], |
| 159 | [19, 20, 21, 22]]) |
| 160 | |
| 161 | >>> trim(x, axes={0: 1, 1: 1}) |
| 162 | array([[ 7, 8, 9, 10], |
| 163 | [13, 14, 15, 16]]) |
| 164 | """ |
| 165 | if isinstance(axes, Integral): |
| 166 | axes = [axes] * x.ndim |
| 167 | if isinstance(axes, dict): |
| 168 | axes = [axes.get(i, 0) for i in range(x.ndim)] |
| 169 | |
| 170 | return x[tuple(slice(ax, -ax if ax else None) for ax in axes)] |
| 171 | |
| 172 | |
| 173 | def topk(a, k, axis, keepdims): |