AI Glossary / Graphs and symbolic AI / Beginner
Data compression
Definition
Data compression represents information using fewer bits than a chosen original representation, when the data and method permit it. Lossless compression allows exact reconstruction; lossy compression discards some information to achieve a smaller representation under an accepted quality trade-off.
What it means
Files often contain repetition or predictable patterns. A compression method can replace those patterns with shorter descriptions. The decoder interprets the description to recover the data, or an approximation of it in a lossy method.
The distinction between lossless and lossy is about what can be recovered. A lossless archive should restore the original bytes exactly. A compressed photograph may instead preserve a visually useful approximation while changing individual pixel values.
Compression is related to AI through data storage, transmission and representation, and learned models can form part of a compression system. However, ordinary compression does not require machine learning. Shrinking a neural network is also a separate use of the word compression, involving model parameters or computation rather than necessarily encoding a data file.
How it works
An encoder finds a representation suited to the input's regularities. Repeated sequences can be replaced with references, frequently occurring symbols can receive shorter codes, and predictable values can be represented as differences from a prediction.
The decoder must know how to interpret the result. That information may be fixed by a format, included in the file, or supplied through a shared dictionary or model. Headers, tables and metadata contribute to the total size, so they must be counted when measuring savings.
Lossless schemes preserve enough information to reverse the encoding. Lossy schemes add a choice about which distinctions to remove, such as rounding numerical values or quantizing transformed image information. The acceptable distortion depends on the intended task.
No lossless method can shorten every possible input. If all messages of a given length had distinct shorter encodings, there would not be enough shorter bit strings to represent them all uniquely. Some inputs therefore remain similar in size or become larger, particularly once format overhead is included.
Three practical examples
- A business compresses text logs losslessly so that archived records can be restored exactly.
- A website uses appropriately encoded images to reduce transfer size while checking that text and important visual detail remain readable.
- A sensor service compresses repeated or predictable readings before transmission, with explicit rules about whether exact values must survive.
Think of it this way
Think of replacing “ten consecutive blue tiles” with a count and a color. That description is shorter than listing every tile separately when the run is long.
It is less useful when colors change at every position, and the listener must know how counts and colors are encoded. Compression succeeds because of particular patterns, not simply because the encoder gives the file a new name.
Worked example
Counting the real bytes saved
Suppose a toy format stores each original symbol in one byte. The input is AAAAABBBBBCCCCCC: five As, five Bs and six Cs, giving 16 bytes.
A run-length format stores each run as a one-byte count followed by a one-byte symbol. The three runs require 3×2=6 bytes. Add a two-byte header identifying the format, and the complete compressed representation is 8 bytes.
Using original size divided by compressed size, the compression ratio is 16/8=2:1. The storage reduction is (16-8)/16=50%. Counting only the run payload would exaggerate the saving.
For ABABABAB, there are eight one-symbol runs. The same scheme needs 8×2+2=18 bytes to represent an original 8-byte string, so it expands the input. These figures use a deliberately simplified format; they are not predictions for a general-purpose archive program.
Illustrative case study
A fictional inspection firm, Maple Images, wants to reduce the transfer size of photographs used by a visual defect detector. A small file-size trial looks attractive, but a review includes images containing fine scratches and small printed identifiers.
The team compares detector behavior and human readability across encoding settings, keeping the originals as the reference. It discovers that visual acceptability for a thumbnail is not the same requirement as preserving the evidence needed for a detailed inspection.
The firm records the chosen format, settings and evaluation task. For exact numeric logs it uses lossless storage, while image choices are evaluated against the inspection workflow. It does not assume that one compression setting is appropriate for every dataset.
Common misconceptions and mistakes
- Every file gets smaller when compressed. Already compressed or poorly matched data can expand.
- Lossless means the entire editing pipeline preserved everything. A lossless file format cannot restore information discarded before encoding.
- Smaller files always make a system faster. Encoding and decoding consume computation, and access patterns also matter.
- Compression provides secrecy. A compact representation is not encryption or an access-control mechanism.
Business use
Compression can reduce storage and network requirements, but evaluate the complete workload. Include encoding time, decoding time, metadata, random access needs and downstream quality.
For AI inputs, test whether compression changes the information needed for classification, extraction or measurement. If exact reconstruction is required, verify a round trip against the original bytes. For lossy inputs, choose task-relevant measures instead of relying only on file size or a generic visual score. Record the ratio convention because some reports use compressed size divided by original size instead.
People also ask
Is a ZIP file necessarily smaller than its contents?
No. Compression and archive overhead can make small or incompressible inputs larger.
Can a PNG image be lossless?
PNG uses lossless image-data compression, but converting or editing the source before saving can still change the represented image.
Can compression reduce internet transfer time?
It can when reduced bytes outweigh processing and protocol costs. The result depends on the connection, device and data.
Check your understanding
A 16-byte input becomes a 6-byte payload plus a 2-byte required header. Is the compression ratio 16:6 when measuring the full stored file?
Answer: No. The full compressed size is 8 bytes, so the stated original-to-compressed ratio is 2:1.
Related terms
- Lossless compression
- Lossy compression
- Entropy coding
- Run-length encoding
- dictionary coding
- quantisation
- compression ratio
Learn next
Encode a long repeated sequence and an alternating sequence with the toy run-length scheme. Count the header as well as the payload. Then decide which of a transaction log, a website photograph and a measurement file requires exact recovery and explain why.
More in Graphs and symbolic AI
- D-Separation - D-separation is a graphical criterion for determining which conditional independences are implied by a directed acyclic graph.
- Dancing links - Dancing links is a technique for efficiently removing and restoring elements in linked data structures during backtracking.
- Data complexity - Data complexity measures the computational difficulty of a task as the data grows while the query, rule set or other specified logical description is held...
- Data-oriented parsing - Data-oriented parsing, or DOP, is an approach to analyzing language by combining fragments of previously analyzed utterances.
- Datalog - Datalog is a declarative logic-programming language for expressing facts and rules over relations.
- Davis-Putnam algorithm - The Davis - Putnam algorithm is a resolution-based procedure for deciding satisfiability of propositional formulas in conjunctive normal form.
Sources
RFC 1951 defines the lossless DEFLATE format and its use of repeated-sequence references and Huffman coding: https://www.rfc-editor.org/rfc/rfc1951
W3C PNG specification describes lossless image storage and its encoding stages: https://www.w3.org/TR/png-3/
The toy encoding arithmetic and Maple Images case are original illustrations.
Previous: Data complexity Next: Data compression ratio