Home > OS >  Understand numpy's transpose
Understand numpy's transpose

Time:05-10

I have the following python code

import numpy as np
import itertools as it

ref_list = [0, 1, 2]
p = it.permutations(ref_list)
transpose_list = tuple(p)

#print('transpose_list', transpose_list)

na = nb = nc = 2


A = np.zeros((na,nb,nc))
n = 1
for la in range(na):
    for lb in range(nb):
        for lc in range(nc):
            A[la,lb,lc] = n
            n = n   1   


factor_list = [(i 1)*0.0 for i in range(6)]
factor_list[0] = 0.1
factor_list[1] = 0.2
factor_list[2] = 0.3
factor_list[3] = 0.4





sum_A = np.zeros((na,nb,nc))
for m, t in enumerate(transpose_list):
    if abs(factor_list[m]) < 1.e-3:
        continue 
    factor_list[m]  * np.transpose(A, transpose_list[m]) 

    print('inter', m, t, factor_list[m], np.transpose(A, transpose_list[m])[0,0,1] )

B = np.transpose(A, (0, 2, 1))
C = np.transpose(A, (1, 2, 0))

for la in range(na):
    for lb in range(nb):
        for lc in range(nc):
            print(la,lb,lc,'A',A[la,lb,lc],'B',B[la,lb,lc],'C',C[la,lb,lc])

The result is

inter 0 (0, 1, 2) 0.1 2.0
inter 1 (0, 2, 1) 0.2 3.0
inter 2 (1, 0, 2) 0.3 2.0
inter 3 (1, 2, 0) 0.4 5.0
0 0 0 A 1.0 B 1.0 C 1.0
0 0 1 A 2.0 B 3.0 C 5.0
0 1 0 A 3.0 B 2.0 C 2.0
0 1 1 A 4.0 B 4.0 C 6.0
1 0 0 A 5.0 B 5.0 C 3.0
1 0 1 A 6.0 B 7.0 C 7.0
1 1 0 A 7.0 B 6.0 C 4.0
1 1 1 A 8.0 B 8.0 C 8.0

My question is, why the inter 1 and inter 3 get 3.0 and 5.0? The objective is to obtain P(A)[0,0,1].

For inter 1, it is (0, 2, 1), I thought about (0,2,1) on [0,0,1] -> [0,1,0]

For inter 3, it is (1, 2, 0), I thought about (1,2,0) on [0,0,1] -> [0,1,0]

So the value should be the same. The output are not the same (3.0 and 5.0). So apparently I misunderstood np.transpose. What would be the correct understanding that what happened inside np.transpose?

More specifically, from How does numpy.transpose work for this example?, Anand S Kumar's answer I tried to think from both (0, 2, 1) and (1, 2, 0), both lead to

(0,0,0) -> (0,0,0)
(0,0,1) -> (0,1,0)

I guess it related to the inverse of permutation. But I am not sure why.

CodePudding user response:

A more direct way of making your A:

In [29]: A = np.arange(1,9).reshape(2,2,2)
In [30]: A
Out[30]: 
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])

The transposes:

In [31]: B = np.transpose(A, (0, 2, 1))
    ...: C = np.transpose(A, (1, 2, 0))
In [32]: B
Out[32]: 
array([[[1, 3],
        [2, 4]],

       [[5, 7],
        [6, 8]]])
In [33]: C
Out[33]: 
array([[[1, 5],
        [2, 6]],

       [[3, 7],
        [4, 8]]])

two of the cases:

In [35]: A[0,0,1], B[0,1,0],C[0,1,0]
Out[35]: (2, 2, 2)
In [36]: A[1,0,0], B[1,0,0], C[0,0,1]
Out[36]: (5, 5, 5)

It's eash to match A and B, by just swaping the last 2 indices. It's tempting to just swap the 1st and 3rd for C, but that's wrong. When the 1st is moved to the end, the others shift over without changing their order:

In [38]: for la in range(na):
    ...:     for lb in range(nb):
    ...:         for lc in range(nc):
    ...:             print(la,lb,lc,'A',A[la,lb,lc],'B',B[la,lc,lb],'C',C[lb,lc,la])

0 0 0 A 1 B 1 C 1
0 0 1 A 2 B 2 C 2
0 1 0 A 3 B 3 C 3
0 1 1 A 4 B 4 C 4
1 0 0 A 5 B 5 C 5
1 0 1 A 6 B 6 C 6
1 1 0 A 7 B 7 C 7
1 1 1 A 8 B 8 C 8
  • Related