Không có thử nghiệm "tốt nhất" cho một ngoại lệ. Lý tưởng nhất là bạn nên kết hợp thông tin ưu tiên (ví dụ: "Thông số này không được vượt quá x vì blah ...").
Hầu hết các thử nghiệm cho các ngoại lệ sử dụng độ lệch tuyệt đối trung bình, thay vì tỷ lệ phần trăm thứ 95 hoặc một số phép đo dựa trên phương sai khác. Nếu không, phương sai/stddev được tính toán sẽ bị sai lệch nặng nề bởi các ngoại lệ.
Đây là một hàm thực hiện một trong các thử nghiệm ngoại lệ phổ biến hơn.
def is_outlier(points, thresh=3.5):
"""
Returns a boolean array with True if points are outliers and False
otherwise.
Parameters:
-----------
points : An numobservations by numdimensions array of observations
thresh : The modified z-score to use as a threshold. Observations with
a modified z-score (based on the median absolute deviation) greater
than this value will be classified as outliers.
Returns:
--------
mask : A numobservations-length boolean array.
References:
----------
Boris Iglewicz and David Hoaglin (1993), "Volume 16: How to Detect and
Handle Outliers", The ASQC Basic References in Quality Control:
Statistical Techniques, Edward F. Mykytka, Ph.D., Editor.
"""
if len(points.shape) == 1:
points = points[:,None]
median = np.median(points, axis=0)
diff = np.sum((points - median)**2, axis=-1)
diff = np.sqrt(diff)
med_abs_deviation = np.median(diff)
modified_z_score = 0.6745 * diff/med_abs_deviation
return modified_z_score > thresh
Như một ví dụ của việc sử dụng nó, bạn muốn làm điều gì đó như sau:
import numpy as np
import matplotlib.pyplot as plt
# The function above... In my case it's in a local utilities module
from sci_utilities import is_outlier
# Generate some data
x = np.random.random(100)
# Append a few "bad" points
x = np.r_[x, -3, -10, 100]
# Keep only the "good" points
# "~" operates as a logical not operator on boolean numpy arrays
filtered = x[~is_outlier(x)]
# Plot the results
fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.hist(x)
ax1.set_title('Original')
ax2.hist(filtered)
ax2.set_title('Without Outliers')
plt.show()

Nguồn
2012-08-09 15:16:03
Loại cốt truyện nào? Tiêu tan? Biểu đồ? –
Tôi đang vẽ đồ thị với biểu đồ. –