从最佳连续分布进行抽样
从拟合良好的概率分布进行随机抽样有助于保护隐私。同时,它还能让获授权的一方对数据进行准确的统计分析。
在本练习中,您将对 IBM 数据集中的 monthly_income 列进行匿名化。在上一课中,您已确定连续分布 exponnorm 是最佳拟合。请用它来建模收入。
数据集已作为 hr 提供。
本练习是课程的一部分
Python 中的数据隐私与匿名化
练习说明
- 从
scipy包中导入stats模块。 - 将
exponnorm分布拟合到连续变量monthly_income,以获得该分布的参数,后续据此生成样本。 - 从
exponnorm分布中抽样,并使用.rvs()方法替换monthly_income。将 size 指定为与该列长度相同。 - 将薪资四舍五入到最接近的整数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import stats from scipy
____
# Fit the exponnorm distribution to the continuous variable monthly income
params = ____
# Sample from the exponnorm distribution and replace monthly income
hr['monthly_income'] = ____
# Round the salaries to their closest integer
hr['monthly_income'] = ____
# See the resulting dataset
print(hr.head())