Skip to content

Instantly share code, notes, and snippets.

@gakhov
Last active October 17, 2016 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gakhov/dd84ed6eefee5d116714 to your computer and use it in GitHub Desktop.
Save gakhov/dd84ed6eefee5d116714 to your computer and use it in GitHub Desktop.
# Find potential outliers in values array
# and visualize them on a plot
def is_outlier(value, p25, p75):
"""Check if value is an outlier
"""
lower = p25 - 1.5 * (p75 - p25)
upper = p75 + 1.5 * (p75 - p25)
return value <= lower or value >= upper
def get_indices_of_outliers(values):
"""Get outlier indices (if any)
"""
p25 = np.percentile(values, 25)
p75 = np.percentile(values, 75)
indices_of_outliers = []
for ind, value in enumerate(values):
if is_outlier(value, p25, p75):
indices_of_outliers.append(ind)
return indices_of_outliers
indices_of_outliers = get_indices_of_outliers(dist)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(dist, 'b-', label='distances')
ax.plot(
indices_of_outliers,
values[indices_of_outliers],
'ro',
markersize = 7,
label='outliers')
ax.legend(loc='best')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment