python kNN_for_anomaly_detection.PY
or run kNN_for_anomaly_detection.ipynb in jupyter notebook.
This script uses the KNN(k-Nearest Neighbors) algorithm to detect anomaly and outlier with hands-on example codes.
iris.csv (Very small and famous data,This is a good sample data for this example. Each sample contains four features.)
Result picture,and PDF.
We will use the famous Iris dataset(https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv). In this example,only two characteristics are used("sepal_length", "sepal_width").
This script can run on any system, including Windows, Linux, and OS. But here are some requirements about module version.
Currently, the code supports python >=3.5
- scikit-learn (==0.23.2)
- numpy (==1.19.2)
- matplotlib
- pandas
see results.pdf
https://towardsdatascience.com/k-nearest-neighbors-knn-for-anomaly-detection-fdf8ee160d13