AI Hints
Home›Data Science Tutorial›Modern Analyst Tools
Data Science · Lesson 64 of 101

Reducing Columns

Finding the few that matter.

⏱ 2 min read▶ Video at 1:46:17🎓 Free
Watch this lesson in the Data Science courseWatch on YouTube ↗Full Data Science course

Each cafe has sales figures for eight products. How can you compare twelve cafes on eight numbers at once? It would be nice to see one or two scores that capture most of it. That's what principal component analysis does.

Reducing Columns
From the video: Reducing Columns
  • Eight numbers per cafe
  • Hard to compare
  • One or two scores

PCA on the cafes

We make a table of revenue by city and product, standardise it, and ask for two components. Run it. Look at that. The first component explains almost all of the variation, nearly a hundred percent. So all eight products are telling one story. And what's the story? It's just size. Abuja and Cairo are at one end, and Osaka is at the other. The large cafes sell more of everything. That's an insight in itself. It says that the product mix doesn't really differ between cafes. Whenever one component explains nearly everything, you've found one hidden factor that drives it all.

pca_cafes.pypython
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

sales = pd.read_csv("sales.csv")
t = sales.pivot_table(index="city", columns="product", values="revenue", aggfunc="sum")
share = t.div(t.sum(axis=1), axis=0)

X = StandardScaler().fit_transform(t)
p = PCA(2).fit(X)
print("Variance explained:", p.explained_variance_ratio_.round(2).tolist())
print(pd.Series(p.transform(X)[:, 0].round(1), t.index).sort_values().iloc[[0, 1, -2, -1]].to_string())
Output
Variance explained: [1.0, 0.0]
city
Abuja   -3.9
Cairo   -3.9
Milan    3.0
Osaka    5.4

Summary

A few components can tell the whole story. Next, a new assistant that's changing how analysts work.

Key takeaways
  • PCA finds main directions
  • Often one factor dominates
  • Look at what it represents
Next up: Your AI Analyst — talking to your data