user785099 - 8 months ago 48

Python Question

There has a nd array

`A`

`[100,255,255,3]`

`A1 = A[i,:,:,:]`

`A1`

`[255,255,3]`

`[1,255,255,3]`

Answer

Use `np.newaxis`

on your resulting array.

Very simple example on a 2d array:

```
x = np.array([[0, 1], [2, 3]])
x.shape
#: (2, 2)
x[np.newaxis]
#: array([[[0, 1],
#: [2, 3]]])
x[np.newaxis].shape
#: (1, 2, 2)
```

Source (Stackoverflow)