Last active
October 17, 2016 12:39
-
-
Save gakhov/dd84ed6eefee5d116714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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