开始使用免费开始使用

在 PyOD 中使用改进的 z 分数

现在该用 pyod 来检测异常值了。我们将使用 pyod 中的 MAD 估计器来计算改进的 z 分数。该估计器内部已经调用了 median_abs_deviation 函数,因此无需重复之前的步骤。

MAD 估计器已从 pyod.models.mad 导入,数据保存在 prices 中。

本练习是课程的一部分

Python 中的异常检测

查看课程

练习说明

  • threshold 为 3.5 来初始化 MAD()
  • prices 重塑为 2D。
  • 通过同时拟合和预测,使用 madprices 上生成内点/离群点标签。
  • 对离群点进行子集化,离群点用 1 表示。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Initialize with a threshold of 3.5
mad = ____(____=____)

# Reshape prices to make it 2D
prices_reshaped = ____.____(-1, 1)

# Fit and predict outlier labels on prices_reshaped
labels = ____

# Filter for outliers
outliers = ____[____ == ____]

print(len(outliers))
编辑并运行代码