学习测试代码
"""
# -*- coding: utf-8 -*-
# @Time : 2024/1/25 16:32
# @Author : 王摇摆
# @FileName: code5.py
# @Software: PyCharm
# @Blog :https://blog.csdn.net/weixin_44943389?type=blog
# 异常点去除的原理,3Σ原理
"""
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
np.random.seed(103)
data = np.random.normal(50, 10, 500)
mean_value = np.mean(data)
std_dev = np.std(data)
lower_bound = mean_value - 3 * std_dev
upper_bound = mean_value + 3 * std_dev
outliers = (data < lower_bound) | (data > upper_bound)
plt.figure()
plt.scatter(np.arange(len(data))[~outliers], data[~outliers], 25, 'b', marker='o', label='正常数据')
plt.scatter(np.arange(len(data))[outliers], data[outliers], 40, 'r', marker='d', label='异常值')
plt.axhline(lower_bound, linestyle='--', linewidth=1.5, label='异常值下界')
plt.axhline(upper_bound, linestyle='--', linewidth=1.5, label='异常值上界')
plt.grid(True)
plt.xlabel('数据点索引', fontsize=12)
plt.ylabel('数据值', fontsize=12)
plt.title('数据点和异常值', fontsize=14)
plt.legend(loc='best')
plt.xlim([0, len(data)])
plt.show()