7
$\begingroup$

I have a data set with rows: 134000 and columns: 200. I am trying to identify the outliers in data set using LocalOutlierFactor from scikit-learn. Although I understand how the algorithm works, I am unable to decide n_neighbors for my data set.

Kindly suggest.

$\endgroup$
3
2
$\begingroup$

One normally uses Grid Search for calculating the optimum parameters in these situations:

from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
import numpy as np

n = 30 # Max number of neighbours you want to consider
param_grid = {'n_neighbors': np.arange(n)}
grid = GridSearchCV(KNeighborsClassifier(), param_grid)

Then given this grid, you can fit it to your data to compute its optimum values (from those you provided, they may not be global optima (or even local if the returned value is one of the extrema of your input range)):

grid.fit(X_train, y_train)

You can view the optimum parameters from your input by calling:

grid.best_params_
>>> {'n_neighbors': ?}

You can automatically select an estimator with said optimum parameters by calling:

model = grid.best_estimator_
y_pred = model.fit(X_train, y_train).predict(X_test)

Note: you can find the optimum values of other parameters by adding them to the input dictionary param_grid.

$\endgroup$
3
  • $\begingroup$ I think the question asks about how many neighbours to choose in the LocalOutlierFactor data pre-processor, not in applying the KNearestNeighbors Classifier. It is a more difficult problem in that Outlier Detection is in general an unsupervised task. $\endgroup$
    – Attack68
    Mar 11 '19 at 20:37
  • $\begingroup$ Thank you for your response, I finally tried this on my data set. Although consuming entire training data set for grid search was taking too much time that why I used n = 300 samples for grid search. I get the best_estimator as 2. What does best_estimator indicate? I am clueless. I looked up internet for grid search and its for hyper tuning. $\endgroup$ Mar 15 '19 at 3:39
  • $\begingroup$ @NehaBhushan best_estimator_ returns the estimator you input automatically loaded with the optimum parameters. You can use it the same way you would use the estimator expicitly, e.g. best_estimator_ is the same as KNeighborsClassifier(n_neighbors=best_params_['n_neighbors']) $\endgroup$
    – iacob
    Mar 15 '19 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.