TheM00s3 - 6 months ago 24

Python Question

I have the following list of indices

`[2 4 3 4]`

`targets = np.zeros((features.shape[0], 5))`

`([0,0,1,0,0], [0,0,0,0,1], [0,0,0,1,0], [0,0,0,0,1])`

Answer

I believe you can do something like this:

```
targets = np.zeros((4, 5))
ind = [2, 4, 3, 4]
targets[np.arange(0, 4), ind] = 1
```

Here is the result:

```
array([[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 1.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 1.]])
```