如何从箱形图中获取异常值及其坐标的值

时间:2014-02-24 21:30:58

标签: matlab

如果数据是矩阵,是否可以调用boxplot的属性来获取异常值和异常值的元素坐标?

为了这个例子,让我们说我有这个数据数组显示两个异常值,我该怎么做?

A = [ -2 -1 2 2.1 2.2 2.5 2.6 2.8 3 3.2 3.4 3.5 4.2 4.5 5];

figure(1)
s = subplot(6,1,1:5);
o = boxplot(A, 'whisker', 1.5);
title('A')
axis(s,[0 2 -3 7])

s1 = subplot(6,1,6);
plot(A,0,'bo')
axis(s1,[-3 7 -1 1])
set(s1,'YTickLabel',[],'YTick',[])

%@chappjc posted a working method
s_Outliers = findobj(o,'Tag','Outliers');
outliers_of_A = get(s_Outliers,'YData');
disp('The values of the outliers');
disp(outliers_of_A);

感谢您的答案 - 它完美无缺。 @chappjc发布了一个工作方法 - 使用此方法是否可以删除outliers_of_A形式A的值?

3 个答案:

答案 0 :(得分:3)

异常值标记的句柄包含实际的异常值数据。创建boxplot,将句柄结构保存为输出参数。从R2014a开始,异常值句柄是列表中的最后一个(如果没有显示缺口)或倒数第二个(如果显示缺口),但您可以使用findobj来确定:

>> hb = boxplot(A, 'whisker', 1.5);
>> hOutliers = findobj(hb,'Tag','Outliers');
>> yy = get(hOutliers,'YData')
yy =
    -2    -1

答案 1 :(得分:2)

虽然@chappjc已经就如何使用Boxplot对象本身给出了一个很好的答案,但我在下面给出了数学方法:

Q1 = quantile(A,0.25);
Q3 = quantile(A,0.75);
Spread = 1.5*(Q3-Q1);
MaxValue = Q3 + Spread;
MinValue = Q1 - Spread;
A( A>MaxValue | A<MinValue)
ans =

-2    -1

答案 2 :(得分:0)

获取离群值索引的一种更简单的方法是使用方法“ isoutlier()”

TF = isoutlier(A,'mean') % The method 'mean' is the same used by the boxplot for getting the outlier values

%this will give you a true/false vector

outlier_index = find(TF==1)

%Here you have the index of your outlier

oulier_values = A(oulier_index)