Anomaly Detection in Python
Bekhruz (Bex) Tuychiev
Data Science Writer
n_neighbors
parameterimport pandas as pd
males_transformed = pd.read_csv("males_transformed.csv")
males_transformed.head()
abdominalextensiondepthsitting acromialheight acromionradialelength ...
0 0.365531 0.427976 0.113152
1 -0.492137 -0.724973 -0.529301
2 0.857097 -0.146048 0.357496
3 -0.462610 -1.521525 -1.467860
4 -0.026349 2.151957 1.985876
from pyod.models.lof import LOF
# Fit lof = LOF(n_neighbors=20, metric="manhattan") lof.fit(males_transformed) print(lof.labels_)
array([0, 0, 0, ..., 0, 0, 1])
# Isolate the outliers
probs = lof.predict_proba(males_transformed)
is_outlier = probs[:, 1] > 0.55
outliers = males_transformed[is_outlier]
len(outliers)
2
n_neighbors
is the most importantn_neigbors
:method
is always largest
Anomaly Detection in Python