liveBook · Manning

Data cleaning

As explained in Outlier Detection in Python

Data cleaning is the process of identifying and correcting, removing, or excluding inaccurate, corrupted, irrelevant, or anomalous data.

Data cleaning prepares data for modeling by identifying and removing strong outliers, while also addressing encoding, scaling, null values, feature engineering, and feature selection. section 4.6

For outlier detection, cleaning is especially important when a detector supports only novelty detection and requires training data without outliers. A practical strategy runs several statistical or machine-learning detectors, removes only a small set of strongest findings, then may add those records back for scoring. section 4.6

Common cleaning choices include:

  • Inspect null counts and fill selected numeric values, such as replacing missing strikeouts with the column median. section 4.6
  • Remove extreme numeric rows by calculating quartiles and an interquartile-range threshold, then retaining rows below every column limit. section 4.6
  • Treat synthetic inlier generation as a stronger form of cleaning when contamination is high, while checking whether its distribution represents real data. section 11.1.2
Listing 4.2 Removing extreme values from the data
limit_dict = {}
 
for col_name in df.columns:
    if (df[col_name].dtype in ['int64', 'float64']):
        q1 = df[col_name].quantile(0.25)
        q3 = df[col_name].quantile(0.75)
        iqr = q3 - q1
        limit = q3 + (3.5*iqr)
        limit_dict[col_name] = limit
 
cond = [True]*len(df)
for key, limit in limit_dict.items():
    cond = cond & (df[key] <= limit)
    
print(len(df))
clean_df = df[cond]
print(len(clean_df)) 

Use the worked dataframe pattern to calculate per-column limits and construct a cleaned subset. section 4.6

How Outlier Detection in Python covers it

How deep it goes
Explains preprocessing choices and gives detector-based and IQR-based procedures, but stops short of universal cleaning rules.
Examples
Uses null-count inspection, median imputation for Strikeouts, IQR filtering, detector ensembles, and synthetic inlier datasets.
What it assumes
Assumes basic machine-learning workflows and tabular columns while explicitly addressing categorical encoding, numeric binning, scaling, and null values.
What you can do after
Apply via statistical tests, machine-learning detectors, and dataframe filters while preserving caution about false exclusions and distribution mismatch.
How it connects
Connects outlier detection with novelty detection, preprocessing, missing-value handling, feature engineering, and synthetic data.

Also explained in

Compare how 14 books explain data cleaning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.