Skip to content

Instantly share code, notes, and snippets.

@finlytics-hub
Last active July 6, 2020 06:50
Show Gist options
  • Save finlytics-hub/86d6d3c5e3022999d8061362d87c8a8b to your computer and use it in GitHub Desktop.
Save finlytics-hub/86d6d3c5e3022999d8061362d87c8a8b to your computer and use it in GitHub Desktop.
Practical demonstration of using LOF to drop outlier rows
# Import the required library
from sklearn.neighbors import LocalOutlierFactor
# define LOF class
lof = LocalOutlierFactor() # consider playing around with 'n_neighbors' parameter
# predict whether the numerical columns are outlier or not
yhat = lof.fit_predict(X_train)
# select all rows that are not outliers
mask = yhat != -1
X_train_clean = X_train[mask]
# Don't forget to apply the mask to your target variable as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment