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.
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.
- Reduces dimensionality: fewer columns for the same rows.
- Preserves variance: components are ranked by how much of the total variation they explain.
- Unsupervised: it does not depend on labels.
- Orthogonal components: each one carries information the others do not.
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.
- Standardize the variables when their scales differ.
- Compute the covariance or correlation matrix.
- Get the eigenvalues and eigenvectors of that matrix.
- Sort the components by explained variance.
- Choose how many components to keep, for example until they accumulate a target share of variance.
- Project the original data onto the selected components.
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.
- Interpretability: components are combinations of variables, not the original variables, which makes results harder to explain in business terms.
- Non-linear relationships: PCA captures linear structure; it will miss curved or clustered patterns.
- Few variables, all relevant: reducing dimensions can throw away useful signal with no real gain.
- Untreated scales: variables with large units dominate the variance and distort the components.
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.
- Check the correlation structure between variables.
- Handle missing values.
- Detect and deal with outliers.
- Standardize the features.
- Fit the scaler and PCA on training data only.
- Re-check the explained variance after cleaning.
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.
- NumPy and Pandas for arrays and dataframes.
- Functions, loops and conditionals.
- Data visualization to inspect components.
- Cleaning, preparation and outlier detection.
- Feature selection and cross-validation for the modeling stage.
Build the Python foundation PCA sits on
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently 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.