QuickBasket, a grocery delivery app, asks customers a 12-question survey every month. The founders get a sheet with 12 numbers per customer and cannot see the big picture.
The analytics team has 300 survey answers. They suspect the 12 questions really measure only a few things. They want to shrink the 12 columns into 2 or 3 clear scores that are easy to track and chart.
Goal: Reduce 12 columns to a few new columns that keep most of the information. PCA (Principal Component Analysis) finds those columns.
300 customers, each rating 12 things from 1 (bad) to 10 (great).
| customer_id | delivery_speed | order_tracking | packaging | delivery_staff | on_time | item_condition | product_prices | discounts | delivery_fee | value_for_money | offers | membership_value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4001 | 7 | 6 | 9 | 5 | 7 | 7 | 9 | 9 | 10 | 10 | 8 | 8 |
| 4002 | 8 | 6 | 6 | 8 | 8 | 7 | 5 | 6 | 8 | 5 | 4 | 7 |
| 4003 | 6 | 4 | 3 | 5 | 4 | 6 | 5 | 6 | 7 | 5 | 5 | 6 |
| 4004 | 5 | 7 | 6 | 8 | 8 | 8 | 6 | 3 | 4 | 3 | 5 | 4 |
| 4005 | 8 | 6 | 5 | 6 | 6 | 8 | 4 | 4 | 6 | 4 | 7 | 5 |
| 4006 | 6 | 9 | 6 | 5 | 6 | 9 | 4 | 5 | 7 | 4 | 8 | 7 |
First 6 of 300 rows · realistic practice data
Press Run on each step in order, or run them all at once. You can change any code and run it again.
Starting Python…
12 answer columns for every customer. Hard to read all at once.
A correlation matrix shows how each pair of questions moves together (1 = always together, 0 = no link). Look for blocks of strong values. Questions that move together are measuring the same underlying thing, and PCA can merge them.
PCA looks for directions of biggest spread, so every column must be on the same scale first. Then PCA builds 12 new columns, called principal components. PC1 holds the most information (variance), PC2 the next most, and so on.
Bars show what each component explains; the line shows the running total. A common rule is to keep enough components to explain about 80% of the information, or to stop where the bars suddenly get small.
Each component is a mix of the original questions. The loadings show how much each question counts in it. Read the signs: if every question has the same sign, the component is an overall happiness score. If one block of questions is positive and another negative, the component compares the two blocks. Give each component a name from this table.
Now keep only 2 components. 300 rows × 12 columns become 300 rows × 2 columns, and we check how much information we kept.
With only 2 columns, all 300 customers fit on one chart. Customers far from the middle are unusually happy or unhappy. For example, colour each customer by their value_for_money answer to see where the price-unhappy customers sit.
PCA can turn the 2 scores back into 12 answers (inverse_transform). Comparing them with the real answers shows what was lost. Small errors mean the 2 scores really do carry most of the survey.
20 questions on new data. You write the code; hints point the way without giving it.