類別資料去識別化

Data Privacy and Anonymization in Python

Rebeca Gonzalez

Instructor

概化(Generalization)

    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
    Age    Gender    Department    Condition
0    <40   F         Finance       Anxiety disorders
1    >=40  M         Production    Bronquitis
2    <40   F         Finance       Dysthymia
3    <40   F         Production    Dysthymia
4    >=40  M         Marketing     Flu
Data Privacy and Anonymization in Python

類別資料的概化

# See the dataset
hr.head()
    Age    BusinessTravel        Department                EducationField    EmployeeNumber
0    41    Travel_Rarely         Sales                     Life Sciences     1
1    49    Travel_Frequently     Research & Development    Life Sciences     2
2    37    Travel_Rarely         Research & Development    Other             4
3    33    Travel_Frequently     Research & Development    Life Sciences     5
4    27    Travel_Rarely         Research & Development    Medical           7
Data Privacy and Anonymization in Python

類別資料(Categorical data)

可能值有限或固定。

  • 種族
  • 性別
  • 家鄉
  • 年齡層
  • 教育程度
  • 喜歡的電影與偏好
Data Privacy and Anonymization in Python

類別資料去識別化

Education Field 中類別值的直方圖

Data Privacy and Anonymization in Python

類別資料去識別化

     Department                EducationField
0    Sales                     Life Sciences
1    Research & Development    Life Sciences
2    Research & Development    Other
3    Research & Development    Life Sciences
4    Research & Development    Medical

原始資料集

     Department                EducationField
0    Sales                     Medical
1    Research & Development    Marketing
2    Research & Development    Life Sciences
3    Research & Development    Other
4    Research & Development    Life Sciences

從原始資料集中 educationField 欄位的機率分佈抽樣後得到的資料集。

Data Privacy and Anonymization in Python

從資料中抽樣

美國人口普查公開發佈其蒐集的民眾資料樣本。

用於計算大規模統計模式:

  • 平均數
  • 變異數
  • 叢集
Data Privacy and Anonymization in Python

探索分佈

# Show the absolute frequencies of each unique value
hr['EducationField'].value_counts()
Life Sciences       606
Medical             464
Marketing           159
Technical Degree    132
Other                82
Human Resources      27
Name: EducationField, dtype: int64
Data Privacy and Anonymization in Python

探索分佈

# Generate a bar plot for the categories
df['BusinessTravel'].value_counts().plot(kind='bar')

Business Travel 欄位中類別值的長條圖

Data Privacy and Anonymization in Python

探索分佈

# Obtain the absolute frequencies of each unique value
counts = hr['EducationField'].value_counts()

# Print the list of indexes
print(counts.index)
Index(['Life Sciences', 'Medical', 'Marketing', 
      'Technical Degree', 'Other', 'Human Resources'],
       dtype='object')
Data Privacy and Anonymization in Python

探索分佈

# Probability distributions of each unique value
counts = df['EducationField'].value_counts(normalize=True)
Life Sciences       0.412245
Medical             0.315646
Marketing           0.108163
Technical Degree    0.089796
Other               0.055782
Human Resources     0.018367
Name: EducationField, dtype: float64
Data Privacy and Anonymization in Python

探索分佈

# Values of the frequencies of each unique value
df['EducationField'].value_counts(normalize=True).values
array([0.4122449 , 0.31564626, 0.10816327, 0.08979592, 0.05578231,
       0.01836735])
Data Privacy and Anonymization in Python

從相同分佈抽樣

# Sample from a probability distribution
hr_sample['EducationField']= np.random.choice(counts.index, 
                                              p=counts.values, 
                                              size=len(hr))


# See resulting dataset hr.head()
    Age    BusinessTravel        Department                EducationField    EmployeeNumber
0    41    Travel_Rarely         Sales                     Life Sciences     1
1    49    Travel_Frequently     Research & Development    Medical           2
2    37    Travel_Rarely         Research & Development    Marketing         4
3    33    Travel_Frequently     Research & Development    Technical Degree  5
4    27    Travel_Rarely         Research & Development    Medical           7
Data Privacy and Anonymization in Python

從相同分佈抽樣

# Show the absolute frequencies of each category
hr['EducationField'].value_counts()
Life Sciences       606
Medical             464
Marketing           159
Technical Degree    132
Other                82
Human Resources      27
Name: EducationField, dtype: int64
# Show the frequencies of the resulting column
hr_sample['EducationField'].value_counts()
Life Sciences       604
Medical             493
Marketing           158
Technical Degree    120
Other                61
Human Resources      34
Name: EducationField, dtype: int64
Data Privacy and Anonymization in Python

一起來練習吧!

Data Privacy and Anonymization in Python

Preparing Video For Download...