简体   繁体   中英

Use of np.linalg.norm for checking the eigen vectors in PCA

I was following a tutorial on PCA I came to the point of selecting the principal components.

This is the link for the tutorial on PCA :

I am stuck at this point in the code. I couldn't understand what it actually does?

eigen_values, eigen_vectors = np.linalg.eig(cor_mat2)

for ev in eigen_vectors:
    np.testing.assert_array_almost_equal(1.0, np.linalg.norm(ev))
print('Everything ok!')

I really appreciate if anyone could help me understand.

What does np.linalg.norm checks here?

As can be read in np.linalng.norm documentation, this function calculates L2 Norm of the vector.

All this loop does is ensuring, that each eigenvector is of unit length, so each eigenvector's importance for data representation can be compared using eigenvalues .

Eigenvectors span a new base for your projection, and as such, those are of unit length (as described in the article). They wouldn't have to be but it's easier that way, you can think of it like new xyz axis in 3-D (such canonnical base is always constructed of vectors containing zeros in all dimensions and one in only one place, x would be vector (1, 0, 0) , y would be (0, 1, 0) and z (0, 0, 1) ).

In order to get the new directions containing most information about data (linear-wise at least, most variance) and perform dimensionality reduction of your desired size (say N ), we will have to compare their "influence" on data. That's what eigenvalues are used for, as eigenvectors cannot be compared unles normalized to the same (unit) scale.

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