π AI/ML Security with Python β Membership Inference π
Posted on: November 23, 2026
Description:
A model can reveal more than a predicted class. Its confidence may provide clues about whether a particular record appeared in training. This lab tests that ideaβand counts the mistakes.
What we know in this lab
We generate 2,000 synthetic records. A random forest trains on 1,000 members; the other 1,000 are nonmembers. We control the split, so membership truth is known.
Half of each group calibrates the membership rule. The remaining 500 members and 500 nonmembers form a separate attack evaluation set. None of the nonmembers trains the target model.
This is a simple confidence-threshold experiment, not a reproduction of a shadow-model attack.
Turn confidence into a guess
probs = model.predict_proba(X)
scores = probs.max(axis=1)
guess_member = scores >= threshold
The maximum class probability measures classification confidence. It is not the probability of training membership. We test whether higher confidence is a useful membership signal.
Choose the threshold using only known-member/nonmember calibration records, then freeze it before evaluation. That known-membership calibration is a favorable assumption that may not be available in practice.
The measured errors
For the unrestricted forest, calibration selected 0.87. On the separate balanced evaluation set:
| Actual membership | Guessed member | Guessed nonmember |
|---|---|---|
| Member | 362 | 138 |
| Nonmember | 107 | 393 |
Membership accuracy was 75.5%, compared with a 50% majority-class baseline. The false-positive rate was 21.4%, and member recall was 72.4%. ROC AUC was 0.844, describing ranking across thresholds rather than certainty for an individual record.
The model falsely implicated 107 unseen records and missed 138 actual training records. Confidence is a clue, not proof.
Reduce capacity and measure again
Using the same data and splits, we limit tree depth to 4 and independently recalibrate the threshold.
| Target model | Class test accuracy | Membership accuracy | Membership ROC AUC |
|---|---|---|---|
| Unrestricted forest | 83.9% | 75.5% | 0.844 |
| Depth 4 | 79.0% | 53.1% | 0.549 |
This particular heuristic becomes much weaker, while classification performance also declines. That is an observed trade-off in this setup, not a privacy guarantee.
Limits that matter
These results use one synthetic dataset, fixed split, and seed. They are not a general attack success rate. Membership prevalence is 50% here; different real-world prevalence changes precision and can make accuracy misleading.
Other methods can use losses, reference models, shadow models, or predicted labels. Removing confidence scores alone does not establish privacy. Reducing overfitting and unnecessary output exposure can be useful safeguards, but stronger privacy claims require appropriate evaluation and, where needed, formal privacy methods.
Takeaway: evaluate the signal and its errors. Do not treat confidence as evidence that a specific personβs record was used for training.
Run it
Membership_Inference.ipynb contains the complete executed experiment and confidence-distribution charts. experiment.py provides a command-line reproduction; install requirements.txt first.
No comments yet. Be the first to comment!