Numbers and Categories
Numbers and Categories: Scaling Features, Turning Categories into Numbers, Missing Values in a Model.
Updated
- Scaling Features (Journeyman): Features measured in different units (pounds, years, a 0 or 1) pull unevenly on any model that measures distance or penalizes large weights, so standardizing rescales each one with the training data's mean and standard deviation, which StandardScaler learns with fit on the training part and applies with transform to both parts.
- Turning Categories into Numbers (Journeyman): Numbering categories such as dog, cat and rabbit 1, 2 and 3 invents an order and distances that aren't there, so one-hot encoding gives each category a column of its own, which OneHotEncoder learns from the training data, with handle_unknown='ignore' for a category it never saw, while OrdinalEncoder fits categories that really have an order, given in that order.
- Missing Values in a Model (Journeyman): Most scikit-learn models stop with an error when a feature holds NaN, and dropping those rows throws away examples and leaves a new pet with a blank unscorable, so SimpleImputer fills each blank with a value learned from the training data (the median for numbers, the most frequent value for categories), and add_indicator=True keeps a column saying which values were missing.