Code Problem

Hello! I’ve dug out some codes from CNN Utils for my own application
I want to use this code but it gave me problems:
Y = np.eye(6)[Labels.reshape(-1)].T
Can somebody help me know what’s going on this code?

hi @Chiang_Yuhan

Are you asking in regards of a specific course?

DLS Specialization CNN Week 1

Y = np.eye(6)[Labels.reshape(-1).T
What exactly is it that you want that code to do for you?

One Hot encoding sir.

Looks to me like it’s creating an eye matrix, so yo have ‘n’ rows (6 in your case, that’s the number of
labels you are using?). Each row has a single ‘1’.

Then it uses a vector expression to copy individual rows of that eye matrix, once for each of your Labels.

Can you say which week of the CNN course you found that? Because there are much easier ways to create a one-hot encoding.

I pulled the code out from cnn.utils file. I have used another method to create the one-hot encoding. Thank you sir.

Which week and assignment?

I believe that it’s W2A2 of the DLS

I believe it’s in C4 W1 A1.

At that point, they had not introduced any packages that do this in one function call.

It works as I described earlier.

1 Like