Data types and transformations
Preprocessing changes how observations are represented or selected. It is distinct from relational schema normalisation: scaling a numeric column does not put a relation into 3NF, and a BCNF decomposition does not standardise its numeric values.
8.1 Measurement scales and database types
| Measurement scale | Meaningful comparisons | Examples and cautions |
|---|---|---|
| Nominal | Equality and inequality | City code, colour; arithmetic on labels has no meaning |
| Ordinal | Order, not equal spacing | Low/medium/high; rank differences do not measure equal changes |
| Interval | Order and differences | Celsius temperature; zero is conventional, so ratios are not intrinsic |
| Ratio | Order, differences and ratios | Mass, duration; zero has physical meaning |
Binary variables have two categories. Symmetric binary attributes treat both outcomes equally; asymmetric binary attributes give special importance to presence. Discrete variables take countable values; continuous measurements are represented to finite precision in a computer. A numeric-looking identifier is still nominal.
| SQL type family | Intended representation |
|---|---|
INTEGER | Whole numbers; finite implementation-dependent range |
DECIMAL(p,s) / NUMERIC(p,s) | Exact decimal with precision and scale ; digits before the decimal under the ordinary nonnegative-scale convention |
REAL, floating-point types | Approximate numbers; decimal equality can be affected by rounding |
CHAR(n) / VARCHAR(n) | Fixed-width / variable-length character data, with implementation-specific storage overhead |
| Date, time, timestamp, interval | Calendar points or elapsed intervals, not arbitrary strings |
| Boolean | TRUE/FALSE plus SQL null when nullable |
Use text for identifiers with significant leading zeroes. One-hot encoding gives nominal categories separate indicator columns; assigning codes 1,2,3 must not imply metric distances. Fit preprocessing parameters on the training/reference data and reuse them for future records; fitting on evaluation data leaks information.
8.2 Numeric normalisation and scaling
For a reference column treated as a full population, use and . If the question specifies a sample standard deviation, its denominator is instead. Do not silently mix them. Z-scoring does not require normally distributed data and does not make the distribution Gaussian.
A new value outside the fitted min–max range maps outside unless clipping is specified. A constant column has no unique min–max or z-score result under the displayed formulas; choose a stated handling policy, rather than divide by zero. Outliers strongly affect extrema and standard deviation. Median and interquartile-range scaling can be more resistant, but requires its own stated convention.
8.3 Discretisation and smoothing
Discretisation replaces numerical values with interval labels. Equal-width bins have equal numerical spans; equal-frequency or quantile bins aim for equal observation counts after sorting. Equal-frequency bins generally have unequal widths. Ties can make exact equal counts impossible unless equal values are split, which must be stated.
Binning can smooth values by replacing each with its bin mean, median or nearest bin boundary. Boundary ties need a rule. Smoothing retains a numerical representative; discretisation retains the category. Supervised discretisation uses class labels, often preferring splits that reduce weighted class entropy. Unsupervised binning uses only the attribute's values. Bottom-up merging and top-down splitting are alternative ways to choose intervals.
8.4 Sampling and representative subsets
Simple random sampling without replacement selects a uniformly random subset of a fixed size. Sampling with replacement can repeat observations. Bernoulli sampling independently retains each row with a given probability and therefore has a random sample size. Stratified sampling draws within each group; cluster sampling chooses groups and then their members. Systematic sampling uses a random start and regular spacing; periodic data can bias its representativeness.
Proportional stratified allocation uses approximately , with integer rounding arranged so the allocations sum to . Oversampling rare groups can improve their representation, but their raw sample frequencies are no longer population proportions. Reservoir sampling keeps a uniform sample of rows from a stream: after the first , retain incoming row with probability and replace a uniformly chosen reservoir entry if retained.
8.5 Compression, representation and loss
Lossless compression reconstructs the exact original representation. Lossy compression allows controlled error. Column-wise storage often exposes repeated or slowly changing values, helping dictionary, run-length and delta representations. Compression can reduce disk traffic but requires coding and decoding work.
| Method | Store | Best fit and caution |
|---|---|---|
| Dictionary encoding | Distinct-value dictionary plus value codes | Repeated categorical values; include the dictionary cost |
| Run-length encoding | Value and length for each run | Long consecutive repeats; alternating values may expand |
| Delta encoding | Initial value and successive differences | Sorted or smooth sequences; differences still need encoding |
| Quantisation | Bin/level code and representative | Lossy unless every original value is recoverable |
| Prefix coding | Variable-length uniquely decodable codes | Frequent symbols get shorter codes; codebook overhead matters |
For supervised discretisation, a split's information gain is the parent class entropy minus the child entropies weighted by child proportions. It measures label uncertainty reduction, not a guaranteed compression ratio. Prefix-code construction algorithms belong in the Algorithms handbook; here the focus is representation cost.
Last-minute recall
- Numeric identifiers can still be nominal data.
- Min–max scaling uses a range; z-score uses a standard deviation.
- Population and sample standard deviations have different denominators.
- Equal-width bins need not contain equal numbers of observations.
- Sampling without replacement cannot repeat a row.
- Unequal sampling fractions require population weights for overall estimates.
- Count dictionary and metadata costs before claiming compression.
- Lossless decoding must reproduce the input exactly; quantisation generally does not.