Maximum number of principal components in PCA. Is sklearn wrong?

Recently I've been interested in applying PCA to a dataset I have and I wanted to develop a deep understanding of what I would actually be doing when I implement it.

Today I encountered two confronting answers to the question of what is the maximum number of principal components. The two answers are these ones:

Do any of you know what is the meaning of that extra component that sklearn's PCA is offering?

• If the number of samples $n$ is less than or equal to the number of features, the $n$-th PC will be constant zero (eigenvalue = 0). This is what sklearn will presumably return. The number of non-trivial PCs is $n-1$ as per the linked answer. Mar 16, 2018 at 16:27
• Are you doing PCA with or without centering?
– whuber
Mar 16, 2018 at 16:31
• @whuber I don't think sklearn.decomposition.PCA can do PCA without centering. I don't see such an option in the documentation. Mar 16, 2018 at 16:41
• @amoeba Completely on point! I just checked what is the n-th principal component of my data and it is always 0! Problem solved :) Mar 16, 2018 at 17:37
• If you use scikit's PCA then it does centering for you. Mar 16, 2018 at 22:12

If the number of samples $$n$$ is less than or equal to the number of features, the $$n$$-th PC will be constant zero (eigenvalue = 0). This is what sklearn will presumably return. The number of non-trivial PCs is $$n−1$$ as per the linked answer.

• Could you please explain why this is so?
– Sos
Feb 29, 2020 at 10:01
• Because the rank of PC's matrix should be less than or equal to n. Recall the linear independence. Jun 23, 2020 at 17:54