9951 explained code solutions for 126 technologies


python-scikit-learnNearest neighbors classifier with a radius voting


from sklearn import datasets, neighbors, model_selection

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = neighbors.RadiusNeighborsClassifier(0.75)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)ctrl + c
from sklearn import

import module from scikit-learn

load_iris

loads Iris dataset

model_selection.train_test_split

splits given X and y datasets to test (25% of values by default) and train (75% of values by default) subsets

.RadiusNeighborsClassifier(

K neighbors classification model with a given radius neighbors voting

0.75

radius to vote within

.fit(

train model with a given features and target variable dataset

.predict(

predict target variable based on given features dataset


Usage example

from sklearn import datasets, neighbors, model_selection

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = neighbors.RadiusNeighborsClassifier(0.75)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print(y_pred)
output
[1 0 1 2 1 0 2 2 2 2 0 0 2 1 1 1 1 0 0 2 1 2 2 0 2 0 2 1 2 1 2 1 2 1 1 1 0
 1]