Remix.run Logo
NoahZuniga a day ago

> # We must reshape X to be a column vector (3,1)

> # or rely on broadcasting rules carefully.

> Z = Y @ X.reshape(3, 1)

Why not use X.transpose()?

fph a day ago | parent | next [-]

Actually, I just tried Y @ X in Numpy and it works just fine.

It's because in Python 1-dimensional arrays are actually a thing, unlike in Matlab. That line of code is a non-example; it is easier to make it work in Python than in Matlab.

ivansavz a day ago | parent [-]

The result of `Y @ X` has shape (3,), so the next line (concatenate as columns) fails.

To make `Z` a column vector, we would need something like `Z = (Y @ X)[:,np.newaxis]`.

Although, I'm not sure why the author is using `concatenate` when the more idiomatic function would be stack, so the change you suggest works and is pretty clean:

   Z = Y @ X
   np.stack([Z, Z], axis=1)
   # array([[14, 14],
   #        [32, 32],
   #        [50, 50]])
with convention that vectors are shape (3,) instead of (3,1).
dgfl a day ago | parent | next [-]

You do realize how many arbitrary words and concepts that are nowhere to be found in “conventional” math there are here, right?

I know what all of these do, but I just can’t shake the feeling that I’m constantly fighting with an actual python. Very aptly named.

I also think it’s more to do with the libraries than with the language, which I actually like the syntax of. But numpy tries to be this highly unopinionated tool that can do everything, and ends up annoying to use anywhere. Matplotlib is even worse, possibly the worst API I’ve ever used.

Aerolfos a day ago | parent | prev [-]

> To make `Z` a column vector, we would need something like `Z = (Y @ X)[:,np.newaxis]`.

Doesn't just (Y @ X)[None] work? None adding an extra dimension works in practice but I don't know if you're "supposed" to do that

ivansavz 21 hours ago | parent [-]

It seems `(Y @ X)[None]` produces a row vector of shape (1,3),

   (Y @ X)[None]
   
   # array([[14, 32, 50]])
   
but `(Y @ X)[None].T` works as you described:

   (Y @ X)[None].T
   
   # array([[14],
   #        [32],
   #        [50]])

I don't know either RE supposed to or not, though I know np.newaxis is an alias for None.
wodenokoto a day ago | parent | prev | next [-]

Isn’t X.T still valid? I believe it’s been phased out in pandas but still around in numpy.

a day ago | parent [-]
[deleted]
boscillator a day ago | parent | prev | next [-]

It doesn't seem to be the example here, but I know that X.transpose() does not work if X is a (3,) and not a (3,1), which is the common way to present vectors in numpy. MATLAB forcing everything to be at least rank-2 solves a bunch of these headaches.

ivansavz a day ago | parent | prev | next [-]

I thought so too, but it doesn't seem to work since X has shape (3,).

This seems to work,

   Z = Y @ X[:,np.newaxis]
thought it is arguably more complicated than calling the `.reshape(3,1)` method.
Aerolfos a day ago | parent | prev | next [-]

> Why not use X.transpose()?

Or just X.T, the shorthand alias for that

a day ago | parent | prev [-]
[deleted]