How to get the 1st Principal Component by PCA using Python?

前端 未结 3 1972
星月不相逢
星月不相逢 2020-12-21 22:55

I have a set of 2D vectors presented in a n*2 matrix form.

I wish to get the 1st principal component, i.e. the vector that indicates the direction with

相关标签:
3条回答
  • 2020-12-21 23:27

    It isn't obvious from your example that you are using matplotlib.mlab.PCA but if so, the documentation states that the returned object has an attribute Wt, which is "the weight vector for projecting a numdims point or array into PCA space".

    PCA returns the eigenvalues in descending order (you can tell by looking at the fracs attribute of the returned object). So the first principal component (first eigenvector) will be the first row of Wt.

    As noted by @denis, your eigenvectors will be 2D (not 3D) since your input data are 2D.

    0 讨论(0)
  • 2020-12-21 23:34

    PCA gives only 2d vecs from 2d data.

    Look at the picture in Wikipedia PCA:
    starting with a point cloud (dataMatrix) like that, and using matplotlib.mlab.PCA,
    myPCA.Wt[0] is the first PC, the long one in the picture.

    0 讨论(0)
  • 2020-12-21 23:42

    Similar question here: Principal component analysis in Python

    Or you could have a look at scikits learn: http://scikit-learn.org/0.13/modules/generated/sklearn.decomposition.PCA.html

    0 讨论(0)
提交回复
热议问题