Machine Learning Practice
KNeighborsClassifier
RadiusNeighborsClassifier
Step 1: Instantiate a KNeighborsClassifer estimator without passing any arguments to it to create a classifer object.
from sklearn.neighbors import KNeighborsClassifier
kneighbor_classifier = KNeighborsClassifier()
Step 2: Call fit method on KNeighbors classifier object with training feature matrix and label vector as arguments.
# Model training with feature matrix X_train and
# label vector or matrix y_train
kneighbor_classifier.fit(X_train, y_train)
n_neighbors
parameter.
kneighbor_classifier = KNeighborsClassifier(n_neighbors = 3)
n_neighbors = 5
weights
kneighbor_classifier = KNeighborsClassifier(weights= 'uniform')
Default:
weights
parameter also accepts a user-defined function which takes an array of distances as input, and returns an array of the same shape containing the weights.def user_weights(weights_array):
return weights_array
kneighbor_classifier = KNeighborsClassifier(weights=user_weights)
Example:
algorithm
‘ball_tree’ will use BallTree
‘kd_tree’ will use KDTree
‘brute’ will use a brute-force search
‘auto’ will attempt to decide the most appropriate algorithm based on the values passed to the fit method.
kneighbor_classifier = KNeighborsClassifier(algorithm='auto')
Default:
leaf_size
For 'ball_tree' and 'kd_tree' algorithms, there are some other parameters to be set.
metric
p
Step 1: Instantiate a RadiusNeighborsClassifer estimator without passing any arguments to it to create a classifer object.
from sklearn.neighbors import RadiusNeighborsClassifier
radius_classifier = RadiusNeighborsClassifier()
Step 2: Call fit method on RadiusNeighbors classifier object with training feature matrix and label vector as arguments.
# Model training with feature matrix X_train and
# label vector or matrix y_train
radius_classifier.fit(X_train, y_train)
radius
parameter.
radius_classifier = RadiusNeighborsClassifier(radius=1.0)
r = 1.0
weights
algorithm
‘uniform’
‘distance’
[callable] function
default = 'uniform'
‘ball_tree’
‘kd_tree’
‘brute’
default = ‘auto’
‘auto’
leaf_size
metric
p
default = 30
default = 'minkowski'
default = 2