用標準差處理離群值
給定一個含有單一變數「age」的 basetable。年齡是捐款者在網路表單中手動填寫,因此容易有打字錯誤並出現離群值。請將所有低於「年齡平均數減去 3 倍年齡標準差」的值,以該下限值取代;並將所有高於「年齡平均數加上 3 倍年齡標準差」的值,以該上限值取代。
本練習屬於課程
Python 中級預測分析
練習說明
- 列印「age」的最大值。
- 計算「age」的平均數與標準差。
- 依據標準差經驗法則計算下限與上限。
- 在 basetable 中新增變數「age_mod」,將離群值替換後,並列印「age _mod」的新最大值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Show the maximum age
print(___["___"].___())
# Calculate mean and standard deviation of age
mean_age = ____["____"].____()
std_age = ____["____"].____()
# Calculate the lower and upper limits
lower_limit = ____ - ____ * ____
upper_limit = ____ + ____ * ____
# Add a variable age_no_outliers to the basetable with outliers replaced
basetable["age_mod"] = (pd.Series([____(____(____, ____), ____)
for a in basetable["age"]]))
print(___["___"].___())