Post

Data Normalization

Data Normalization

Big data can be challenging especially for organized and analyze due to its volume, variety, and complexity. To address this issue, normalization is commonly used.

However, normalization exists in 2 main forms: Database Normalization and Data Mining normalization.

Although they share similar goal of improving data usability, they are applied for different stages and purposes.

๐Ÿ—„๏ธ Database normalization

Database normalization is used in database design to reduce redundancy and improve data integrity. It organizes data into multiple related tables involves dividing larger tables into smaller and ensures consistency.

Why Normalize?

  • Eliminates duplicate data
  • Reduces update, insert, and delete anomalies
  • Improves data consistency
  • Makes the database easier to maintain

There is a lot of stage form in database normalization that is 1NF, 2NF, 3NF and higher normal form typically used only in specialized databased design like BCNF, 4NF, 5NF.

Example (unnormalized table)

Original table

order_idcustomer_nameproductsquantitiespricesshipping_address
101AlicePhone, Charger1, 2500, 20Jakarta, Indonesia
102BobLaptop11000Bandung, Indonesia

Problems:

  • Multiple products in one column
  • Quantities & prices not atomic
  • Hard to search/filter products

1NF

Rule:

  • Each column must contain atomic (single) values.
  • No repeating groups or arrays.

Convert table

order_idcustomer_nameproductquantitypriceshipping_address
101AlicePhone1500Jakarta, Indonesia
101AliceCharger220Jakarta, Indonesia
102BobLaptop11000Bandung, Indonesia

2NF

Rule:

  • Must be already in 1NF.
  • No partially dependency on a composite key.

Assume composite key: (order_id, product) in 1NF table.

Dependencies:

  • order_id โ†’ customer_name
  • order_id โ†’ shipping_address

Decompose table:

orders

order_idcustomer_nameshipping_address
101AliceJakarta, Indonesia
102BobBandung, Indonesia

order_items

order_idproductquantityprice
101Phone1500
101Charger220
102Laptop11000

3NF

Rule:

  • Must be in 2NF.
  • No transitive dependency.

Observe: customer_name โ†’ shipping_address in 2NF orders table.

Problem: A customerโ€™s address is determined by customer, not directly by the order.

Decompose table:

customers

customer_idcustomer_nameshipping_address
C1AliceJakarta, Indonesia
C2BobBandung, Indonesia

orders

order_idcustomer_id
101C1
102C2

order_items

order_idproductquantityprice
101Phone1500
101Charger220
102Laptop11000

Boyce-Codd Normal Form (BCNF)

Rule:

  • Must be in 3NF.
  • For every functional dependency X โ†’ Y, X must be super key (candidate key).

Observed: dependency product โ†’ price in 3NF order_items table.

Problem: A productโ€™s price is determined by the product itself, not by the order. So a non-key attribute (product) is determining another attribute (price).

BCNF issue: product โ†’ price because product is not a super key in order_items therefore, a non-key attribute is determining another attriubute.

Decompose table:

customers

customer_idcustomer_nameshipping_address
C1AliceJakarta, Indonesia
C2BobBandung, Indonesia

orders

order_idcustomer_id
101C1
102C2

products

productprice
Phone500
Charger20
Laptop1000

order_items

order_idproductquantity
101Phone1
101Charger2
102Laptop1

4NF

Rule:

  • Must be in 3NF/BCNF.
  • No non-trivial multivalued dependencies (MVDs).

Because previous table already normalized so in this example we change the orders table in 3NF/BCNF like below to make example bad design for 4NF.

order_idcustomer_idproductcoupon
101C1PhoneDISC10
102C2PhoneNEWUSER

Observe:

  • order_id โ†’โ†’ product
  • order_id โ†’โ†’ coupon

Problem: the table wourld force a cross-product combination like below and leads to redudancy and write (insert, update, delete) anomalies.

order_idproductcoupon
101PhoneDISC10
101PhoneNEWUSER
101LaptopDISC10
101LaptopNEWUSER

Decompose table:

orders (bcnf + 4NF base table)

order_idcustomer_id
101C1
102C2

order_products

order_idproduct
101Phone
102Phone

order_coupons

order_idcoupon
101DISC10
102NEWUSER

5NF / Join Normal Form (JNF)

Rule:

  • Must be in 4NF.
  • Canโ€™t be decomposed further without losing information, except by joining on candidate keys.

5NF or Join is rare in real systems case in this example we gonna use real world scenario Suppliers - Parts - Projects 4NF tables.

supplies

supplierpartproject
S1P1J1
S1P2J1
S1P1J2
S1P2J2

Observe independent relationship:

  1. supplier โ†’ part.
  2. supplier โ†’ project.
  3. part โ†” project compatibility (independent rule).

Problem: supplies table is incorrectly enforcing a ternary combination, which leads to redudancy and write (insert, update, delete) anomalies.

Decompose table:

supplier_parts

supplierpart
S1P1
S1P2

supplier_projects

supplierproject
S1J1
S1J2

part_projects

partproject
P1J1
P2J1
P1J2
P2J2

๐Ÿ“Š Data mining normalization

Data mining normalization is used in data preprocessing for machine learning. It scales numerical values into a standard range so that features contribute equally to the model.

When normalization is important

It is especially important for:

  • k-Nearest Neighbors (KNN)
  • k-Means clustering
  • Neural networks
  • Principal Component Analysis (PCA)
  • Support Vector Machines (SVM)

But less critical for algorithm:

  • Decision trees
  • Random forests

Common method:

Min-max normalization

Transform values into a specified range.

  • Range usually [0, 1]
  • Outliers sensitivity high
$$ x' = \frac{x - x_{\min}}{x_{\max} - x_{\min}} $$

Example:

If values range from 10 to 50 and $x = 30:$

$$ x' = \frac{30 - 10}{50 - 10} = 0.5 $$

Z-score normalization (standardization)

Transform data to have a mean of 0 and a standard deviation of 1.

  • No fixed range
  • Outliers sensitivity moderate
$$ z = \frac{x-\mu}{\sigma} $$

Where:

  • $\mu$ = mean
  • $\sigma$ = standard deviation

Example:

If $x = 70$, mean $ = 50$, standard deviation $ = 10$:

$$ z = \frac{70 - 50}{10} = 2 $$

Advantages:

  • Less affected by outliers than Min-max normalization.
  • Commonly used in statistical analysis.

Decimal scaling normalization

Moves the decimal point based on the maximum absolute value.

  • Range typically (-1, 1)
  • Outliers sensitivity moderate
$$ x' = \frac{x}{10^j} $$

Where $j$ is the smallest integer such that all normalized values fall between -1 and 1.

Example:

Suppose we have $x = [12, 345, -67, 8]$

Where:

  • Maximum absolute value = 345
  • It has 3 digits โ†’ so $j = 3$
Original ($xโ€™$)CalculationNormalized
12$\frac{12}{10^3}$0.012
345$\frac{345}{10^3}$0.345
-67$\frac{-67}{10^3}$-0.67
8$\frac{8}{10^3}$0.008

Robust scaling (Median and IQR)

Use median and interquartial range (IQR), which are much less sensitive to extreme values.

  • No fixed range
  • Outliers sensitivity low
\[x' = \frac{x - \mathrm{median}(x)}{\mathrm{IQR}(x)}\]

Where:

  • median(x) = middle value of the data
  • $IQR(x) = Q3 - Q1$ (75th percentile minus 25th percentile)

Example:

Suppose we have dataset: [10, 12, 14, 16, 18, 20, 100]

Notice: 100 is an outlier

Where:

  • median = 16
  • Lower half = [10, 12, 14] โ†’ $Q1 = 12$
  • Upper half = [18, 20, 100] โ†’ $Q3 = 20$
  • $IQR = Q3 - Q1 = 20 - 12 = 8$
Original ($xโ€™$)CalculationScaled
10$\frac{10-16}{8}$-0.75
12$\frac{12-16}{8}$-0.50
14$\frac{14-16}{8}$-0.25
16$\frac{16-16}{8}$0
18$\frac{18-16}{8}$0.25
20$\frac{20-16}{8}$0.50
100$\frac{100-16}{8}$10.5

๐Ÿ’ก Common idea

Both types of normalization aim to make data better organized, more useful, improve quality and effienciency but they solve different problems:

  • Database normalization โ†’ makes data storage cleaner and more reliable.
  • Data mining normalization โ†’ makes data values easier for algorithms to process.

How they connect in a real project

For example in a real system such as an e-commerce platform, both types of normalization are used at different stages of the data pipeline:

  1. Data storage (Database Normalization)
    Store data customers, orders, and products in separate well structured tables. This ensure consistency and avoids duplication in the database.
  2. Data extraction for analysis
    Data retrieved from databased for analytics or machine learning tasks.
  3. Data processing (Data Mining Normalization)
    Features such as purchase amounts, customer age, and order frequency are normalized This ensured all featured on similar scale before training a machine learning model.
  4. Model training
    A machine learning model is trained using the processed data for tasks like recommendation or prediction.
This post is licensed under CC BY 4.0 by the author.