CV Courseversity

Data Preparation and Exploratory Analysis

Covers how to clean messy data — missing values, outliers, duplicates — and use visualization and transformation to explore and understand a dataset before modeling.

“A hospital hands you a spreadsheet of ten thousand patient records to build a readmission-risk model. Some ages are blank, one recorded weight is "9999," and several patient IDs appear twice. Before any model is trained, you must decide what to fix, what to flag, and what a first look at the data's shape and distributions can already tell you about the problem you're solving. What does it mean to actually understand a dataset before touching an algorithm?”

Cleaning Data: Missing Values, Outliers, and Duplicates · 15 min

Real-world datasets are rarely ready to feed directly into a model; Google's Machine Learning Crash Course describes the process of detecting and fixing such problems as "scrubbing," and notes that a program can be written to detect omitted values, duplicate examples, and out-of-range feature values before training begins. Missing data is one of the most common issues: a patient record with a blank age field can either be dropped from the dataset (if missingness is rare) or imputed — filled in with a reasonable estimate, such as the median age of similar patients — so that no information is discarded unnecessarily. The choice matters: dropping too many rows can shrink a dataset past the point of being representative, while naive imputation can quietly distort the very patterns a model is meant to learn, so the method chosen should be documented and justified for the specific dataset at hand and revisited if the proportion of missing values in a column turns out to be unusually high.

Outliers are values that lie far outside the typical range of a variable and can be genuine (a very high but real income) or erroneous (a sensor glitch or data-entry mistake). The NIST/SEMATECH e-Handbook of Statistical Methods treats identifying such unusual observations as a core purpose of exploratory data analysis, using systematic techniques rather than eyeballing individual numbers. A common rule of thumb is the interquartile range (IQR) method: compute the 25th and 75th percentiles of a variable, and flag any value more than 1.5 times the IQR beyond either boundary as a potential outlier. A worked example: in a dataset of sensor temperature readings mostly between 15 and 30 degrees, a single recorded value of 9999 is almost certainly a placeholder for a missing or failed reading rather than a real temperature, and the IQR rule would flag it immediately for investigation rather than allowing it to silently distort any statistic computed from the column, such as a mean.

Duplicate records are another quiet source of error, especially when data is merged from multiple sources — for example, combining a hospital's intake system with its billing system might create two records for the same patient visit with slightly different formatting. Google's crash course material on scrubbing explicitly lists duplicate examples alongside omitted values and out-of-range values as a problem to detect programmatically, since duplicates silently give some observations more influence over a model's learned patterns than others, biasing results toward whatever happened to be entered twice. A worked example: if 200 of ten thousand patient records are accidental duplicates of the same 100 patients, any statistic — like the average length of hospital stay — will be skewed toward those 100 patients' particular characteristics unless the duplicates are identified and removed. Consistency checks — verifying that a supposed unique identifier really is unique, and that categorical fields use consistent spelling — catch this class of error before it reaches a model.

Exploratory Analysis: Visualization, Transformation, and Pattern Discovery · 15 min

Exploratory data analysis (EDA) is the practice of examining a dataset's structure, distributions, and relationships before formal modeling, and the NIST/SEMATECH e-Handbook presents it as an approach that emphasizes graphical techniques to gain insight into data, rather than relying purely on a small number of summary statistics. A single summary number can hide a great deal: two variables can have the same mean and standard deviation while having entirely different shapes, and a histogram or scatterplot reveals this immediately where a table of statistics would not. This graphical emphasis is deliberate: rather than assuming a dataset fits a particular textbook distribution and computing statistics that presuppose that shape, EDA starts by looking, so that any unusual structure — skew, multiple peaks, or unexpected gaps — is caught before it silently undermines a later modeling choice that assumed the data looked simpler than it actually does. A worked example: a histogram of patient ages that appears bimodal — with peaks around age 30 and age 70, and few patients in between — visually suggests two distinct subpopulations (perhaps maternity patients and geriatric patients) that may need to be modeled or sampled differently, a pattern a single "average age" statistic would completely conceal.

Many variables in real data are skewed — a small number of very large values pull the distribution's tail out, as with income, hospital charges, or word frequencies — and this skew can make patterns harder to see and can violate assumptions that some downstream statistical or modeling techniques rely on. A log transformation (replacing each value with its logarithm) compresses large values proportionally more than small ones, often turning a heavily right-skewed distribution into something closer to symmetric and easier to visualize and model; this is a standard technique covered in both the NIST handbook's discussion of data transformation and in scikit-learn's preprocessing documentation, which provides tools such as power and quantile transformers for exactly this purpose. A worked example: raw household income data, where most households earn under $80,000 but a few earn over $2,000,000, produces a histogram dominated by one enormous bar and a long empty tail; after a log transform, the same data spreads out into a roughly bell-shaped curve that is far easier to inspect for outliers or subgroups.

Beyond individual variables, EDA looks for relationships between variables — patterns that later inform feature engineering and modeling choices. A correlation matrix or a grid of pairwise scatterplots can reveal, for instance, that two supposedly independent sensor readings are almost perfectly correlated (suggesting redundancy) or that a variable expected to predict an outcome shows no visible relationship to it at all. The NIST handbook frames this pattern-discovery step as central to EDA's purpose: uncovering underlying structure, detecting anomalies, and testing assumptions before committing to a particular model. A worked example: plotting hospital length-of-stay against patient age might reveal a clear upward trend for patients over 60 but no relationship at all for younger patients, suggesting that age should be represented as a nonlinear or segmented feature rather than treated as a single linear predictor. Noticing this kind of relationship during EDA, before any model is trained, is what allows a practitioner to make an informed feature-engineering decision later rather than discovering the same limitation only after a linear model has already underperformed.

Practice

Raw vs. Cleaned Data

Raw ID Age Wt 01 — 70 02 41 9999 03 29 65 03 29 65 Cleaned ID Age Wt 01 35 70 02 41 68 03 29 65

Cleaning replaces a missing age with an imputed value, corrects an out-of-range weight, and removes a duplicate row.

  • Scrubbing a dataset means systematically detecting missing values, duplicates, and out-of-range values rather than eyeballing individual rows.
  • EDA relies on graphical techniques like histograms and scatterplots because a single summary statistic can hide patterns like bimodal distributions.
  • A log transformation is a standard fix for heavily right-skewed variables, such as income, making them easier to visualize and model.

Recall Practice

Missing dataClick to reveal
A patient record has a blank age field. What are the two basic options for handling it, and what is the risk of each?
The row can be dropped, risking a smaller and less representative dataset, or the missing value can be imputed with an estimate like the median age, risking a subtly distorted pattern if done carelessly.
OutliersClick to reveal
How does the interquartile range (IQR) method flag a potential outlier?
It computes the 25th and 75th percentiles of a variable and flags any value more than 1.5 times the IQR beyond either boundary as a potential outlier.
EDA purposeClick to reveal
Why does EDA favor histograms and scatterplots over just computing the mean and standard deviation?
Because two very differently shaped distributions can share the same mean and standard deviation, so graphical techniques reveal structure like skew or bimodality that summary numbers alone would conceal.
Skewed dataClick to reveal
A variable like income has most values small and a few very large. What transformation is commonly applied, and why?
A log transformation is applied because it compresses large values proportionally more than small ones, turning a heavily skewed distribution into a more symmetric, analyzable shape.

Glossary

Data scrubbing
The process of systematically detecting and fixing problems in a dataset such as missing values, duplicates, and out-of-range values.
Imputation
Filling in a missing value with an estimated value, such as the mean or median of the variable, rather than discarding the record.
Interquartile range (IQR)
The range between a variable's 25th and 75th percentiles, commonly used as the basis for a rule that flags potential outliers.
Exploratory data analysis (EDA)
The practice of examining a dataset's structure, distributions, and relationships, largely through graphical techniques, before formal modeling.
Log transformation
Replacing each value in a variable with its logarithm, commonly used to reduce skew in heavily right-skewed data.
Correlation matrix
A table showing the pairwise correlation between every pair of variables in a dataset, used to spot redundancy or relationships.
Practical Activity

Clean a Supplied Ten-Row Data Table by Hand

This is a virtual, paper-and-pencil exercise using a small fictional data table (ten rows of invented patient-visit records) supplied within the lesson materials. The learner manually identifies missing values, a clear outlier, and a duplicate row, decides how to handle each, and sketches a simple histogram of one column by hand. No real patient data, live database, or external system is accessed at any point.

Ready to test yourself?

5 questions on this module.

Start Quiz