Skip to content

Instantly share code, notes, and snippets.

@vishalkuo
Last active March 31, 2021 21:53
Show Gist options
  • Save vishalkuo/f4aec300cf6252ed28d3 to your computer and use it in GitHub Desktop.
Save vishalkuo/f4aec300cf6252ed28d3 to your computer and use it in GitHub Desktop.
Remove outliers using numpy. Normally, an outlier is outside 1.5 * the IQR experimental analysis has shown that a higher/lower IQR might produce more accurate results. Interestingly, after 1000 runs, removing outliers creates a larger standard deviation between test run results.
import numpy as np
def removeOutliers(x, outlierConstant):
a = np.array(x)
upper_quartile = np.percentile(a, 75)
lower_quartile = np.percentile(a, 25)
IQR = (upper_quartile - lower_quartile) * outlierConstant
quartileSet = (lower_quartile - IQR, upper_quartile + IQR)
resultList = []
for y in a.tolist():
if y >= quartileSet[0] and y <= quartileSet[1]:
resultList.append(y)
return resultList
@prasadsonar2
Copy link

what is outlier constant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment