+91 99301 12627
Machine Learning

PCA Explained: Principal Component Analysis for Beginners

By Mehul Prajapati

Prism splitting light into components representing principal component analysis
On this page

Short answer: Principal Component Analysis (PCA) is an unsupervised technique that reduces many correlated features to a smaller set of new features called principal components. The first component captures the direction of greatest variance in the data, the second captures the next most variance at right angles to the first, and so on. By keeping only the top components, you compress the data, remove redundancy, speed up models and make high-dimensional data easier to visualise, at the cost of interpretability.

Key takeaways

  • PCA creates new uncorrelated features ordered by variance explained.
  • Always scale features first; PCA is sensitive to units.
  • Choose components using cumulative explained variance (e.g. 90–95%).
  • Components are combinations of original features, so they’re harder to interpret.

An intuitive example

Imagine customer data with “annual spend”, “monthly spend” and “number of purchases”. These are highly correlated: they all describe “how much the customer buys”. PCA can combine them into one component capturing most of that information, so a model needs fewer inputs.

How PCA works (simplified)

  1. Standardise each feature (mean 0, standard deviation 1)
  2. Compute how features vary together (the covariance matrix)
  3. Find the directions of maximum variance (eigenvectors) and how much variance each explains (eigenvalues)
  4. Project the data onto the top k directions

Background maths: how much maths you need for data science.

Python example

from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import numpy as np

X_scaled = StandardScaler().fit_transform(X)

pca = PCA().fit(X_scaled)
cumulative = np.cumsum(pca.explained_variance_ratio_)
k = int(np.argmax(cumulative >= 0.95)) + 1
print(f'{k} components explain 95% of the variance')

X_reduced = PCA(n_components=k).fit_transform(X_scaled)

Shortcut: PCA(n_components=0.95) automatically keeps enough components to explain 95% of variance.

Using PCA inside a model pipeline

from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression

model = make_pipeline(StandardScaler(), PCA(n_components=0.95), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)

Fitting PCA inside a pipeline ensures it learns only from training data, which avoids leakage. See logistic regression explained.

Choosing the number of components

  • Cumulative explained variance: keep components until you reach 90–95%
  • Scree plot: look for the “elbow” where extra components add little
  • Model performance: tune n_components with cross-validation
  • Visualisation: use 2 or 3 components to plot data

Common uses

  • Visualising high-dimensional data in 2D
  • Reducing features before clustering (e.g. K-means)
  • Removing multicollinearity for linear models
  • Compressing data such as images
  • Noise reduction

Limitations

  • Captures only linear relationships (t-SNE and UMAP are popular for non-linear visualisation)
  • Components are hard to explain to business users
  • High variance isn’t always what matters for prediction
  • Sensitive to outliers and feature scaling

PCA vs feature selection

PCA Feature selection
Output New combined features Subset of original features
Interpretability Lower Higher
Uses target? No (unsupervised) Often yes

More on preparing features: feature engineering.

Frequently asked questions

Does PCA always improve model accuracy?

No. It can speed up training and reduce overfitting, but it can also discard useful information. Always compare with and without PCA.

Do tree-based models need PCA?

Usually not; trees handle many features and correlated inputs reasonably well.

Is PCA asked in interviews?

Yes, especially explained variance and why scaling matters. See machine learning interview questions.

Conclusion

With PCA explained, remember the recipe: scale, fit, check explained variance, keep enough components and validate the effect on your model. Used well, PCA simplifies data without losing what matters.

Learn dimensionality reduction hands-on in our Machine Learning course.

Learn with Mehul Prajapati in Borivali West

At Mehul Prajapati’s institute, you get 100% practical, hands-on training by industry experts with a minimum of 5+ years of experience, plus job-oriented programmes with placement support. Weekday and weekend batches are available, with multiple 2-hour slots between 8 am and 6 pm.

Address: 12th Floor, 1214, Gold Crest Business Centre, LT Rd, Above Westside Showroom, Borivali West, Mumbai, Maharashtra 400092
Call: +91 99301 12627 / +91 96645 45072

Visit the centre or call us to choose the right course and batch and to know the current fee structure.