用数据泛化进行匿名化

Python 中的数据隐私与匿名化

Rebeca Gonzalez

Data engineer

数据泛化

一种将数据值替换为更不精确值的技术。

目的是去除部分标识符,同时保留分析所需的可用性。

可将具体值替换为更泛化的值:"Dancer" -> "Artist"

     name               location
0    Amanda Hooper      146 Rodgers Field\nGregoryview, MS 71630
1    Sarah Smith        817 Garcia Shoal\nJonesville, AR 30299
2    Sean Boyd III      1938 90th St\nDallas, TX 59715
     name               location
0    Amanda Hooper      998 Boone Estate\nReedborough, MS 71630
1    Sarah Smith        7255 Shelby Rapids Apt. 455\nKarenland, AK 30299
2    Sean Boyd III      791 Crist Parks\nGreenton, TX 59715
Python 中的数据隐私与匿名化

数据泛化

# 原始数据
df_employees.head()
     first name   last name    age   ssn
0    Amber        Brown        91    798-29-4785
1    William      Gibson       34    431-66-8381
2    Daniel       Lee          92    825-91-5550
3    Andrea       Stevenson    64    188-59-3544
4    Julie        Horn         35    020-60-6388
# 泛化数据:年龄分箱,SSN 掩码
generalized_df.head()
    First name    Last name    Age        SSN
0    Amber        Brown        (80, 99]    798-**-****
1    William      Gibson       (30, 50]    431-**-****
2    Daniel       Lee          (80, 99]    825-**-****
3    Andrea       Stevenson    (60, 80]    188-**-****
4    Julie        Horn         (30, 50]    020-**-****

34 岁将被归入 30–50 区间。这也称为分箱。

Python 中的数据隐私与匿名化

数据汇总

左侧为具体职业列表,右侧为对应的汇总类别。

Python 中的数据隐私与匿名化

医疗数据集

# 浏览数据集
df_medical.head()
    age    gender    department    condition
0    30    F         Finance       Anxiety disorders
1    42    M         Production    Bronchitis
2    35    F         Marketing     Dysthymia
3    39    F         Production    Dysthymia
4    40    M         Marketing     Flu
Python 中的数据隐私与匿名化

医疗数据集

# 查看 age 变量的直方图
df_medical['age'].hist(bins=15)

使用 hist 方法生成的数据集中 age 属性的直方图

Python 中的数据隐私与匿名化

泛化

# 通过转为二元数据进行泛化
df_medical['age'] = df_medical['age'].apply(lambda x:">=40" if x>=40 else "<40" )


# 查看结果 df_medical.head()
    age    gender    department    condition
0    <40   F         Finance       Bronchitis
1    >=40  M         Production    Bronquitis
2    <40   F         Finance       Dysthymia
3    <40   F         Production    Dysthymia
4    >=40  M         Marketing     Flu
Python 中的数据隐私与匿名化

顶端与底端编码

# 查看 age 变量的直方图
df_medical['age'].hist(bins=15)

使用 hist 方法生成的数据集中 age 属性的直方图

  • 少数人小于25岁,少数人高于55岁
  • 对这些离群人群设定边界,降低再识别风险
  • 适用于类别样本极少时,尤其分布两端
Python 中的数据隐私与匿名化

顶端编码

# 筛选受影响的行
df_medical[df_medical['age'] >= 55]
      age   gender   department     condition
26    56    F        Production     Flu
65    55    M        Finance        Dysthymia
126   59    F        Production     Anxiety disorders
139   58    F        Finance        Dysthymia
142   59    M        Marketing      Flu
145   57    M        Marketing      Anxiety disorders
Python 中的数据隐私与匿名化

顶端编码实现

# 将 age 顶端编码为 55
df_medical.loc[df_medical['age'] > 55, 'age'] = 55


# 筛选受影响的行 df_medical[df_medical['age'] >= 55]
      age   gender   department     condition
26    55    F        Production     Flu
65    55    M        Finance        Dysthymia
126   55    F        Production     Anxiety disorders
139   55    F        Finance        Dysthymia
142   55    M        Marketing      Flu
145   55    M        Marketing      Anxiety disorders
Python 中的数据隐私与匿名化

底端编码

# 查看 age 变量的直方图
df_medical['age'].hist(bins=15)

应用顶端编码后,使用 hist 方法生成的数据集中 age 属性的直方图。直方图右侧不再显示对应最高值的离群点

Python 中的数据隐私与匿名化

底端编码实现

# 将 age 底端编码为 25
df_medical.loc[df['age'] < 25, 'age'] = 25


# 查看 age 变量的直方图 df_medical['age'].hist(bins=15)

应用顶端与底端编码后,使用 hist 方法生成的数据集中 age 属性的直方图。无离群点

Python 中的数据隐私与匿名化

数据泛化与隐私模型

与抑制和掩码结合,并遵循 K-匿名等隐私模型时效果更佳。

设定数据集需满足的条件,以控制披露风险。

下一章将学习如何实现该隐私模型!

Python 中的数据隐私与匿名化

Time to practice!

Python 中的数据隐私与匿名化

Preparing Video For Download...