從最佳連續機率分配進行抽樣
從擬合良好的機率分配進行隨機抽樣有助於維護隱私。同時,也能讓獲授權的單位對資料做出準確的統計分析。
在這個練習中,你要將 IBM 資料集中的 monthly_income 欄位去識別化。在前一個單元中,你判定連續分配 exponnorm 是最合適的擬合。請用它來建模薪資。
資料集已載入為 hr。
本練習屬於課程
Data Privacy and Anonymization in 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())