K-Anonymity Risk Checker
Operator: Abdulla Al Fahad. How tools are built and checked
Removing names from a table does not make its people anonymous: age, postcode and sex together are often enough to pick one person out, and a table where most rows are the only one with their combination is a table of individuals. K-anonymity puts a number on that, the size of the smallest group of records that share the same quasi-identifier values.
Key features
- Choose quasi-identifier columns and, optionally, one sensitive column; each quasi-identifier is detected as a number, date, code, category or kept exact, and can be re-typed
- k (smallest group), unique records, number of groups, records below a target k, and a group-size histogram
- Prosecutor risk (worst case 1/k, average groups per record), journalist risk with an optional population-share setting, and marketer risk
- For the sensitive column, distinct and entropy l-diversity and t-closeness, with equal distance for categories and ordered distance for numbers
- k with each column left out in turn, to show which column makes people stand out
How the risk measures are computed and what generalisation does
Records are grouped by the tuple of their quasi-identifier values after generalisation, with a blank counted as a value of its own. The smallest group size is k, and records in groups below the target are at risk. Prosecutor risk assumes the attacker knows the target is in the table, so a record's risk is one over its group size. Journalist risk assumes matching against a larger population: with the share of it that your file covers set to s, a group's risk is estimated as s over its size, capped at 1. Marketer risk is the expected share of records re-identified by an attacker who tries to match every record at once. It equals groups divided by records only when the file is the whole population; with a population share s set, it is the average of s over group size across all records, so it falls with the share the same way journalist risk does.
For the sensitive column, distinct l-diversity counts the values in each group, entropy l-diversity turns the distribution into an effective count (e to the entropy), and t is the largest distance between a group's distribution and the table's: half the summed absolute differences for categories, and the earth mover's distance over the ordered values divided by the number of values minus one for numbers.
How it works
Generalisation levels are built per column. Numbers get bands of 2, 5, 10, 20, 25, 50, 100 and larger, as far as the range allows. Dates go to month, quarter, year, five-year span and decade. Codes such as postcodes keep fewer leading characters. Categories pool values seen fewer than 2, 3, 5, 10 and more times as Other. Every column ends in suppressed (*).
Suggest generalisation runs a greedy search: from the exact values it tries raising each column by one level, scores each step by the at-risk records it removes per unit of detail lost, applies the best, and repeats until no more than the allowed share of records (5% by default) remains below k, the search runs out of steps, or ten seconds pass. The result is a starting point, not an optimum.
Groups are found with integer codes, so 300,000 rows with five columns are grouped in a fraction of a second, and t-closeness is computed from a sorted pass over the rows instead of a table per group.
Direct identifiers are guessed from column names and value shapes (e-mail addresses, phone numbers, columns where every value differs) and offered for removal from the export.
When to run it
- A hospital analyst preparing a release adds the exact admit date to age and ZIP: on the example table every one of the 480 records becomes unique (k 1, 480 groups). Rounding the date to a year still leaves 318 records unique, so the date alone cannot be blamed and the leave-one-out table is where to look.
- A municipal data team publishing survey results needs to know whether five-year age bands are enough, and watches k and the unique count change as she moves the level.
- A data steward screens a customer export before it goes to an analytics vendor, and the identifier hints flag the email and phone columns to be dropped.
- A researcher with a 3% sample of a population sets the population share to 0.03 and reads a journalist risk well below the prosecutor risk.
Running a check
- Drop a CSV or XLSX file, or choose Load example. Up to 300,000 rows are read.
- Tick the quasi-identifier columns and, if you want l-diversity and t-closeness, pick the sensitive column. Set k, l and t targets.
- Read the verdict and the figures, then check the leave-one-out k to see which column matters most.
- Choose Suggest generalisation, or set a level per column by hand, and watch the figures change.
- Download the generalised CSV; records in groups below k can be left out of it.
Example
480 invented admission records (PatientID, Age, ZIP, Sex, AdmitDate, Diagnosis, Charge); quasi-identifiers Age, ZIP and Sex, sensitive column Diagnosis, k target 5
Quasi-identifiers: Age (numbers), ZIP (codes, 12 values), Sex (2 values)
Sensitive: Diagnosis (7 values)
Targets: k 5, l 2, t 0.2, up to 5% of records may be suppressedBefore and after generalisation
Exact values: k 1, 398 groups, 344 unique records, 456 of 480 records below k 5; worst prosecutor risk 100%, average 82.9%
Suggested: Age in bands of 20, ZIP to its first 3 characters, Sex exact
After: 46 groups, 5 unique records, 16 records below k 5 (3.3%); average prosecutor risk 9.6%
Diagnosis still has a group with a single value (l 1), 272 records sit in groups with t above 0.2Limits
K-anonymity protects against identifying a record, not against learning something about a person. A group of 6 where all 6 have the same diagnosis discloses that diagnosis, which is what l-diversity and t-closeness are for.
The result covers the ticked columns only. Other columns, free text, and outside datasets can still single a person out, and the page cannot judge them.
The journalist estimate scales group sizes by one number. It is a rough bound, not a model of a real population file.
Bands and prefixes are chosen from the values in the file. A band of 20 years or a three-character postcode prefix says less about each person but also makes the table less useful for analysis, so check the generalised figures you actually need before releasing it.
- Start with the columns an outsider could plausibly look up, such as age, sex, postcode, occupation and key dates, not every column in the file.
- Set the k target from your own risk policy; 5 is a common floor for internal sharing and higher values are used for public releases.
Common errors and how to fix them
- The sensitive column has more than 5,000 distinct values, so l-diversity and t-closeness were skipped.
- Pick a column with fewer values, or group it first (for example diagnosis codes to chapters) and load the result.
- Check the targets: k at least 2, l at least 1, t and the population share between 0 and 1.
- Enter plain numbers in the target boxes. The page does not guess at values such as 5% or 0.1.1.
Best practices
- Remove direct identifiers first, then check the quasi-identifiers; the export leaves out any column you tick as an identifier.
- Check l and t as well as k whenever a sensitive column exists, and treat a k of 5 with an l of 1 as unfinished.
- Keep the generalisation settings with the release so the next release applies the same rules.
What the suggestion changed in the example
The search took ZIP from five characters to four, then raised Age through bands of 2, 5, 10 and 20, and finally cut ZIP to three characters. Each step removed at-risk records for little detail: the 12 ZIP codes in the file fall into 5 prefixes of three characters, and ages 18 to 94 fall into five bands of 20. Sex was left exact because it already has two values.
Setting the targets
k, l and t answer different questions and are set independently. k limits how small a group can be, so it bounds the chance of picking out a person. l bounds how little variety the sensitive column may have inside a group, which stops a group of identical diagnoses from disclosing one. t bounds how far a group's distribution may stray from the table's, which catches the case where every group has several values but all of them skew the same way. The defaults of 5, 2 and 0.2 are starting points for exploration; the right values depend on who receives the data and what else they hold.
Frequently asked questions
Why is k still 1 after the suggestion when 5% of records may be suppressed?
The search stops once at most 5% of records sit below the target. Five unique records remain in the table, and the export leaves out records in groups smaller than k when that option is ticked, which removes them.
Should I use the CSV anonymizer instead?
That one changes values: it masks and replaces whole columns. This page measures how identifiable the table is and how much coarser the quasi-identifier columns have to get, and only the columns you tick are changed.
Does t-closeness work on a numeric sensitive column?
Yes. When at least 90% of values read as numbers it uses the ordered distance over the sorted distinct values, so two nearby salaries count as closer than two far apart ones. For a category column every difference counts the same.
Why are blanks treated as a value?
A missing value is something an attacker can see, so a record with a blank age is in a group with other blank ages, not with everyone. Treating blanks as values avoids reporting a better k than the file deserves.