การสร้างชุดข้อมูลจริงด้วย Faker

ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

Rebeca Gonzalez

Data engineer

การสร้างข้อมูลด้วย Faker

fake_data.name()
'Kelly Clark'
fake_data.name_male()
'Antonio Henderson'
fake_data.name_female()
'Jennifer Ortega'
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

DataFrame ของลูกค้า

clients_df
gender    active
0    Female    No
1    Male      Yes
2    Male      No
3    Female    Yes
4    Male      Yes
...    ...    ...
1465    Male    Yes
1466    Male    Yes
1467    Male    Yes
1468    Male    Yes
1469    Male    Yes
1470 rows × 2 columns
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างชุดข้อมูลด้วย Faker

  • สร้างชื่อที่ไม่ซ้ำกันและสอดคล้องกับเพศ
  • สร้างเมืองแบบสุ่ม
  • สร้างเมืองที่กำหนดตามการกระจายความน่าจะเป็น
  • สร้างอีเมล
  • สร้างวันที่ในช่วงเวลาที่กำหนด
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

ทำให้ชื่อสอดคล้องกับเพศ

การสร้างชื่อที่ไม่ซ้ำในชุดข้อมูล

หลีกเลี่ยงการซ้ำกัน
# Import the Faker class
from faker import Faker

# Initialize a Faker class fake_data = Faker()
# Generate a name according to the gender, that will be unique in the dataset clients_df['name'] = [fake_data.unique.name_female() if x == "Female"
else fake_data.unique.name_male()
for x in clients_df['gender']]
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

ทำให้ชื่อสอดคล้องกับเพศ

# Explore the dataset
clients_df
    gender    active    name
0    Female    No       Michelle Lang
1    Male      Yes      Robert Norton
2    Male      No       Matthew Brown
3    Female    Yes      Sherry Jones
4    Male      Yes      Steven Vega
...    ...    ...    ...
1465    Male    Yes    Bradley Smith
1466    Male    Yes    Tyler Yu
1467    Male    Yes    Mr. Joshua Gallegos
1468    Male    Yes    Brian Aguilar
1469    Male    Yes    David Johnson
1470 rows × 3 columns
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างเมืองแบบสุ่ม

# Generating a random city
clients_df['city'] = [fake_data.city() 
                      for x in range(len(clients_df))]


clients_df.head()
    Gender    Active    Name              City
0    Female   No        Stacy Hooper      Reedland
1    Male     Yes       Michael Rogers    North Michellestad
2    Male     No        James Sanchez     West Josephburgh
3    Female   Yes       Taylor Berger     Hermanton
4    Male     Yes       Joshua Coleman    South Amandaland
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างอีเมล

# Generating emails with different domains
clients_df['contact email'] = [fake_data.company_email() 
                               for x in range(len(clients_df))]


# Explore the dataset clients_df.head()
    gender     active    name            city                  contact email
0    Female    No        Stacy Hooper    Reedland              [email protected]
1    Male      Yes       Michael Rogers  North Michellestad    [email protected]
2    Male      No        James Sanchez   West Josephburgh      [email protected]
3    Female    Yes       Taylor Berger   Hermanton             [email protected]
4    Male      Yes       Joshua Coleman  South Amandaland      [email protected]
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างอีเมล

# Generating emails with company like domains and username similar to name
clients_df['Contact email'] = [x.replace(" ", "") + "@" +
                               fake_data.domain_name() 
                               for x in clients_df['Name']]

# Explore the resulting DataFrame clients_df.head()
    gender     active    name            city                  contact email
0    Female    No        Stacy Hooper    Reedland              [email protected]
1    Male      Yes       Michael Rogers  North Michellestad    [email protected]
2    Male      No        James Sanchez   West Josephburgh      [email protected]
3    Female    Yes       Taylor Berger   Hermanton             [email protected]
4    Male      Yes       Joshua Coleman  South Amandaland      [email protected]
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างวันที่

วันที่ระหว่างสองช่วงเวลา

# Generating dates between two times
clients_df['date'] = [fake_data.date_between(start_date="-10y", end_date="now") 
                               for x in range(len(clients_df))]

# Explore the resulting DataFrame clients_df.head()
    gender     active    name            city                  contact email                  date
0    Female    No        Stacy Hooper    Reedland              [email protected]       2019-11-20
1    Male      Yes       Michael Rogers  North Michellestad    [email protected]       2015-02-22
2    Male      No        James Sanchez   West Josephburgh      [email protected]  2015-12-11
3    Female    Yes       Taylor Berger   Hermanton             [email protected]       2012-12-13
4    Male      Yes       Joshua Coleman  South Amandaland      [email protected]      2014-05-22
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างเมืองตามการกระจายความน่าจะเป็น

เมื่อจำลองชุดข้อมูลจริง สามารถหลีกเลี่ยงการเปิดเผยชื่อค่าจริงได้

# Import numpy
import numpy as np


# Obtain or specify the probabilities p = (0.58, 0.23, 0.16, 0.03) cities = ["New York", "Chicago", "Seattle", "Dallas"]
# Generate the cities from the selected ones following a distribution clients_df['city'] = np.random.choice(cities, size=len(clients_df), p=p)
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

การสร้างเมืองตามการกระจายความน่าจะเป็น

# See the resulting dataset
clients_df.head()
    gender     active    name            city        contact email                  date
0    Female    No        Stacy Hooper    Chicago     [email protected]       2019-11-20
1    Male      Yes       Michael Rogers  New York    [email protected]       2015-02-22
2    Male      No        James Sanchez   New York    [email protected]  2015-12-11
3    Female    Yes       Taylor Berger   Chicago     [email protected]       2012-12-13
4    Male      Yes       Joshua Coleman  New York    [email protected]      2014-05-22
ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

มาฝึกกันเถอะ!

ความเป็นส่วนตัวของข้อมูลและการทำให้ข้อมูลไม่ระบุตัวตนด้วย Python

Preparing Video For Download...