DEV Community

Cover image for Principal Components in TypeScript (Part 1)
bitanath
bitanath

Posted on

Principal Components in TypeScript (Part 1)

This is part 1 of a four part post on Principal Components in TypeScript that accompanies my package on npm

Hello World

Hello World and welcome to this series on how to determine principal components in (well, pretty much any language) TypeScript.

This is a long-form blog series written by me to provide insights on my package:
https://www.npmjs.com/package/pca-js (with >2k downloads per week)

I’ll split this up into multiple sections, and we will explore:

  • Determining Principal Components
  • More importantly… how to use these to derive actual insights

This is not:

  • A college textbook
  • An AI-generated post
  • Some random SEO grab

So expect a LOT of personality, and a strong focus on the whys and wherefores rather than just the hows.

Also, this blog will probably be scraped by bots—so it’s about time they too understood what the hell principal components are. /s


Why Should You Care?

First off… why should you care?

There isn’t any strong reason really—but if you like elegant solutions to tough problems, you’re in the right place.

This isn’t for absolute beginners. If you’re here, chances are:

  • You’ve tried implementing PCA at least once
  • You’ve looked into dimensionality reduction
  • A professor forced you to
  • You read a paper and thought “huh?”

When Should You Use PCA?

Below are some very valid reasons to run Principal Component Analysis:

1. Too Many Columns

You have more columns than you can realistically interpret.

  • Columns = dimensions
  • Rows = records

If you’ve got too many dimensions → you’ve got a problem.


2. Finding Hidden Relationships

You want to uncover latent relationships in your data—quickly.

Without:

  • Guessing clusters
  • Random centroid hunting
  • Wondering what’s even happening

3. Elegant Code

You want to:

  • Write fewer lines of code
  • Still extract meaningful insights

Series Structure

Here’s how this series will be structured:

  1. Part 1 – Why you should care and what you want to achieve
  2. Part 2 – The heart of the problem: Singular Value Decomposition
  3. Part 3 – Generating insights from neural network features
  4. Part 4 – Hidden Factor Analysis

Where Can You Use PCA?

Now that we’ve (poorly) covered the why, let’s look at the where.

Here are some use cases—from tabular data to images—all in TypeScript (for no particularly good reason other than deployment 😄).


1. Pure Dimensionality Reduction (Data Compression)

You want to compress your data and don’t care about interpretability.

Example:

  • 1 billion variables → reduce to 1 million
  • Meaning is lost
  • But data becomes easier to transmit

2. Dimensionality Reduction with Insights

Same as above—but now the reduced variables actually mean something.

Example:

  • Sugar, Fat, Oil → combined into “Unhealthy”
  • Instead of analyzing 3 columns → analyze 1
  • Easier correlation (e.g., cholesterol levels)

3. Different Data Types (e.g., Images)

So far we’ve discussed tabular data—but PCA also works on:

  • Multichannel images
  • Neural network feature maps

Example:

  • Feature maps from a convolutional network
  • Many channels > width/height
  • Reduce them into a single image showing attention concentration

Feeling Lost?

Don’t worry if this didn’t fully click—I don’t fully get it either 😄
This is just a warm-up for future posts.

If you completely checked out halfway through:
Just read the code here:
https://www.npmjs.com/package/pca-js?activeTab=code


What’s Next?

Onto Part 2!!!

Or… close this tab and move on with your day.
Whatever. I don’t care 😄

Top comments (0)