Your customer data mixes columns on very different scales: visits go from 3 to 9, money spent from 38 to 230. A model that compares raw numbers lets the biggest column decide almost everything. The fix is to standardize each column: subtract the column's mean, then divide by its spread. Afterwards every column has mean 0 and spread 1.
The tools: X.mean(axis=0) gives back one mean per column. X.std(axis=0) gives back each column's standard deviation: how far, on average, the values sit from their mean. Both give one number per column, so for 3 columns their shape is (3,). With broadcasting, numpy uses such a row of numbers on every row of a 2-D array:
boxes = np.array([[10, 200],
[20, 400]])
print(boxes - boxes.mean(axis=0)) # each column minus its own mean
After scaling, a value tells you how many standard deviations it sits above (positive) or below (negative) its column's mean. Your X has 5 customers and 3 columns: visits, spent and days since the last order. The finished script prints:
[[ 5 134 104] [ 9 230 32] [ 3 38 120] [ 7 182 56] [ 6 166 88]] Shapes: (5, 3) (3,) [[-0.5 -0.25 0.75] [ 1.5 1.25 -1.5 ] [-1.5 -1.75 1.25] [ 0.5 0.5 -0.75] [ 0. 0.25 0.25]] New means: [0. 0. 0.] New stds: [1. 1. 1.]
Your task: below print(X), keep the column means in means and the column standard deviations in stds, and print "Shapes:" with the shapes of X and means. Then build scaled by subtracting means from X and dividing by stds, and print it with np.round to 2 decimals. Finally check the result: print "New means:" and "New stds:" with the mean and the standard deviation of each column of scaled, rounded to 3 decimals.
Assignment
Follow the Coding Tutorial to standardize the three columns of X and check that each one ends with mean 0 and standard deviation 1.
Hint
Use axis=0 to get one number per column. Python divides before it subtracts, so put the subtraction in parentheses before dividing by stds.
Free plan: 30 tutor messages. PRO is unlimited.
