Reputation: 2606
You need to sort the columns by decay values of the diagonal elements, 0.884>0.749>0.640, this swap 1 and 3 column
numpy.array(
[
[ 0.640 -0.655 0.399]
[ 0.617 0.749 0.239]
[-0.456 0.093 0.884]
]
to receive the result :
numpy.array(
[
[ 0.399 -0.655 0.640]
[ 0.239 0.749 0.617]
[-0.884 0.093 -0.456]
]
Upvotes: 0
Views: 1827
Reputation: 689
I would do :
a[: , numpy.argsort(a.diagonal())[::-1] ]
a.diagonal
to get the diagonal values with [::-1]
to get them in reverse ordernumpy.argsort
to get the new order of the columnsUpvotes: 1
Reputation: 12587
You could use advanced splicing:
>>> import numpy as np
>>> a = np.arange(25).reshape(5,5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
>>> a[:,[0,4]] = a[:,[4,0]]
>>> a
array([[ 4, 1, 2, 3, 0],
[ 9, 6, 7, 8, 5],
[14, 11, 12, 13, 10],
[19, 16, 17, 18, 15],
[24, 21, 22, 23, 20]])
>>>
Upvotes: 0
Reputation: 86188
I think this is what you are looking for:
>>> a
array([[ 0.64 , -0.655, 0.399],
[ 0.617, 0.749, 0.239],
[-0.456, 0.093, 0.884]])
>>> a[:, np.argsort(a.diagonal() * -1)]
array([[ 0.399, -0.655, 0.64 ],
[ 0.239, 0.749, 0.617],
[ 0.884, 0.093, -0.456]])
Upvotes: 2