Start learning free

LESSON 99 OF 165 · MACHINE LEARNING: MORE MODELS

Fewer Columns: PCA

Practice

Your 5 feature columns don't all say different things: a customer who visits a lot usually orders a lot and spends a lot too. PCA (principal component analysis) builds new columns, each one a mix of the old ones. The first new column follows the direction in which the customers differ most, the second one the next biggest, and so on. Keep only the first few and you get fewer columns that still hold most of the information.

PCA(n_components=2) makes a PCA that will keep 2 new columns. Its fit_transform(data) learns the directions from the data and gives back a new array: the same rows, 2 columns. After that, explained_variance_ratio_ gives back one share per new column: how much of the total spread it keeps, where 1.0 would be all of it. Scale the columns first, or a column counted in money drowns out a column of 0s and 1s.

from sklearn.decomposition import PCA

points = [[1, 2], [2, 4.1], [3, 5.9], [4, 8]]
one = PCA(n_components=1)
flat = one.fit_transform(points)
print(flat.shape)                     # (4, 1)
print(one.explained_variance_ratio_)  # almost 1: the points lie nearly on a line

The starting code loads the customers, scales the 5 columns into scaled and prints its shape. With your lines, the script prints:

Before: (300, 5)
After: (300, 2)
Spread kept by each new column: [0.472 0.2  ]
Kept in total: 0.672

Then the check prints what each new column is made of and the first 3 customers in the new columns. Two columns keep about two thirds of the spread of five.

Your task: below the blank line, make a PCA with n_components=2 and keep it in pca. Fit it on scaled with fit_transform and keep the result in reduced. Then print "After:" with the shape of reduced, "Spread kept by each new column:" with the shares rounded to 3 digits, and "Kept in total:" with their sum, rounded to 3 digits.


Assignment
Follow the Coding Tutorial to squeeze the 5 scaled columns into 2 with PCA and print how much of the spread they keep.


Hint
fit_transform learns and transforms in one call. explained_variance_ratio_ is a numpy array, so it has its own .round(3) and .sum(); wrap the sum in round(…, 3).


STEP 1 OF 5
    Ctrl + Enter to check