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 में डेटा प्राइवेसी और अज्ञातिकरण

Clients 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 में डेटा प्राइवेसी और अज्ञातिकरण

नामों को उनके जेंडर से मिलाना

डेटासेट में यूनिक नाम जनरेट करना

डुप्लिकेट से बचें
# Faker क्लास इम्पोर्ट करें
from faker import Faker

# Faker क्लास इनिशियलाइज़ करें fake_data = Faker()
# जेंडर के अनुसार नाम जनरेट करें, जो डेटासेट में यूनिक हो clients_df['name'] = [fake_data.unique.name_female() if x == "Female"
else fake_data.unique.name_male()
for x in clients_df['gender']]
Python में डेटा प्राइवेसी और अज्ञातिकरण

नामों को उनके जेंडर से मिलाना

# डेटासेट एक्सप्लोर करें
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 में डेटा प्राइवेसी और अज्ञातिकरण

रैंडम शहर जनरेट करना

# रैंडम शहर जनरेट करना
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 में डेटा प्राइवेसी और अज्ञातिकरण

ईमेल जनरेट करना

# अलग-अलग डोमेन के साथ ईमेल जनरेट करना
clients_df['contact email'] = [fake_data.company_email() 
                               for x in range(len(clients_df))]


# डेटासेट एक्सप्लोर करें 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 में डेटा प्राइवेसी और अज्ञातिकरण

ईमेल जनरेट करना

# कंपनी-जैसे डोमेन और नाम-जैसा यूज़रनेम के साथ ईमेल जनरेट करना
clients_df['Contact email'] = [x.replace(" ", "") + "@" +
                               fake_data.domain_name() 
                               for x in clients_df['Name']]

# परिणामी 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 में डेटा प्राइवेसी और अज्ञातिकरण

तिथियाँ जनरेट करना

दो समयों के बीच तिथियाँ

# दो समयों के बीच तिथियाँ जनरेट करना
clients_df['date'] = [fake_data.date_between(start_date="-10y", end_date="now") 
                               for x in range(len(clients_df))]

# परिणामी 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 में डेटा प्राइवेसी और अज्ञातिकरण

प्रायिकता-वितरण के अनुसार शहर जनरेट करना

किसी वास्तविक डेटासेट की नकल करते समय, हम वास्तविक मानों के नाम लीक होने से बचा सकते हैं.

# numpy इम्पोर्ट करें
import numpy as np


# प्रायिकताएँ प्राप्त करें या निर्दिष्ट करें p = (0.58, 0.23, 0.16, 0.03) cities = ["New York", "Chicago", "Seattle", "Dallas"]
# चुने हुए शहरों में से डिस्ट्रीब्यूशन के अनुसार जनरेट करें clients_df['city'] = np.random.choice(cities, size=len(clients_df), p=p)
Python में डेटा प्राइवेसी और अज्ञातिकरण

प्रायिकता-वितरण के अनुसार शहर जनरेट करना

# परिणामी डेटासेट देखें
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...