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
.