Preparing the y Vectors Using One-Hot Encoding (if Necessary) As explained in lectures, if the number of dasses K = 2, then we will only use a single output node and the y vector will remain as it is. However, it 3. then we will have K output nodes and we will have to reformat our y vector(s) using one-hot-encoding Given a column vector of labels y with ascending labels starting from 0, the one-hot-encoded vector ŷ corresponding to y is a vector with the same number of rows as y and K columns in which the value of each row in y specifies the index of the column in 9 of the same row that should be set to 1 All other values are zeros. This can either be done in vectorized format with numpy Indexing magic, or a loop If you want to Up to you. Since we'l only ever do this operation a very small number of times, it isn't critical that it be vectorized Go ahead and complete the function below to produce the matrix yhat corresponding to a given y as per the spec desaibed [2 marks]
In [15]: def onehotenc(y): #y - a column vector with one Label per row, assumed to contain ascending Labels from 0 up to X. K - len(np.unique(y)) #You will need this. Don't change. Number of classes K. yhat - np.array([e]) #FILL I CODE BELOW: IM You need to set the variable yhat correctly yhat - np.zeros((y size, k)) Code to set the variable yhat correctly. I would supgest first setting the dimensions of what correctly and the proceeding yhat[np.arange(y size), y] - 1 STOP FILLING IN HERE return yhat.astype("int")
Preparing the y Vectors Using One-Hot Encoding (if Necessary) As explained in lectures, if the number of dasses K = 2, t
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Preparing the y Vectors Using One-Hot Encoding (if Necessary) As explained in lectures, if the number of dasses K = 2, t
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!