# 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.

> **Note: Key points**
>
> - [StepDropCorr](../../reference/StepDropCorr.md#mlfeatures.StepDropCorr) drops one column from each highly correlated pair (default threshold 0.9), which keeps a linear model from fitting redundant columns.
> - [StepDropNearZeroVariance](../../reference/StepDropNearZeroVariance.md#mlfeatures.StepDropNearZeroVariance) drops columns that are almost constant, and [StepDropZeroVariance](../../reference/StepDropZeroVariance.md#mlfeatures.StepDropZeroVariance) only those that are fully constant or all missing.
> - Filters are not a stage of the [eight-step order](../../user-guide/basics/four-stages-eight-steps.md); they run after stage 1 and again after the dummy step.


# Setup


``` python
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:


``` python
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.


``` python
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.


``` python
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](../../reference/StepDropZeroVariance.md#mlfeatures.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](../../user-guide/basics/ensure-robust-to-new-data.md).
