Skip to content

Handling Outliers

Outliers could be:

  • True rare events (high-value orders)
  • Measurement/unit problems
  • Recording errors
  • Different segment (VIP customers)

So the first step is always: investigate.

  • Check the source system
  • Confirm units
  • Validate against business rules
Remove via IQR bounds
clean = df[(df["amount"] >= lower) & (df["amount"] <= upper)].copy()
print(clean)

Removing is risky when you report business totals.

Capping keeps all rows but limits extreme values.

Cap values
df["amount_capped"] = df["amount"].clip(lower, upper)

Useful when values span many orders of magnitude.

Log transform
import numpy as np
 
df["amount_log"] = np.log1p(df["amount"])  # log(1+x) to handle 0
  • Reporting metrics → investigate, maybe cap
  • ML features → cap or transform often helps
  • Fraud/anomaly detection → keep outliers (they may be the signal)
  • detection rule
  • chosen handling method
  • expected impact
diagram Handling outliers decision mermaid
After detecting an outlier, the right response depends on why it exists and what the data will be used for.

Most values cluster tightly together; an outlier sits far away from that cluster. Capping doesn’t delete it — it pulls the value back to a boundary you choose.

sketch An outlier on a number line p5.js
Normal values cluster together; an outlier sits far away and can be pulled back to a cap boundary instead of deleted.

Now that extreme values are under control, move on to Feature Scaling (MinMax vs Standard) so all your numeric features live on comparable scales.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading