Elective Data Mining & Warehousing

Chapter 6
Data Understanding and Preprocessing

Post Graduate Program in Computer Science Engineering
Elective Data Mining & Warehousing   •   Post Graduate

6. Data Understanding and Preprocessing

Introduction and Context

Data mining rarely fails because an algorithm cannot be executed. It fails more often because the data supplied to the algorithm do not represent the phenomenon being studied, contain systematic errors, or are expressed in forms that obscure useful patterns. Data understanding and preprocessing therefore constitute an analytical discipline rather than a routine cleaning stage. They connect the original operational data with the assumptions required by statistical and machine-learning methods.

In a data-warehousing environment, preprocessing begins with an understanding of source systems, business meanings, data lineage, grain, time periods, and quality constraints. A single analytical table may combine transactional records, customer profiles, sensor streams, and externally acquired data. These sources can differ in units, keys, update frequency, missingness mechanisms, and definitions. The analyst must determine what each observation represents before deciding how it should be transformed.

The central principle is that preprocessing must preserve analytical validity. A transformation that improves model accuracy on a particular sample may introduce leakage, conceal a meaningful minority group, or make results impossible to interpret. Consequently, preprocessing decisions should be documented, reproducible, evaluated against the analytical objective, and applied consistently to training and future data.

Learning Objectives

After studying this chapter, the reader should be able to:

1. Explain the role of data understanding in the data-mining lifecycle.

2. Use exploratory analysis, visualization, and sampling to assess data quality and structure.

3. Diagnose and treat missing values, outliers, inconsistent records, and duplicate observations.

4. Select suitable normalization, discretization, and categorical-encoding methods.

5. Construct useful features while avoiding target leakage and unjustified transformations.

6. Explain dimensionality-reduction methods and their implications for interpretation.

7. Identify class imbalance and select appropriate sampling, weighting, and evaluation strategies.

8. Design a defensible, reproducible preprocessing workflow for professional applications.

From Raw Records to Analytical Evidence

A useful starting point is to define the analytical unit, sometimes called the grain of the data. In a sales table, a row may represent an invoice, an invoice line, a customer-day, or a product-month. Confusing these levels can produce duplicated measures and misleading relationships. For example, joining customer data to invoice-line data may make a high-value customer appear many times, thereby distorting averages unless the data are aggregated appropriately.

Data understanding has several connected dimensions. Structural understanding concerns schemas, data types, keys, relationships, and cardinality. Semantic understanding concerns what fields mean, how they are measured, and which business rules govern them. Statistical understanding concerns distributions, associations, variation, and unusual observations. Operational understanding concerns how data are generated, updated, corrected, and governed.

A practical workflow is iterative:

  • define the analytical question and target population;
  • inventory sources, fields, keys, and data lineage;
  • profile distributions, missingness, uniqueness, and validity;
  • visualize important variables and relationships;
  • identify quality problems and possible causes;
  • split data into training, validation, and test sets where modeling is intended;
  • fit preprocessing operations using training data only;
  • validate transformed data and document assumptions.

The separation of training and test data is essential. If an imputation value, scaling parameter, feature-selection rule, or principal-component basis is calculated using the full dataset before splitting, information from the test set influences the model. This is data leakage and produces an optimistically biased estimate of performance.

Exploratory Analysis: Asking What the Data Are Saying

Exploratory data analysis (EDA) combines numerical summaries, visual displays, and domain reasoning. Univariate analysis examines one variable at a time. For numerical attributes, useful summaries include count, mean, median, standard deviation, quartiles, minimum, maximum, interquartile range (IQR), skewness, and selected percentiles. The median and IQR are often more informative than the mean and standard deviation when distributions are skewed or contaminated by extreme values.

For categorical variables, analysts inspect frequency tables, the number of distinct values, rare categories, and invalid labels. “M”, “Male”, and “male” may represent one category or different categories depending on the source system. High-cardinality fields such as transaction identifiers should generally not be treated as ordinary predictors.

Bivariate and multivariate analysis examine relationships. Scatter plots and correlation matrices can reveal association, nonlinearity, clusters, and heteroscedasticity. A correlation coefficient measures a particular form of association; a low correlation does not prove independence, and a high correlation does not establish causality. Box plots grouped by class, heat maps, pair plots, and time-series plots can expose patterns hidden by aggregate statistics.

Visualization is also a quality-control instrument. A sudden spike on a date plot may indicate a genuine event, a system migration, or duplicated loading. A geographic map may reveal impossible locations. A histogram with a sharp boundary may suggest censoring or an operational limit rather than a natural distribution. Effective EDA alternates between visual evidence and questions about how the data were produced.

Sampling for Understanding and Modeling

Sampling reduces computational cost and can make exploratory analysis manageable, but it must reflect the objective. Simple random sampling gives each record an equal probability of selection. Stratified sampling preserves selected subgroup proportions and is particularly useful when classes, regions, or customer segments differ substantially. Systematic sampling selects records at regular intervals, although it can be biased when the ordering contains periodicity. Cluster sampling selects groups such as branches or households and may be efficient operationally, but observations within clusters are often correlated.

For rare-event problems, a random sample may contain too few positive cases. Stratification, oversampling of the minority class, or deliberate case-control sampling may be appropriate. Sampling must be recorded: population definition, frame, method, random seed, inclusion probabilities, and any weighting scheme should be available for audit and replication.

Missing Values: Absence as Information

A missing value is not automatically an error. It may mean that a measurement was not collected, a question was not applicable, a device failed, a customer refused to respond, or a value was deliberately suppressed. Common conceptual mechanisms are:

  • Missing completely at random (MCAR): missingness is unrelated to observed and unobserved values.
  • Missing at random (MAR): missingness depends on observed variables after those variables are considered.
  • Missing not at random (MNAR): missingness depends on the unobserved value itself or on an unmeasured process.

These mechanisms cannot always be established from the dataset alone. Missingness indicators and comparisons between records with and without values can provide evidence, but domain knowledge remains important.

Treatment begins with profiling missingness by field, subgroup, and time. Options include deletion, simple imputation, model-based imputation, explicit missing categories, and retaining a missingness indicator. Mean imputation is easy but reduces variation and can distort relationships. Median imputation is more robust for skewed numeric data. Mode imputation may be acceptable for low-cardinality categorical fields but can overrepresent the most common class. More advanced approaches include k-nearest-neighbor imputation, regression imputation, multiple imputation, and iterative model-based methods.

Deletion is defensible when missingness is rare and plausibly random, or when a record lacks essential information. It is dangerous when missingness is systematic. For a categorical variable, “Not reported” may be analytically different from “No,” and these categories should not be conflated. Any imputation model must be fitted on the training partition and then applied to validation, test, and production records.

Outliers, Errors, and Rare but Valid Cases

An outlier is an observation that differs markedly from the general pattern. It may be a data-entry error, a measurement failure, a legitimate extreme, or evidence of a previously unknown subgroup. The IQR rule flags values below Q1 − 1.5 IQR or above Q3 + 1.5 IQR; standardized scores can also be useful under approximate distributional assumptions. Robust methods such as the median absolute deviation are preferable when the data are heavily skewed.

Outlier treatment should follow diagnosis, not a mechanical threshold. A negative age is usually invalid; an unusually large insurance claim may be genuine and central to the business problem. Possible actions include correction from a trusted source, removal of demonstrably erroneous records, winsorization, logarithmic transformation, robust scaling, or retention with an indicator variable. Removing outliers merely because they reduce model error can eliminate the very cases that matter operationally.

Duplicate records require similar care. Exact duplicates may result from repeated extraction, while near-duplicates may reflect legitimate updates. Deduplication requires a business key, timestamp logic, and rules for selecting the authoritative version. Referential integrity checks should identify records whose foreign keys do not match valid dimension or master records.

Transforming Variables for Algorithms

Scaling and Normalization

Many algorithms depend on distances, inner products, or optimization geometry. If income is measured in thousands and age in years, an unscaled distance-based method may allow income to dominate. Common transformations include:

  • Min–max scaling: maps values to a chosen interval, commonly [0,1]. It is intuitive but sensitive to extreme values.
  • Standardization: subtracts the mean and divides by the standard deviation. It is useful for many linear, distance-based, and gradient-based methods, but remains sensitive to outliers.
  • Robust scaling: uses the median and IQR, making it less affected by extreme observations.
  • Log or power transformations: reduce right skew and may make multiplicative relationships easier to model.

Tree-based methods are usually less sensitive to monotonic scaling, whereas k-nearest neighbors, k-means, support vector machines, neural networks, and principal component analysis commonly benefit from scaling. Scaling parameters must be learned from training data only.

Discretization

Discretization converts a continuous variable into intervals such as low, medium, and high. Equal-width binning divides the numerical range into intervals of equal width; equal-frequency binning aims for similar numbers of observations per bin. Supervised discretization uses the target to select informative cut points, but must be performed within the training data to avoid leakage.

Discretization can improve interpretability, support rule-based analysis, and reduce sensitivity to noise. It also loses information, introduces boundary effects, and can conceal meaningful variation. Domain-defined intervals are often preferable when they correspond to regulatory or operational thresholds.

Encoding Categorical Data

Nominal categories require representations compatible with the selected algorithm. One-hot encoding creates a binary variable for each category, usually omitting one level in models with an intercept to avoid perfect linear dependence. Ordinal encoding is appropriate only when categories possess a genuine order. Assigning codes such as 1, 2, and 3 to unrelated occupations falsely implies distance and order.

High-cardinality variables may require frequency encoding, hashing, learned embeddings, or carefully regularized target encoding. Target encoding replaces a category with an estimate related to the target, but naïve target encoding leaks outcome information. It should use out-of-fold estimates and smoothing, with all calculations confined to the training process. Rare categories may be grouped only when the grouping is substantively defensible or validated empirically.

Feature Construction and Selection

Feature construction translates raw measurements into variables more closely related to the analytical question. Examples include customer tenure from dates, order frequency over a defined window, ratio of completed to attempted transactions, rolling sensor averages, text length, and interaction terms. The definition of a feature must specify its reference time. A feature that uses a customer’s activity after the prediction date is leakage, even if it is technically available in the database.

Feature engineering should reflect domain mechanisms, not merely maximize associations in one sample. Ratios can be unstable when denominators are small; date differences require a clear time zone and treatment of missing dates; aggregations require a well-defined observation window. Features should be checked for constant values, duplicate information, excessive missingness, implausible ranges, and post-outcome information.

Feature selection reduces irrelevant or redundant predictors. Filter methods use statistics such as variance, correlation, mutual information, or chi-square scores. Wrapper methods evaluate subsets through model performance but may be computationally expensive. Embedded methods perform selection during training, for example through regularization or tree-based importance. Selection must be nested within cross-validation when performance estimates are being reported.

Dimensionality Reduction

High-dimensional data can suffer from computational burden, redundant variables, sparse observations, and difficult visualization. Dimensionality reduction creates a smaller representation while attempting to preserve selected properties.

Principal component analysis (PCA) forms orthogonal linear combinations of centered, usually scaled variables. The first component captures the greatest possible variance, the second captures the greatest remaining variance subject to orthogonality, and so forth. Explained-variance ratios help choose the number of components, but variance is not identical to predictive usefulness. PCA components may be difficult to interpret, and the method is sensitive to scaling and outliers.

Linear discriminant analysis seeks projections that separate known classes by considering between-class and within-class variation. It is supervised and has assumptions that may not hold in complex data. Nonlinear methods such as manifold learning and t-SNE can produce useful visualizations of local structure, but their axes and global distances should not be interpreted casually. UMAP and autoencoders are other contemporary approaches, each with tuning choices and validation requirements.

Dimensionality reduction should serve a clear purpose: visualization, compression, denoising, or modeling. A reduced representation should be evaluated for information loss, stability, downstream performance, and interpretability.

Imbalanced Data and Unequal Error Costs

In classification, imbalance occurs when one class is much less frequent than another. Accuracy can then be misleading: a classifier that always predicts the majority class may achieve high accuracy while detecting no rare events. Suitable measures include precision, recall, F1-score, balanced accuracy, area under the precision–recall curve, and cost-sensitive expected loss. The appropriate measure depends on the consequences of false positives and false negatives.

Approaches include random undersampling of the majority class, random oversampling of the minority class, synthetic methods such as SMOTE, class-weighted learning, threshold adjustment, and anomaly-detection formulations. Synthetic samples should be generated inside the training folds, never before the train-test split. Oversampling can overfit duplicated cases, undersampling can discard useful information, and synthetic interpolation may create implausible observations in mixed or highly constrained data.

The decision threshold should be selected using validation data and business costs rather than defaulting to 0.5. Evaluation should use a representative untouched test set, and temporal or group-based splitting may be necessary when random splitting would allow related records to appear in both partitions.

An Integrated Preprocessing Framework

A defensible workflow can be organized into five analytical questions. First, what population and outcome are being represented? Second, what quality problems arise from the collection process? Third, which transformations are required by the chosen algorithm? Fourth, how will transformations be validated without leakage? Fifth, how will the resulting dataset and decisions be monitored after deployment?

In practice, preprocessing is implemented as a reproducible pipeline. The pipeline may include type conversion, validation, imputation, encoding, scaling, feature construction, and model fitting. Version control should preserve source-data identifiers, code, configuration, random seeds, and transformation parameters. Data-quality tests can check schema changes, range violations, missingness spikes, category drift, duplicate rates, and unexpected distributions.

Illustrative Case Study: Predicting Missed Medical Appointments

Background. A regional clinic wants to identify appointments at high risk of non-attendance so that reminders and limited overbooking can be planned. The dataset contains appointment time, booking lead time, patient age group, prior attendance history, service type, neighborhood, reminder status, and the attendance outcome.

Situation and problem. Only a minority of appointments are missed. Several fields contain missing values, neighborhood labels are inconsistent, and some records were duplicated when two scheduling systems were merged. A preliminary model reports 92 percent accuracy but identifies few missed appointments.

Analysis. The analytical grain is one appointment. Patient-level histories must be calculated using information available before each appointment. The outcome must not be used to construct predictors. Exploratory analysis reveals that missing reminder status is concentrated in one legacy system, so missingness may indicate process differences rather than random absence. A temporal split is preferable because future scheduling patterns should be predicted from past appointments. Accuracy is inappropriate as the primary measure; recall, precision, and precision–recall performance are more informative.

Approach. Exact duplicates are removed using appointment identifiers and timestamps, while conflicting duplicates are resolved through source-system precedence rules. Neighborhood strings are standardized and rare categories are grouped according to a documented geography rule. Numeric lead time is median-imputed within the training data, with an indicator for missingness. Categorical missingness is retained as “not recorded.” Features include prior attendance rate computed from earlier appointments, number of prior appointments, day-of-week, and booking lead-time bands. One-hot encoding is used for nominal categories, and class weights are supplied to a classifier. The probability threshold is selected using the operational cost of an unnecessary reminder versus a missed appointment. All operations are placed in a training pipeline.

Outcome. The clinic obtains a model whose reported performance is lower than the original accuracy claim but whose prioritized list captures substantially more missed appointments at an acceptable reminder workload. Monitoring is established for changes in missing reminder status, appointment types, and the distribution of predicted risk.

Learning points. The case illustrates that defining the grain, respecting time, treating missingness as potentially informative, preventing leakage, selecting suitable metrics, and aligning thresholds with operational costs are more important than applying a sophisticated algorithm prematurely.

Risks, Governance, and Professional Practice

Preprocessing can produce discriminatory outcomes even when the model does not explicitly use a protected attribute. Imputation, sampling, and encoding may affect demographic groups differently. Analysts should examine subgroup missingness, error rates, calibration, and the consequences of removing or merging categories. Protected attributes may need to be retained for fairness auditing even if excluded from prediction.

Data minimization, purpose limitation, access control, retention rules, and privacy-preserving practices apply to preprocessing as much as to data collection. Re-identification risk can increase when detailed features are combined. Synthetic data and anonymization are not automatic guarantees of privacy. In regulated settings, transformation logic, provenance, consent boundaries, and explanations of automated decisions may be subject to formal review.

Common mistakes include imputing before splitting, scaling the full dataset, deleting all outliers, treating identifier fields as predictors, using accuracy for rare events, applying ordinal codes to nominal categories, and constructing features with future information. Another mistake is to regard preprocessing as a one-time activity. Production data drift, changes in source systems, and evolving definitions require continuous validation.

Current Developments

Modern preprocessing increasingly combines declarative data-quality rules, automated profiling, feature stores, and machine-learning operations platforms. Automated feature engineering and model-based imputation can accelerate experimentation, but their outputs require human review, leakage tests, and documentation. Representation learning allows models to derive features from text, images, graphs, and event sequences, yet the need for sound sampling, temporal validity, fairness assessment, and monitoring remains.

Data-centric AI places greater emphasis on improving labels, coverage, consistency, and subgroup representation rather than only changing algorithms. Drift detection compares incoming distributions with reference data, while concept-drift analysis examines whether relationships between predictors and outcomes have changed. These developments strengthen the case for treating preprocessing as a governed lifecycle capability.

Best-Practice Checklist

A professional preprocessing design should:

  • state the analytical grain, population, target, and prediction time;
  • profile structure, semantics, distributions, missingness, duplicates, and validity;
  • distinguish errors from legitimate rare observations;
  • split data appropriately before learning transformations;
  • use pipelines so training and production transformations are identical;
  • choose metrics and sampling methods according to class prevalence and error costs;
  • test subgroup effects, leakage, drift, and reproducibility;
  • document assumptions, parameters, source versions, and exceptions;
  • retain an untouched evaluation set and avoid repeated informal test-set tuning;
  • monitor the deployed process and revise it when the data-generating context changes.

Chapter Summary

Data understanding establishes what records represent, how they were generated, and whether they are suitable for analysis. Exploratory analysis combines statistical summaries, visualization, domain knowledge, and purposeful sampling. Missing values must be interpreted according to their causes; outliers must be diagnosed rather than automatically deleted. Scaling, discretization, and encoding make variables suitable for particular algorithms, while feature construction expresses domain knowledge and must respect prediction time. Feature selection and dimensionality reduction can improve efficiency but may reduce interpretability or discard predictive information. Imbalanced classification requires suitable metrics, careful sampling, cost-sensitive learning, and threshold decisions. Across all stages, prevention of leakage, reproducibility, fairness, privacy, governance, and post-deployment monitoring are essential. Preprocessing is therefore an iterative, evidence-based component of data mining rather than a mechanical preliminary task.

Key Terms

Analytical grain: The precise entity or event represented by one row of a dataset.

Exploratory data analysis: The use of summaries, visualizations, and investigation to understand data structure and quality.

Data leakage: The unintended use of information unavailable at prediction time or derived from evaluation data.

MCAR: Missing completely at random; missingness is unrelated to observed and unobserved values.

MAR: Missing at random conditional on observed variables.

MNAR: Missingness related to the unobserved value or an unmeasured process.

Imputation: Replacement or estimation of missing values using a defined method.

Outlier: An observation unusually distant from the general pattern of the data.

Winsorization: Limiting extreme values to selected percentile boundaries.

Standardization: Transformation using a mean and standard deviation, commonly producing zero mean and unit variance.

Robust scaling: Scaling based on statistics such as the median and IQR.

Discretization: Conversion of continuous values into intervals or categories.

One-hot encoding: Representation of a categorical variable through separate binary indicators.

Target encoding: Replacing categories with statistics derived from the target, requiring leakage control.

Feature engineering: Construction or transformation of variables to represent useful analytical information.

Dimensionality reduction: Creation of a lower-dimensional representation of data.

Principal component analysis: An unsupervised linear method that forms orthogonal components ordered by captured variance.

Class imbalance: A substantial difference in the frequencies of outcome classes.

Precision: The proportion of predicted positives that are actually positive.

Recall: The proportion of actual positives that are correctly identified.

Review Questions

Short Answer Questions

1. Why must the analytical grain be defined before joining data sources?

2. Distinguish MCAR, MAR, and MNAR missingness.

3. Why can accuracy be misleading for imbalanced classification?

4. When is robust scaling preferable to standardization?

5. What is data leakage, and how can a preprocessing pipeline reduce it?

Long Answer Questions

1. Explain an end-to-end workflow for exploratory analysis and preprocessing, including the role of training and test partitions.

2. Compare deletion, simple imputation, and model-based imputation, discussing their assumptions and risks.

3. Discuss scaling, discretization, and categorical encoding, and relate each technique to algorithmic requirements.

4. Explain PCA, its benefits, assumptions, limitations, and appropriate uses in data mining.

5. Describe strategies for imbalanced data and explain how evaluation metrics should reflect operational costs.

Analytical / Application Questions

1. A fraud dataset has 1 percent positive cases, duplicated transactions, and a timestamp available only after investigation. Design a leakage-resistant preprocessing and evaluation plan.

2. A hospital dataset shows that missing laboratory values are concentrated among emergency patients. What analyses and treatment options would you consider, and why?

3. A customer model performs well on a random test split but poorly after deployment in a new region. Diagnose possible preprocessing and sampling causes and propose monitoring actions.

22. Multiple Choice Questions with Answers

Question 1

A dataset contains multiple rows per invoice line, but the intended analysis is at customer-month level. What should be done first?

A. Apply min–max scaling to all numeric fields

B. Define the analytical grain and aggregate records appropriately

C. Remove all categorical variables

D. Use PCA to eliminate duplicate rows

Correct Answer: B. Define the analytical grain and aggregate records appropriately

Question 2

Which action most directly prevents leakage when imputing and scaling data for model evaluation?

A. Fit transformations separately on the test set

B. Fit transformations on the complete dataset before splitting

C. Fit transformations on training data and apply them unchanged to test data

D. Avoid imputation in all circumstances

Correct Answer: C. Fit transformations on training data and apply them unchanged to test data

Question 3

A missing value is concentrated in records from one legacy system. Which interpretation is most appropriate?

A. It must be MCAR

B. It may be informative about the data-collection process

C. It should always be replaced by zero

D. It proves the field is irrelevant

Correct Answer: B. It may be informative about the data-collection process

Question 4

Which treatment is generally most defensible for an extreme insurance claim that has been verified as genuine?

A. Automatically delete it

B. Replace it with the mean

C. Retain it and consider robust methods or a suitable transformation

D. Convert it to a categorical identifier

Correct Answer: C. Retain it and consider robust methods or a suitable transformation

Question 5

Which encoding is appropriate for nominal occupations when no natural order exists?

A. Ordinal codes 1, 2, and 3

B. One-hot encoding

C. Ranking by alphabetical order

D. Replacing every occupation by the target label

Correct Answer: B. One-hot encoding

Question 6

Why must target encoding be performed carefully?

A. It cannot represent categorical variables

B. It always increases dimensionality

C. It can leak outcome information into predictors

D. It is unaffected by sample size

Correct Answer: C. It can leak outcome information into predictors

Question 7

What is the primary objective of the first principal component in PCA?

A. Maximize captured variance subject to the component construction

B. Maximize the number of categorical levels

C. Guarantee the highest classification accuracy

D. Remove only missing values

Correct Answer: A. Maximize captured variance subject to the component construction

Question 8

For a rare-event classifier where missed positives are very costly, which evaluation approach is most suitable?

A. Use accuracy alone

B. Evaluate recall and precision-related measures and choose a cost-aware threshold

C. Evaluate only the majority-class count

D. Use random labels to balance the test set

Correct Answer: B. Evaluate recall and precision-related measures and choose a cost-aware threshold

Question 9

When should synthetic minority samples such as SMOTE be generated?

A. Before the train-test split

B. On the untouched test set

C. Within the training portion of each validation process

D. Only after deployment

Correct Answer: C. Within the training portion of each validation process

Question 10

A feature uses a patient's visits occurring after the prediction date. What problem does this create?

A. Under-scaling

B. Target or temporal leakage

C. Class balancing

D. Valid dimensionality reduction

Correct Answer: B. Target or temporal leakage

Elective Data Mining & Warehousing
Chapter 6 of 14