Problem 1.

When solving matrix feature pairs with Numpy, the size of returned results is random, but when we solve data mining, we often need to arrange the eigenvalues in order from large to small. How to sort eigenvalues and eigenvectors simply?

2. The analysis

Numpy’s argsort function, which extracts sorted indexes. For instance,

a = np.array([3.1.2.4])
b = np.argsort(a)
Copy the code

As a result of

array([1, 2, 0, 3])
Copy the code

The corresponding relationship is that the element in B is the index of the element in A from small to large. The effect is as follows

                         a = [3, 1, 2, 4]

                         b = [1, 2, 0, 4]
smallest item index <---------+  +  +  +-----> Last smallest item index
                                 |  |
  seconde smallest  <------------+  +--------> Third smallest
        item index                             item index
                             Small to big
                    +------------------------>
Copy the code

However, there is a problem, that is, the arrangement is from small to large. In many cases, Data Mining requires the arrangement of eigenvalues from large to small. Unfortunately Numpy didn’t give us any options, so we had to figure it out ourselves. Considering that the order is reversed when multiplied by the negative, we can take the negative of the sorted array

a = np.array([3.1.2.4])
ma = (-a)
bma = np.argsort(ma)
bma
Copy the code

The results of

array([3.0.2.1])
Copy the code

Perfect.

After sorting the eigenvalues and passing the corresponding index to the columns of the eigenvectors, the corresponding sorted eigenvectors can be obtained. The complete example is as follows

m = np.array([[1.0.505.0.569.0.602.0.621.0.603],
[0.505.1.0.422.0.467.0.482.0.45],
[0.569.0.422.1.0.926.0.877.0.878],
[0.602.0.467.0.926.1.0.874.0.894],
[0.621.0.482.0.877.0.874.1.0.937],
[0.603.0.45.0.878.0.894.0.937.1]])
evalue, evector = np.linalg.eig(m)
idx = np.argsort(-evalue)
es = evalue[idx]
idx = np.argsort(-evalue)
es = evalue[idx]
Copy the code

The output is

M = array ([[1., 0.505, 0.569, 0.602, 0.621, 0.603], [0.505, 1., 0.422, 0.467, 0.482, 0.45], [0.569, 0.422, 1, 0.926, 0.877, 0.878], [0.602, 0.467, 0.926, 1, 0.874, 0.894], [0.621, 0.482, 0.877, 0.874, 1, 0.937], [0.603, ]) evalue = array([4.456, 0.782, 0.458, 0.168, 0.054, 0.079]) evector = array ([[0.350, 0.395, 0.846, 0.051, 0.025, 0.014], [0.286, 0.814, 0.502, 0.020, 0.042, 0.014]. [0.439, 0.263, 0.110, 0.504, 0.332, 0.599], [0.446, 0.197, 0.098, 0.470, 0.415, 0.597], [0.448, 0.161, 0.065, 0.548, 0.575, 0.368], [0.447, 0.213, 0.069, 0.469, 0.618, 0.382]] es = array ([4.456, 0.782, 0.458, 0.168, 0.079, 0.054) ev = array ([[0.350, 0.395, 0.846, 0.051, 0.014, 0.025], [0.286, 0.814, 0.502, 0.020, 0.014, 0.042], [0.439, 0.263, 0.110, 0.504, 0.599, 0.332], [0.446, 0.197, 0.098, 0.470, 0.597, 0.415], [0.448, 0.161, 0.065, 0.548, 0.368, 0.575], [0.447, 0.213, 0.069, 0.469, 0.382, 0.618]])Copy the code

Perfect solution.

Conclusion 3.

Today we discussed how to sort eigenvalues and eigenvectors derived from Numpy. Argsort yields the sequence number sorted from smallest to largest, and negative from largest to smallest. The corresponding eigenvectors can be obtained by column index of eigenvectors.

Life is short. I use Python.