Aj Langley - 4 years ago 84

Python Question

I have a matrix that I initialized with

`numpy.random.uniform`

`W = np.random.uniform(-1, 1, (V,N))`

In my case,

`V = 10000`

`N = 50`

`x`

When I multiply W by a one hot vector

`x_vec`

`W.T.dot(x_vec)`

`W[x].T`

`W[x,:].T`

Can anyone explain to me why these two expression return different shapes and if it's possible to return a (50,1) matrix (vector) with the indexing method. The vector of shape (50,) is problematic because it doesn't behave the same way as the (50,1) vector when I multiply it with other matrices, but I'd like to use indexing to speed things up a little.

*Sorry in advance if this question should be in a place like Cross Validated instead of Stack Exchange

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

They are different operations. matrix (in the maths sense) times matrix gives matrix, some of your matrices just happen to have width 1.
Indexing with an integer scalar eats the dimension you are indexing into. Once you are down to a single dimension, `.T`

does nothing because it doesn't have enough axes to shuffle.

If you want to go from (50,) to (50, 1) shape-wise, the recipe is indexing with `None`

like so `v[:, None]`

. In your case you have at least two one-line options:

```
W[x, :][:, None] # or W[x][:, None] or
W[x:x+1, :].T # or W[x:x+1].T
```

The second-line option preserves the first dimension of `W`

by requesting a subrange of length one. The first option can be contracted into a single indexing operation - thanks to @hpaulj for pointing this out - which gives the arguably most readable option:

```
W[x, :, None]
```

The first index (scalar integer `x`

) consumes the first dimension of `W`

, the second dimension (unaffected by `:`

) becomes the first and `None`

creates a new dimension on the right.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**