AI Hints
Home›MLOps Tutorial›Security and Privacy Deep Dive
MLOps · Lesson 66 of 80

Privacy Techniques

Protecting people in the data.

⏱ 2 min read▶ Video at 2:42:03🎓 Free
Watch this lesson in the MLOps courseWatch on YouTube ↗Full MLOps course

Models learn from data about people. That is a responsibility. Removing names is not enough, because other columns can identify a person. There are practical techniques that reduce the risk, while keeping the data useful. In this lesson, we meet the main ones, and try one that adds carefully measured noise.

Privacy Techniques
From the video: Privacy Techniques
  • Names removed is not enough
  • Reduce what can identify
  • Add noise on purpose

A toolbox of privacy techniques

Here is a toolbox. Minimisation, collect, and keep, only what you need. Pseudonymisation, replace identifiers with random codes, kept separately. Aggregation, share counts and averages, instead of individual rows. K anonymity, make sure every combination of identifying fields, matches at least k people. Differential privacy, add calibrated random noise to results, so that no single person's presence changes the answer much. And synthetic data, artificial records with realistic statistics. Each trades some usefulness for some protection.

TechniqueIdea
Minimisationcollect and keep only what is needed
Pseudonymisationrandom codes instead of identifiers
Aggregationcounts and averages, not rows
K-anonymityeach combination matches k people
Differential privacycalibrated noise in results
Synthetic dataartificial records, realistic statistics

Differential privacy in a few lines

Here is the core idea of differential privacy. We count how many customers churned, which is the true answer. Then we answer the same question, five times, adding random Laplace noise, whose size depends on a privacy setting, called epsilon. A large epsilon means little noise, and weak privacy. A small epsilon means a lot of noise, and strong privacy. At epsilon ten, the answers are nearly exact. At one, they are off by about one. At point one, the typical error is over twenty. You can see the trade off directly. More privacy costs accuracy, and you choose the balance.

privacy.pypython
import numpy as np
from churn import make_data

df = make_data(5000, seed=1)
true_count = int((df["churn"] == 1).sum())
rng = np.random.default_rng(0)
print("true number of churners:", true_count)

for epsilon in (10.0, 1.0, 0.1):
    noisy = [true_count + rng.laplace(0, 1 / epsilon) for _ in range(5)]
    typical_error = np.mean([abs(n - true_count) for n in noisy])
    print(f"epsilon {epsilon:4}: answers {[round(n) for n in noisy]}  typical error {typical_error:.1f}")
Output
true number of churners: 715
epsilon 10.0: answers [715, 715, 715, 715, 715]  typical error 0.2
epsilon  1.0: answers [717, 715, 716, 715, 717]  typical error 0.9
epsilon  0.1: answers [725, 663, 728, 688, 721]  typical error 21.6
Also uses churn.py
import numpy as np, pandas as pd

def make_data(n, seed, shifted=False):
    rng = np.random.default_rng(seed)
    tenure = rng.integers(1, 72, n)
    charges = rng.normal(65 + (28 if shifted else 0), 18, n).clip(20, 140).round(2)
    calls = rng.poisson(1.5 + (1.5 if shifted else 0), n)
    contract = rng.choice([0, 1, 2], n, p=[.55, .25, .20] if not shifted else [.30, .30, .40])
    logit = -1.0 - 0.04 * tenure + 0.025 * (charges - 65) + 0.45 * calls - 0.8 * contract
    if shifted:
        logit = -0.2 - 0.01 * tenure - 0.01 * (charges - 65) + 0.2 * calls - 0.2 * contract
    churn = (rng.random(n) < 1 / (1 + np.exp(-logit))).astype(int)
    return pd.DataFrame({"tenure": tenure, "charges": charges, "calls": calls, "contract": contract, "churn": churn})

Privacy and usefulness

There is no free lunch. Strong privacy protections reduce what you can learn from the data. Weak ones leave people exposed. So decide deliberately. Ask what the data is for, who could misuse it, and how harmful a leak would be. Sensitive data, like health, or finances, deserves stronger protection. Involve legal and security experts, early. And remember that privacy is not only a technical property. It is also a promise, to the people whose data you hold.

Stronger protection

  • Less exposure for people
  • Less detail for analysis
  • More noise or aggregation
  • Needed for sensitive data

Weaker protection

  • More useful analysis
  • Higher risk if leaked
  • Fewer controls to manage
  • Only for harmless data

Summary

Excellent. Minimise, pseudonymise, aggregate, and, for strong guarantees, add calibrated noise. More privacy costs some accuracy, so choose deliberately, with experts. Next, a different kind of challenge, data that arrives continuously, streams.

Key takeaways
  • Toolbox: minimise to synthetic
  • Epsilon trades privacy for accuracy
  • Privacy is also a promise
Next up: Streaming Features — data that never stops