Scikit Learn - RadiusNeighborsClassifier



此分类器名称中的半径代表指定半径 r 内的最近邻域,其中 r 由用户指定的浮点值。因此,顾名思义,此分类器根据每个训练点的固定半径 r 内的邻居数目实现基于学习。让我们通过一个实现示例进一步了解它 −

实现示例

此示例中,我们将通过使用 scikit-learn RadiusNeighborsClassifier 对名为鸢尾花的数据集实施 KNN −

首先,导入鸢尾花数据集,如下所示 −

from sklearn.datasets import load_iris
iris = load_iris()

现在,需要将数据分割成训练数据和测试数据。我们将使用 Sklearn train_test_split 函数将数据分割为 70(训练数据)和 20(测试数据)的比率 −

X = iris.data[:, :4]
y = iris.target
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

接下来,我们将使用 Sklearn 预处理模块执行数据缩放,如下所示 −

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

接下来,从 Sklearn 导入 RadiusneighborsClassifier 类并提供半径值,如下所示 −

from sklearn.neighbors import RadiusNeighborsClassifier
rnc = RadiusNeighborsClassifier(radius = 5)
rnc.fit(X_train, y_train)

示例

现在,创建并预测两个观察值的类别,如下所示 −

classes = {0:'setosa',1:'versicolor',2:'virginicia'}
x_new = [[1,1,1,1]]
y_predict = rnc.predict(x_new)
print(classes[y_predict[0]])

输出

versicolor

完整的工作/可执行程序

from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data[:, :4]

y = iris.target
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

from sklearn.neighbors import RadiusNeighborsClassifier
rnc = RadiusNeighborsClassifier(radius = 5)
rnc.fit(X_train, y_train)

classes = {0:'setosa',1:'versicolor',2:'virginicia'}
x_new = [[1,1,1,1]]
y_predict = rnc.predict(x_new)
print(classes[y_predict[0]])
广告