K Nearest Neighbor Optimization Parameters Explained
These are the most commonly adjusted parameters with k Nearest Neighbor Algorithms. Let’s take a deeper look at what they are used for and how to change their values:
n_neighbor: (default 5) This is the most fundamental parameter with kNN algorithms. It regulates how many neighbors should be checked when an item is being classified.
weights: (default: “uniform“) Another important parameter, weights, signifies how weight should be distributed between neighbor values.
“uniform” : This value will cause weights to be distributed equally among all neighbor values.
“distance” : This value will cause weights to be distributed based on their distance (inversely correlated). Closer neighbors will have a higher weight in the algorithm.
[callable] : You can also define a function and assign it to this parameter. Weights will be custom based on the array you are providing.
algorithm: (default: “auto”) Signifies the algorithm that will be used to compute nearest neighbors.
“auto“: Uses most suitable algorithm automatically based on dataset.
“ball_tree“: Uses BallTree algorithm
“kd_tree“: Uses KDTree algorithm
“brute“: Uses brute-force search
knn = KNeighborsClassifier(n_neighbors=40)
knn = KNeighborsClassifier(n_neighbors=40, weights="distance")
knn = KNeighborsClassifier(algorithm="brute")
More kNN Optimization Parameters for fine tuning
Further on, these parameters can be used for further optimization, to avoid performance and size inefficiencies as well as suboptimal algorithm results:
Udacity up to 75% off
Udacity Data Science Courses
You can now enroll in Udacity Data Science and coding courses with up to 75% off discount and enable lucrative career opportunities.
*Financial aid available