Python para Data Science e Analytics
Python para Data Science e Analytics › Guides › What Is PCA: Principal Component Analysis in Practice

What Is PCA: Principal Component Analysis in Practice

PCA (Principal Component Analysis) is a dimensionality reduction technique that turns original variables into principal components ordered by the variance they explain. In practice it swaps many columns for a few while keeping most of the variation in the data. It is an unsupervised technique, used both to explore data and as a preprocessing step before models.

Topics: PCA, redução de dimensionalidade, machine learning

What is PCA in plain terms?

PCA (Principal Component Analysis) is a dimensionality reduction technique that transforms a set of original variables into a new set of variables called principal components. Each component is a linear combination of the original variables, and the components are ordered by how much of the data's variance they explain.

The first component captures the largest share of the variation in the data. The second captures the largest share of what is left, and so on down the list. Keep the first few components and drop the rest, and you have traded many columns for a handful that still carry most of the original information.

PCA is unsupervised: the calculation looks only at how the data varies, never at labels or a target column. That is why it appears both in exploratory analysis and as a preprocessing step before a model.

How does PCA preserve variance?

Variance measures how far values spread around their mean. PCA looks for the directions in the data space where that spread is largest and uses those directions as new axes.

The calculation starts from the covariance matrix (or the correlation matrix) of the variables. The eigenvectors of that matrix point along the component directions, and the eigenvalues say how much variance each direction accounts for. The sum of the eigenvalues is the total variance, so the ratio between one eigenvalue and that sum is the share of variance explained by that component.

Because the components are orthogonal, they do not repeat information: each one captures a distinct slice of the variability. That is what makes it reasonable to discard the last components, which usually represent small variation or noise. In practice you plot the explained variance per component, or the cumulative curve, and choose a cut-off from there.

When should you use PCA, and when is it the wrong tool?

PCA tends to make sense when many variables are correlated with each other, when you want to plot high-dimensional data on two or three axes, or when keeping every column is expensive to compute or store. It also shows up as a preprocessing step to cut redundancy and noise before a model.

There are cases where it does more harm than good.

If the structure you care about is non-linear, methods such as t-SNE, UMAP and autoencoders are usually the better fit. They are built for that shape of data, while PCA is a linear method by construction.

What has to happen before you run PCA?

Standardization comes first. If variables are measured in different units, the one with the larger scale dominates the variance and therefore the components. Standardizing puts every variable on the same footing.

Outliers matter too, because the method is built on variance and covariance: a single extreme point can pull the axes toward itself. Data cleaning and preparation, including outlier detection, come before the projection, not after.

Missing values need handling as well, and the scaler plus the PCA fit should be learned on training data only. Fitting them on the full dataset leaks information from the test set into the model.

Where does PCA fit in a Python learning path?

PCA is not a first-week topic. The Python you need first is array and dataframe handling with NumPy and Pandas, plus user-defined functions, loops and conditionals, and enough visualization to read a scree plot and a scatter of the first two components. Cleaning and preparing data, including outlier detection, matters more here than in most techniques because PCA is built on variance.

After that, PCA sits next to the rest of the modeling toolkit: feature selection, cross-validation, and the algorithms it is often paired with. The Python for Data Science and Analytics course is organized in three main modules, Basic, Intermediate and Advanced, plus bonus modules. It covers IDE and Jupyter Notebook setup, NumPy and Pandas, user-defined functions, loops and conditionals, data visualization, data cleaning and preparation, and outlier detection. The advanced track covers machine learning: linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection and cross-validation, closing with an end-to-end exploratory analysis and machine learning project.

Lessons are recorded and watched at your own pace, with guided practice exercises and real market projects for your portfolio. Access is unlimited with no expiration date, completion comes with a certificate, and there is an exclusive community for networking. No prior programming experience is required to start. The course costs R$ 470, or 12 installments of R$ 20.37 on a card, paid through the Hotmart checkout, which also accepts boleto. Module details are on python.paanalytics.net.

Build the Python foundation PCA sits on

Secure payment via Hotmart, up to 12 installments.

Enroll now Product site

Frequently asked questions

Is PCA supervised or unsupervised?

Unsupervised. PCA looks only at how the variables vary together, with no target column involved. You can still use its output as input to a supervised model, but the PCA step itself never sees the labels.

How many principal components should I keep?

There is no universal number. Look at the cumulative explained variance curve, the point where adding another component stops buying much variance, and how the downstream model performs. Keeping the component count as a value you tune with cross-validation is a common approach.

Does PCA work if my variables are on different scales?

Not well. Variables with larger units contribute more to the variance and end up dominating the components. Standardize the features first so every variable is compared on the same basis.

Is PCA the same as feature selection?

No. Feature selection keeps a subset of the original columns, so you still know what each one means. PCA builds new columns that are combinations of the originals: you keep variance, but you lose the direct link to the original variable names.

Can PCA make a model worse?

Yes. If useful signal sits in the components you dropped, if the relationships are non-linear, or if the people reading the result need the original variables, PCA can hurt. Compare the model with and without it before committing.

Ler em português →