目录
介绍
背景
使用代码
兴趣点
- GitHub存储库
我们的世界充斥着数字数据。2.5万亿字节是每天产生的数据量。大多数时候,数据是个人的和敏感的,与它相关的人不想透露它。个人和敏感数据的一些示例是姓名、身份证号码、种族等。但是,数据还包含有价值的业务洞察力。那么,我们如何平衡隐私与收集和共享有价值信息的需求?这就是数据匿名化的用武之地。
背景随着Python包对数据匿名化和可扩展性的需求不断增长,我认为创建一个可以提供多种数据匿名化技术且易于使用的库会很好。请看看,我的第一个包——anonympy,希望为开源社区做出贡献并帮助其他用户处理敏感数据。就目前而言,该包提供了匿名化表格(pd.DataFrame)和图像数据的功能。
使用代码作为使用示例,让我们匿名化以下数据集——sample.csv。让我们从安装包开始。可以分两步实现:
pip install anonympy
pip install cape-privacy==0.3.0 --no-deps
接下来,加载我们将尝试匿名化的示例数据集:
import pandas as pd
url = r'https://raw.githubusercontent.com/ArtLabss/open-data-anonimizer/
0287f675a535101f145cb975baf361a96ff71ed3/examples/files/new.csv'
df = pd.read_csv(url, parse_dates=['birthdate'])
df.head()
通过查看专栏,我们可以看到所有内容都是个人和敏感的。因此,我们必须将相关技术应用于每一列。我们需要初始化我们的dfAnonymizer对象。
from anonympy.pandas import dfAnonymizer
anonym = dfAnonymizer(df)
在应用任何函数之前,了解列是什么数据类型很重要。让我们检查一下数据类型,看看我们可以使用哪些方法。
# check dtypes
print(anonym.numeric_columns)
print(anonym.categorical_columns)
print(anonym.datetime_columns)
... ['salary', 'age']
... ['first_name', 'address', 'city', 'phone', 'email', 'web']
... ['birthdate']
# available methods for each data type
from anonympy.pandas.utils import available_methods
print(available_methods())
... `numeric`:
* Perturbation - "numeric_noise"
* Binning - "numeric_binning"
* PCA Masking - "numeric_masking"
* Rounding - "numeric_rounding"
`categorical`:
* Synthetic Data - "categorical_fake"
* Synthetic Data Auto - "categorical_fake_auto"
* Resampling from same Distribution - "categorical_resampling"
* Tokenazation - "categorical_tokenization"
* Email Masking - "categorical_email_masking"
`datetime`:
* Synthetic Date - "datetime_fake"
* Perturbation - "datetime_noise"
`general`:
* Drop Column - "column_suppression"
在我们的dataset中,我们有6个分类列,2个数字列和1个datetime类型列。此外,从返回的available_methods列表中,我们可以找到每种数据类型的函数。
让我们在age列中添加一些随机噪声,将salary列中的值四舍五入并部分屏蔽email列。
anonym.numeric_noise('age')
anonym.numeric_rounding('salary')
anonym.categorical_email_masking('email')
# or with a single line
# anonym.anonymize({'age':'numeric_noise',
'salary':'numeric_rounding',
'email':'categorical_email_masking'})
调用to_df()以查看更改,或者为了简短的摘要,请调用info()方法。
anonym.info()
现在我们想用假名字替换first_name列中的名字。为此,我们首先必须检查Faker是否有相应的方法。
from anonympy.pandas.utils import fake_methods
print(fake_methods('f')) # agrs: None / 'all' / any letter
... factories, file_extension, file_name, file_path, firefox, first_name,
first_name_female, first_name_male, first_name_nonbinary, fixed_width,
format, free_email, free_email_domain, future_date, future_datetime
很好,Faker有一个方法叫做first_name,让我们排列列。
anonym.categorical_fake('first_name')
# passing a dictionary is also valid -> {column_name: method_name}
# anonym.categorical_fake({'first_name': 'first_name_female'}
事实证明,检查fake_methods其他列名,Faker 也有address和city的方法。该web列可以用url方法代替,phone可以用phone_number代替。
anonym.categorical_fake_auto() # this will change `address` and `city`
# because column names correspond to method names
anonym.categorical_fake({'web': 'url', 'phone': 'phone_number'}) # here we need to specify,
# because column names differs from method name
剩下要匿名化的最后一列是birthdate。由于我们有包含相同信息的age列,我们可以使用column_supression方法删除此列。但是,为了清楚起见,让我们添加一些噪音。
anonym.datetime_noise('birthdate')
仅此而已。现在让我们比较匿名化前后的数据集。
前:
后:
现在,您的数据集可以安全地公开发布。
兴趣点数据隐私和保护是数据处理的重要组成部分,应引起重视。每个人都希望他的个人和敏感数据受到保护和安全。因此,在本文中,我向您展示了如何使用anonympy进行简单的匿名化和python的伪匿名化。这个库不应该用作万能的魔杖,您仍然必须彻底了解您的数据和正在应用的技术,并始终牢记您的最终目标。这是包的GitHub存储库——anonympy。
https://www.codeproject.com/Articles/5324569/anonympy-Data-Anonymization-with-Python