Skip to main content

Dropping columns

Dropping columns removes model variables that might lead to underidentified models, or models that take a long time to fit. mlfeatures has three filter steps for it: one for correlated columns and two for columns with little or no variance.

  • StepDropCorr drops one column from each highly correlated pair (default threshold 0.9), which keeps a linear model from fitting redundant columns.
  • StepDropNearZeroVariance drops columns that are almost constant, and StepDropZeroVariance only those that are fully constant or all missing.
  • Filters are not a stage of the eight-step order; they run after stage 1 and again after the dummy step.

Setup

from mlfeatures import Recipe, StepDropCorr, StepDropNearZeroVariance, StepDropZeroVariance
from mlfeatures import Selectors as s
from mlfeatures.data import ames_mini, beans

Drop one of each highly correlated pair

StepDropCorr(columns, threshold=...) drops one column from each pair whose correlation exceeds the threshold, keeping the other. In the Ames subset, Garage_Cars and Garage_Area say nearly the same thing (correlation 0.89), so one of them goes:

recipe = Recipe(data=ames_mini, target="Sale_Price").add(
    StepDropCorr(s.numeric_features(), threshold=0.8)
)
fitted = recipe.fit(ames_mini)
fitted.transform(ames_mini).columns
['Neighborhood',
 'Bldg_Type',
 'Exterior_1st',
 'Street',
 'Gr_Liv_Area',
 'Year_Built',
 'Garage_Area',
 'Latitude',
 'Longitude',
 'Sale_Price']

The default threshold is 0.9. The dry beans data shows how much a correlated set can collapse: its 16 shape measurements lose 9 of their number.

recipe = Recipe(data=beans, target="class").add(StepDropCorr(s.numeric_features()))
fitted = recipe.fit(beans)
fitted.transform(beans).columns
['aspect_ratio',
 'extent',
 'solidity',
 'roundness',
 'shape_factor_1',
 'shape_factor_2',
 'shape_factor_4',
 'class']

Drop columns with little or no variance

StepDropNearZeroVariance(columns) drops columns that are nearly constant, which a model can only overfit to. Street is the case here: 12 of 2,930 houses are on a gravel road.

recipe = Recipe(data=ames_mini, target="Sale_Price").add(
    StepDropNearZeroVariance(s.features())
)
fitted = recipe.fit(ames_mini)
fitted.transform(ames_mini).columns
['Neighborhood',
 'Bldg_Type',
 'Exterior_1st',
 'Gr_Liv_Area',
 'Year_Built',
 'Garage_Cars',
 'Garage_Area',
 'Latitude',
 'Longitude',
 'Sale_Price']

StepDropZeroVariance is the strict version: it drops only columns that are completely constant or all missing. Its usual place is right after a dummy step, where a level that is present in the schema but absent from the training rows becomes an all-zero indicator column.

Missing values are a row problem

Filters remove columns. Rows with missing values are a different problem, handled by imputation rather than removal, and they are covered on Ensure robust to new data.