Last active
July 6, 2020 06:50
-
-
Save finlytics-hub/86d6d3c5e3022999d8061362d87c8a8b to your computer and use it in GitHub Desktop.
Practical demonstration of using LOF to drop outlier rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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