# W1A2E2 One Hot Vector

I created the one hot vector but it causes dimension issue. What can be done? Thank you for your time.

You need to check how idx is calculated. How could it have the value of 2354?

1 Like

Thanks for the tips. As I find later, this ravel function tends to make an array 100 times as large:

27 and 2700 are outputs of my commands print(len(y)) and print(len(y.ravel())).

Sorry, but I donâ€™t believe you. Something else is going on. All the ravel() method does is â€śunrollâ€ť or â€śflattenâ€ť an array into a 1D array. I added some instrumentation to my sample function and hereâ€™s what I get:

``````vocab_size = 27
Wax (100, 27) x (27, 1) Waa (100, 100) a_prev (100, 1)
y.shape (27, 1)
len(y) 27
len(y.ravel()) 27
``````

Maybe the real problem is that your y value is the wrong shape. E.g. perhaps it is (27, 100) instead of (27, 1). How could that happen?

One other thing to note: in your earlier post, your initialization of x does not look correct. With that syntax, x will end up being a 1D array. It should be a 2D array. The argument to numpy zeros needs to be â€śtupleâ€ť, right?

The initialization of x should be the reason, as the problem was solved after I wrote np.zeros((n_x,1)) for it. But I still donâ€™t understand why that can cause a *100 effect. Is this explainable?

Did you try printing the shape of your y value in the case that you were seeing the â€ś100 effectâ€ť? Everything in programming is explainable: things donâ€™t just happen randomly or by magic. If you canâ€™t explain it yet, that just means youâ€™re not thinking hard enough.

One thing that may be part of the answer is that you are probably making an incorrect assumption about what the len() function does in python when you apply it to a numpy array. It does not give you the total number of elements in the array: it gives you the number of indices in the first dimension of the array. Watch this:

``````y = np.random.randn(27, 100)
print(y.shape)
print(len(y))
(27, 100)
27
``````

If you want the number of elements in an array, hereâ€™s one way to get that:

``````print(y.size)
2700
``````
2 Likes