简体   繁体   中英

How to label the entries of a matrix as their indices but still keep their original numerical values?

I have a matrix_1 full of numerical values and what I'd like to do is transform this into a matrix_2 with the values(of matrix_1 sorted) and then replace these sorted values in matrix 2 with the original indices from matrix_1.

I don't want to use any loops as the matrices are rather large.

for example : matrix_1=[[2,3,4,1],[6,5,9,7]]

I want to end up with matrix_2=[[(1,4),(1,1),(1,2),(1,3)],
                                 [(2,2),(2,1),(2,4),(2,3)]]

I've tried use np.ndenumerate on the original matrix but it returns array([numpy.ndenumerate object at 0x1a1a9fce90], dtype=object)

I've now also tried np.argsort() but it doesn't seem to work, possibly because all of my entries are floats...

using np.argsort should do the trick:

matrix_1=np.array([[2,3,4,1],[6,5,9,7]])
matrix_1
array([[2, 3, 4, 1],
       [6, 5, 9, 7]])
x = np.argsort(matrix_1,axis=1)
array([[3, 0, 1, 2],
       [1, 0, 3, 2]], dtype=int64)

A matrix consisting of floats shouldn't pose a problem.

You can then create the list as:

[[(i+1,v+1)   for v in enumerate(y)] for i, y in enumerate(x.tolist())]
[[(1, 4), (1, 1), (1, 2), (1, 3)], [(2, 2), (2, 1), (2, 4), (2, 3)]]

You must come from R or other language that start indexing on 1 . In Python, indexes start at 0 , so you have to explicitly add + 1 to the indexes to make them start at 1.

Use argsort and then reshape

m1 = matrix_1.argsort(1) + 1
i = (np.repeat(np.arange(m1.shape[0]), m1.shape[1]) + 1).reshape(m1.shape)

np.concatenate([m1[:, None],i[:, None]], axis=1).swapaxes(2,1)

which outputs

array([[[4, 1],
        [1, 1],
        [2, 1],
        [3, 1]],

       [[2, 2],
        [1, 2],
        [4, 2],
        [3, 2]]])

argsort applied to the flattened array:

In [110]: np.argsort(arr1.ravel())                                              
Out[110]: array([3, 0, 1, 2, 5, 4, 7, 6])

Turn that into 2d indices:

In [111]: np.unravel_index(_,(2,4))                                             
Out[111]: (array([0, 0, 0, 0, 1, 1, 1, 1]), array([3, 0, 1, 2, 1, 0, 3, 2]))

Combine the arrays into one, and reshape:

In [112]: np.transpose(_)                                                       
Out[112]: 
array([[0, 3],
       [0, 0],
       [0, 1],
       [0, 2],
       [1, 1],
       [1, 0],
       [1, 3],
       [1, 2]])
In [113]: _+1           # tweak values to match yours
Out[113]: 
array([[1, 4],
       [1, 1],
       [1, 2],
       [1, 3],
       [2, 2],
       [2, 1],
       [2, 4],
       [2, 3]])
In [114]: _.reshape(2,4,2)                                                      
Out[114]: 
array([[[1, 4],
        [1, 1],
        [1, 2],
        [1, 3]],

       [[2, 2],
        [2, 1],
        [2, 4],
        [2, 3]]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM