4
$\begingroup$

I have the following list:

 lst={{0., 0.}, {0.1, 0.0998334}, {0.2, 0.198669}, {0.3, 0.29552}, {0.4, 
  0.389418}, {0.5, 0.479426}, {0.6, 0.564642}, {0.7, 0.644218}, {0.8, 
   0.717356}, {0.9, 0.783327}, {1., 0.841471}, {1.1, 0.891207}, {1.2, 
  0.932039}, {1.3, 0.963558}, {1.4, 0.98545}, {1.5, 0.997495}, {1.6, 
  0.999574}, {1.7, 0.991665}, {1.8, 0.973848}, {1.9, 0.9463}, {2., 
  0.909297}, {2.1, 0.863209}, {2.2, 0.808496}, {2.3, 0.745705}, {2.4, 
   0.675463}, {2.5, 0.598472}, {2.6, 0.515501}, {2.7, 0.42738}, {2.8, 
  0.334988}, {2.9, 0.239249}, {3., 0.14112}, {3.1, 
  0.0415807}, {3.2, -0.0583741}, {3.3, -0.157746}, {3.4, -0.255541}, \
  {3.5, -0.350783}, {3.6, -0.44252}, {3.7, -0.529836}, {3.8, \
  -0.611858}, {3.9, -0.687766}, {4., -0.756802}, {4.1, -0.818277}, \
  {4.2, -0.871576}, {4.3, -0.916166}, {4.4, -0.951602}, {4.5, \
  -0.97753}, {4.6, -0.993691}, {4.7, -0.999923}, {4.8, -0.996165}, \
  {4.9, -0.982453}, {5., -0.958924}, {5.1, -0.925815}, {5.2, \
 -0.883455}, {5.3, -0.832267}, {5.4, -0.772764}, {5.5, -0.70554}, \
  {5.6, -0.631267}, {5.7, -0.550686}, {5.8, -0.464602}, {5.9, \
  -0.373877}, {6., -0.279415}, {6.1, -0.182163}, {6.2, -0.0830894}, \
  {6.3, 0.0168139}, {6.4, 0.116549}, {6.5, 0.21512}, {6.6, 
    0.311541}, {6.7, 0.40485}, {6.8, 0.494113}, {6.9, 0.57844}, {7., 
     0.656987}, {7.1, 0.728969}, {7.2, 0.793668}, {7.3, 0.850437}, {7.4, 
    0.898708}, {7.5, 0.938}, {7.6, 0.96792}, {7.7, 0.988168}, {7.8, 
   0.998543}, {7.9, 0.998941}, {8., 0.989358}, {8.1, 0.96989}, {8.2, 
    0.940731}, {8.3, 0.902172}, {8.4, 0.854599}, {8.5, 0.798487}, {8.6, 
   0.734397}, {8.7, 0.662969}, {8.8, 0.584917}, {8.9, 0.501021}, {9., 
    0.412118}, {9.1, 0.319098}, {9.2, 0.22289}, {9.3, 0.124454}, {9.4, 
     0.0247754}, {9.5, -0.0751511}, {9.6, -0.174327}, {9.7, -0.271761}, \
    {9.8, -0.366479}, {9.9, -0.457536}, {10., -0.544021}, {1, 0.5}, {3, 
     0.5}};

If I plot this list using ListPlot, I would see that there are two outlier points. I know there are some posts here to detect those points. However, what I am looking for is an efficient command that tests if a point is surrounded by neighboring points; if no points exist in its proximity, then the code should consider that point as an outlier. This is a small code that I want to use for very long and 3D list.

Thanks for help.

$\endgroup$
  • $\begingroup$ Nearest? ClusteringComponents? $\endgroup$ – Henrik Schumacher Dec 10 '17 at 15:21
7
$\begingroup$

If you know the neighborhood radius, you could use the following approach:

outlier[data_, d_] := Pick[
    data,
    Length /@ Nearest[data, data, {Infinity, d}],
    1
]

For you example data set, the neighborhood radius is about .2, so:

outliers = outlier[lst, .2]

{{1, 0.5}, {3, 0.5}}

Here is a plot:

ListPlot[lst /. Thread[outliers -> Thread@Style[outliers, Red]]]

enter image description here

$\endgroup$
  • $\begingroup$ Many thanks for your help. Greatly appreciated. Just a quick question. What if I have 3D points. Will this apply? $\endgroup$ – qahtah Dec 10 '17 at 15:45
  • $\begingroup$ @qahtah It should work with any dimension. $\endgroup$ – Carl Woll Dec 10 '17 at 16:17
  • $\begingroup$ Thanks Carl for your help. $\endgroup$ – qahtah Dec 10 '17 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.