用 z-score 查找离群值
正态分布在自然界中非常常见,也是最常见的分布之一。这就是为什么 z-score 方法常常是检测离群值的快捷方式。
回忆视频中的经验法则:如果某个样本与均值的距离超过 3 个标准差,就可以将其视为极端值。
不过也要注意,使用 z-score 方法需要谨慎。只有当我们有把握数据来自正态分布时,这个方法才合适。否则结果可能具有误导性。
prices 分布已为您加载。
本练习是课程的一部分
Python 中的异常检测
练习说明
- 从相应的
scipy模块中导入zscore函数。 - 计算
prices的 z-score,并将结果保存到scores。 - 创建名为
is_over_3的布尔掩码,用于检查scores的绝对值是否大于 3。 - 使用该掩码筛选
prices中的离群值。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the zscores function
from scipy.____ import ____
# Find the zscores of prices
scores = ____(____)
# Check if the absolute values of scores are over 3
is_over_3 = ____
# Use the mask to subset prices
outliers = ____[____]
print(len(outliers))