I have an array of 60,000 numbers from 0-9:
In [1]: trainY
Out[1]:
array([[5],
[0],
[4],
...,
[5],
[6],
[8]], dtype=int8)
And I have a function to transform each element in trainY into a 10 element vector as per below:
0 -> [1,0,0,0,0,0,0,0,0,0]
1 -> [0,1,0,0,0,0,0,0,0,0]
2 -> [0,0,1,0,0,0,0,0,0,0]
3 -> [0,0,0,1,0,0,0,0,0,0]
...
9 -> [0,0,0,0,0,0,0,0,0,1]
The function:
def transform_y(y):
new_y = np.zeros(10)
new_y[y] = 1
return new_y
My code only works 1 element at a time. What's the best way to transform my trainY array all at once (other than a for loop)? Should I use map? Can someone also show me how to re-write the function so that's it's vectorised?
Thank you.