Start learning free

LESSON 45 OF 165 · MACHINE LEARNING: MODELS

Put Features on One Scale

Practice

In the last lesson, spent got a weight of 0.004 and returned_item got -0.924. That doesn't mean returns matter more. A weight counts per unit of its feature: spent's unit is one dollar and customers spend hundreds, while returned_item is only ever 0 or 1. To compare weights, first put every feature on one scale.

A StandardScaler, from sklearn.preprocessing, rewrites each value as (value − mean) ÷ standard deviation: how many standard deviations it sits above or below its column's mean. After scaling, every column has mean 0, a typical distance from the mean is 1, and below-average values are negative. scaler.fit(X) learns each column's mean and standard deviation and keeps them in scaler.mean_ and scaler.scale_. scaler.transform(X) gives back a new numpy array of scaled values, with the same rows and columns.

ages = [[20], [30], [40]]
s = StandardScaler()
s.fit(ages)                       # learns mean 30, standard deviation 8.16
print(s.transform([[30], [45]]))  # 30 becomes 0.0, 45 becomes about 1.84

Fit the scaler on the training rows only: the test rows play customers nothing has seen, so their values must not shape the means. Then transform the training rows and the test rows with that same scaler, and fit the model on the scaled training rows. Each weight now says how much one standard deviation more of its feature pushes toward yes, so the weights can be compared.

Your code prints the old weights, then the new ones. The check adds the mean of spent your scaler learned and the first test customer, scaled:

Without scaling:
  visits 0.268
  orders 0.278
  spent 0.004
  days_since_last -0.014
  returned_item -0.924
After scaling:
  visits 1.101
  orders 0.466
  spent 0.617
  days_since_last -0.803
  returned_item -0.36
Mean of spent the scaler learned: 189.63
First test customer, scaled: [ 2.51  2.04  1.08 -0.85 -0.35]

days_since_last looked tiny per day (-0.014); per standard deviation it is the strongest push toward no (-0.803). visits now pushes hardest toward yes.

Your task: the old model raw, the helper show_weights and a new model are ready. Below show_weights(raw), make a StandardScaler in scaler, fit it on X_train only, and keep the transformed training and test rows in X_train_scaled and X_test_scaled. Then fit model on the scaled training rows and call show_weights(model).


Assignment
Follow the Coding Tutorial to scale the training and test rows with one StandardScaler, train a second logistic regression on the scaled rows and print its weights.


Hint
Call fit once, on X_train. Then call transform twice with the same scaler: on X_train and on X_test. Fit model on X_train_scaled, not on X_train.


STEP 1 OF 6
    Ctrl + Enter to check