is there a method on python to remove anomalies with the k-nearest neighbors method?

Viewed 111

Good morning all, I hope you are well ! I am working on a project to detect and correct anomalies with the statistical method (I am still a beginner in programming), and with the K-nn method I was able to detect all the anomalies that exist on my data, but I blocked when i tried removing them, can someone help me find a way to undo these anomalies with the k-nearest neighbors method (i am working with python). Best regards.

1 Answers

use the boxplot to get a visual on your outliers then use iqr to remove them from the dataframe.

plt.boxplot(df['value'])
plt.show()

iqr=iqr(df['value'])

Q1=np.quantile(df['value'],0.25) 
Q3=np.quantile(df['value'],0.75)

filter:
df['value'] < Q1 - 1.5 * IQR is an outlier or
df['value'] > Q3 + 1.5 * IQR
Related