Baffled by numpy's transpose - numpy

Let's take a very simple case: an array with shape (2,3,4), ignoring the values.
>>> a.shape
(2, 3, 4)
When we transpose it and print the dimensions:
>>> a.transpose([1,2,0]).shape
(3, 4, 2)
So I'm saying: take axis index 2 and make it the first, then take axis index 0 and make it the second and finally take axis index 1 and make it the third. I should get (4,2,3), right?
Well, I thought perhaps I don't understand the logic fully. So I read the documentation and its says:
Use transpose(a, argsort(axes)) to invert the transposition of tensors
when using the axes keyword argument.
So I tried
>>> c = np.transpose(a, [1,2,0])
>>> c.shape
(3, 4, 2)
>>> np.transpose(a, np.argsort([1,2,0])).shape
(4, 2, 3)
and got yet a completely different shape!
Could someone please explain this? Thanks.

In [259]: a = np.zeros((2,3,4))
In [260]: idx = [1,2,0]
In [261]: a.transpose(idx).shape
Out[261]: (3, 4, 2)
What this has done is take a.shape[1] dimension and put it first. a.shape[2] is 2nd, and a.shape[0] third:
In [262]: np.array(a.shape)[idx]
Out[262]: array([3, 4, 2])
transpose without parameter is a complete reversal of the axis order. It's an extension of the familiar 2d transpose (rows become columns, columns become rows):
In [263]: a.transpose().shape
Out[263]: (4, 3, 2)
In [264]: a.transpose(2,1,0).shape
Out[264]: (4, 3, 2)
And the do-nothing transpose:
In [265]: a.transpose(0,1,2).shape
Out[265]: (2, 3, 4)
You have an initial axes order and final one; describing swap can be hard to visualize if you don't regularly work with lists of size 3 or larger.
Some people find it easier to use swapaxes, which changes the order of just axes. rollaxis is yet another way.
I prefer to use transpose since it can do anything the others can; so I just have to develop an intuitive for one tool.
The argsort comment operates this way:
In [278]: a.transpose(idx).transpose(np.argsort(idx)).shape
Out[278]: (2, 3, 4)
That is, apply it to the result of one transpose to get back the original order.

np.argsort([1,2,0]) returns an array like [2,0,1]
So
np.transpose(a, np.argsort([1,2,0])).shape
act like
np.transpose(a, [2,0,1]).shape
not
np.transpose(a, [1,2,0]).shape

Related

Rearranging numpy arrays

I was not able to find a duplicate of my question, unfortunately, although I am sure that this is a problem which has been solved before
I have a numpy array with a certain set of indices, eg.
ind1 = np.array([1, 3, 5, 7])
With these indices, I can filter some values from another array. Lets call this other array rows. As an example, I can retrieve
rows[ind1] = [1, 10, 20, 15]
The order of rows[ind1] must not be changed in the following.
I have another index array, ind2
ind2 = np.array([4, 5, 6, 7])
I also have an array cols, where I can filter values from using ind2. I know that cols[ind2] results in an array which has the size of rows[ind1] and the entries are the same, but the order is different. An example:
cols[ind2] = [15, 20, 10, 1]
I would like to rearrange the order of cols[ind2], so that it corresponds to rows[ind1]. I am interested in the corresponding order of ind2.
In the example, the result should be
cols[ind2] = [1, 10, 20, 15]
ind2 = [7, 6, 5, 4]
Using numpy, I did not find a way to do this. Any ideas would be helpful. Thanks in advance.
There may be a better way, but you can do this using argsorts.
Let's call your "reordered ind2" ind3.
If you are sure that rows[ind1] and cols[ind2] will have the same length and all of the same elements, then the sorted versions of both will be the same i.e np.sort(rows[ind1]) = np.sort(cols[ind2]).
If this is the case, and you don't run into any problems with repeated elements (unsure of your exact use case), then what you can do is find the indices to put cols[ind2] in order, and then from there, find the indices to put np.sort(cols[ind2]) into the order of rows[ind1].
So, if
p1 = np.argsort(rows[ind1])
and
p2 = np.argsort(cols[ind2])
and
p3 = np.argsort(p1)
Then
ind3 = ind2[p2][p3]. The reason this works is because if you do an argsort of an argsort, it gives you the indices you need to reverse the first sort. p2 sorts cols[ind2] (that's the definition of argsort), and p3 unsorts the result of that back into the order of rows[ind1].

Can einsum be used to reshape operand?

I trying to modify the following code snippet to not use reshape
a = np.random.randn(1, 2, 3, 5)
b = np.random.randn(2, 5, 10)
np.einsum("ijkl,mjl->kim", a, b.reshape(10,2,5))
At first I thought that the reshape is just transposing the operand, but it seems more complicated than that. Is it possible to do this operation without reshaping?

How to conveniently use operations on numpy fortran contiguos arrays?

Some numpy functions like np.matmul(a, b) have convenient behavior for stacks of matrices.
The manual states:
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
Thus, for a.shape = (10 , 2, 4) and b.shape(10, 4, 2) the statementa # b is meaningful and will have shape (10, 2, 2)
However, I'm coming from the linear algebra world, where I'm used to a Fortran contiguous array layout.
The same a represented as a Fortran contiguous array would have shape (4, 2, 10) and similarly b.shape = (2, 4, 10).
To do a # b as before I would have to invoke
(a.T # b.T).T .
Even worse, assume you naively created the same Fortran-contiguous array a with the behavior of matmul in mind, such that it has shape (10, 4, 2).
Then a.strides = (8, 80, 320) with the smallest stride in the 'stack' index, which actually should have highest stride.
Is this really the way to go or am I missing something?
While numpy can handle all sorts of layouts, many details are designed with the "C" layout in mind. Good examples are how nested lists translate into arrays, and the way numpy operations batch excess dimensions as in the matmul case.
It is correct that results in numpy as a rule of thumb do not depend on array layout (FORTRAN,C,non-contiguous); speed, however, certainly does and heavily so:
rng = np.random.default_rng()
a = rng.random((100,111,200))
b = rng.random((111,77,200))
af = np.array(a,order="F")
bf = np.array(b,order="F")
np.allclose((b.T#a.T).T,(bf.T#af.T).T)
# True
timeit(lambda:(b.T#a.T).T,number=10)
# 5.972857117187232
timeit(lambda:(bf.T#af.T).T,number=10)
# 0.1994628761895001
In fact, sometimes it is totally worth it to non-lazily transpose, i.e. copy your data into the best layout:
timeit(lambda:(np.array(b.T,order="C")#np.array(a.T,order="C")).T,number=10)
# 0.3931349152699113
My advice: If you want speed and convenience it is probably best to go with the "C" layout, it doesn't take all that long to get used to and saves you a lot of potential headaches.
numpy's matrix multiplication works regardless of the internal layout of the array. For example, here are two C-ordered arrays:
>>> import numpy as np
>>> a = np.random.rand(10, 2, 4)
>>> b = np.random.rand(10, 4, 2)
>>> print('a', a.shape, a.strides)
>>> print('b', b.shape, b.strides)
a (10, 2, 4) (64, 32, 8)
b (10, 4, 2) (64, 16, 8)
Here are the equivalent arrays in Fortran order:
>>> af = np.asfortranarray(a)
>>> bf = np.asfortranarray(b)
>>> print('af', af.shape, af.strides)
>>> print('bf', bf.shape, bf.strides)
af (10, 2, 4) (8, 80, 160)
bf (10, 4, 2) (8, 80, 320)
Numpy treats equivalent arrays as equivalent, regardless of their internal layout:
>>> np.allclose(a, af) and np.allclose(b, bf)
True
The results of a matrix multiplication do not depend on the internal layout:
>>> np.allclose(a # b, af # bf)
True
and you can even mix layouts if you wish:
>>> np.allclose(a # bf, af # b)
True
In short, the most convenient way to use Fortran-ordered arrays in numpy is to not worry about internal array layout: the shape is all that matters.
If your array shapes differ from what is expected by the numpy matmul API, your best bet is to reshape the arrays, for example using a.transpose(2, 0, 1) # b.transpose(2, 0, 1) or similar, depending on what is appropriate for your use-case, but don't worry: for C or Fortran contiguous arrays, this operation only adjusts the metadata around the array view, it does not cause the underlying data buffer to be copied or re-ordered.

Getting ND behaviour of `np.dot` with `np.tensortdot` or 2-D only `np.dot`

I'm trying to express the N-D behaviour of np.dot using only 2-D np.dot or np.tensordot.
To recap, np.dot does something like the following for N-D: It matches/broadcasts the arrays along all dimensions but the last two and performs dot products for all of them. For example, if x.shape is (2, 3, 4, 5) and y.shape is (2, 3, 5, 4), np.dot(x, y).shape is (2, 3, 4, 4) and np.dot(x, y)[i, j] is np.dot(x[i, j], y[i, j]).
Also, if x.shape is just (4, 5), it will first be converted to (2, 3, 5, 4) via np.broadcast.
I tried np.tensortdot(x, y, axes=(-1, -2)) but it repeats along every dimension of x, y instead of matching them up.
I realise I could write a loop but I was looking for a vectorised solution.
You got the broadcasting behavior of np.dot wrong:
In [254]: x=np.ones((2,3,4,5)); y=np.ones((2,3,5,4))
In [255]: np.dot(x,y).shape
Out[255]: (2, 3, 4, 2, 3, 4)
In [256]: np.matmul(x,y).shape
Out[256]: (2, 3, 4, 4)
and for the (4,5) x:
In [257]: np.dot(x[0,0],y).shape
Out[257]: (4, 2, 3, 4)
In [258]: np.matmul(x[0,0],y).shape
Out[258]: (2, 3, 4, 4)
matmul was added precisely because np.dot does not act like it is performing np.dot(x[i,j,:,:], y[i,j,:,:]) for all i,j.
The shape in Out[255] is the shape of x minus the 5, plus the shape of y minus its 5. In effect an outer produce of everything with summing on the size 5 dimension.
tensordot uses np.dot. It just reshapes and transposes the inputs to reduce the problem to a 2d dot one. Then it massages the result back to the desired shape and order.
In [259]: np.tensordot(x, y, axes=(-1,-2)).shape
Out[259]: (2, 3, 4, 2, 3, 4) # cf Out[255]
In [261]: np.einsum('ijkl,ijlm->ijkm',x,y).shape
Out[261]: (2, 3, 4, 4) # cf Out[256]
Since sparse matrices are 2d to start with - and end with, I don't understand your question. If you have multiple sparse matrices, you'll have to work with them individually.

In numpy, how can I transform a one-line matrix as an array?

I have a matrix m = np.matrix([[1, 2, 3], [4, 5, 6]]).
I extract a vector v = m[0] + m[1], so that now v == [[5, 7, 9]]. The vector's shape is (1, 3), meaning it's considered a matrix, not a vector. How can I make v an actual vector, i.e something of shape (3,)?
I tried to use np.asarray(v) and np.array(v) but they don't do what I want.
I guess the shortest would be
m.A.sum(0)
# array([5, 7, 9])
This converts to array before summing.
If you are starting from an 1xN matrix like v:
v.A1
# array([5, 7, 9])
Use np.squeeze(np.asarray(v)). So you first convert to an array (which other than matrices can have arbitrary n dimensions), then get rid of the extra dimension.
...or avoid using np.matrix in the first place, sparing you the extra step.