 Aj Langley - 4 years ago 84
Python Question

# Why does indexing a matrix by an integer produce a different shape than the dot product with a one hot vector in numpy?

I have a matrix that I initialized with

`numpy.random.uniform`
like so:

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

In my case,
`V = 10000`
and
`N = 50`
,
`x`
is a positive integer

When I multiply W by a one hot vector
`x_vec`
of dimension V X 1, like
`W.T.dot(x_vec)`
, I get a column vector with a shape of (50,1). When I try to get the same vector by indexing W, as in
`W[x].T`
or
`W[x,:].T`
I get shape (50,).

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 Paul Panzer
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