TheM00s3 - 1 year ago 85

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 Source

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.]])
```