Chapter 8

Data types and transformationsDA

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 typesDA

Measurement scaleMeaningful comparisonsExamples and cautions
NominalEquality and inequalityCity code, colour; arithmetic on labels has no meaning
OrdinalOrder, not equal spacingLow/medium/high; rank differences do not measure equal changes
IntervalOrder and differencesCelsius temperature; zero is conventional, so ratios are not intrinsic
RatioOrder, differences and ratiosMass, 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 familyIntended representation
INTEGERWhole numbers; finite implementation-dependent range
DECIMAL(p,s) / NUMERIC(p,s)Exact decimal with precision pp and scale ss; p−sp-s digits before the decimal under the ordinary nonnegative-scale convention
REAL, floating-point typesApproximate 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, intervalCalendar points or elapsed intervals, not arbitrary strings
BooleanTRUE/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 scalingDA

For a reference column treated as a full population, use μx=(∑ixi)/n\mu_x=(\sum_i x_i)/n and σx2=(∑i(xi−μx)2)/n\sigma_x^2=(\sum_i(x_i-\mu_x)^2)/n. If the question specifies a sample standard deviation, its denominator is n−1n-1 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 [a,b][a,b] 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 smoothingDA

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 subsetsDA

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 nh=nNh/Nn_h=nN_h/N, with integer rounding arranged so the allocations sum to nn. Oversampling rare groups can improve their representation, but their raw sample frequencies are no longer population proportions. Reservoir sampling keeps a uniform sample of nn rows from a stream: after the first nn, retain incoming row ii with probability n/in/i and replace a uniformly chosen reservoir entry if retained.

8.5 Compression, representation and lossDA

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.

MethodStoreBest fit and caution
Dictionary encodingDistinct-value dictionary plus value codesRepeated categorical values; include the dictionary cost
Run-length encodingValue and length for each runLong consecutive repeats; alternating values may expand
Delta encodingInitial value and successive differencesSorted or smooth sequences; differences still need encoding
QuantisationBin/level code and representativeLossy unless every original value is recoverable
Prefix codingVariable-length uniquely decodable codesFrequent 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.

Revise offline: the whole Databases and Data Warehousing handbook as one PDF

58 pages · 2.7 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections