.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/neighbors/plot_classification.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_neighbors_plot_classification.py: ================================ 最近邻分类 ================================ 这个示例展示了如何使用 :class:`~sklearn.neighbors.KNeighborsClassifier` 。 我们在鸢尾花数据集上训练这样的分类器,并观察与参数 `weights` 相关的决策边界的差异。 .. GENERATED FROM PYTHON SOURCE LINES 11-15 加载数据 ------------- 在这个例子中,我们使用了鸢尾花数据集。我们将数据分为训练集和测试集。 .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split iris = load_iris(as_frame=True) X = iris.data[["sepal length (cm)", "sepal width (cm)"]] y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 24-30 K-近邻分类器 ------------------------------ 我们希望使用一个k近邻分类器,考虑11个数据点的邻域。由于我们的k近邻模型使用欧几里得距离来寻找最近的邻居,因此在此之前对数据进行缩放是很重要的。有关更详细的信息,请参阅标题为 :ref:`sphx_glr_auto_examples_preprocessing_plot_scaling_importance.py` 的示例。 因此,我们使用 :class:`~sklearn.pipeline.Pipeline` 在使用分类器之前链接一个缩放器。 .. GENERATED FROM PYTHON SOURCE LINES 30-38 .. code-block:: Python from sklearn.neighbors import KNeighborsClassifier from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler clf = Pipeline( steps=[("scaler", StandardScaler()), ("knn", KNeighborsClassifier(n_neighbors=11))] ) .. GENERATED FROM PYTHON SOURCE LINES 39-43 决策边界 ----------------- 现在,我们使用不同的 `weights` 参数值来拟合两个分类器。我们绘制每个分类器的决策边界以及原始数据集,以观察它们之间的差异。 .. GENERATED FROM PYTHON SOURCE LINES 43-75 .. code-block:: Python import matplotlib.pyplot as plt from sklearn.inspection import DecisionBoundaryDisplay _, axs = plt.subplots(ncols=2, figsize=(12, 5)) for ax, weights in zip(axs, ("uniform", "distance")): clf.set_params(knn__weights=weights).fit(X_train, y_train) disp = DecisionBoundaryDisplay.from_estimator( clf, X_test, response_method="predict", plot_method="pcolormesh", xlabel=iris.feature_names[0], ylabel=iris.feature_names[1], shading="auto", alpha=0.5, ax=ax, ) scatter = disp.ax_.scatter(X.iloc[:, 0], X.iloc[:, 1], c=y, edgecolors="k") disp.ax_.legend( scatter.legend_elements()[0], iris.target_names, loc="lower left", title="Classes", ) _ = disp.ax_.set_title( f"3-Class classification\n(k={clf[-1].n_neighbors}, weights={weights!r})" ) plt.show() .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_classification_001.png :alt: 3-Class classification (k=11, weights='uniform'), 3-Class classification (k=11, weights='distance') :srcset: /auto_examples/neighbors/images/sphx_glr_plot_classification_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-82 Conclusion ---------- 我们观察到参数 `weights` 对决策边界有影响。当 `weights="uniform"` 时,所有最近邻对决策的影响相同。而当 `weights="distance"` 时,每个邻居的权重与该邻居到查询点距离的倒数成正比。 在某些情况下,考虑距离可能会改进模型。 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.273 seconds) .. _sphx_glr_download_auto_examples_neighbors_plot_classification.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/neighbors/plot_classification.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_classification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_classification.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_classification.zip ` .. include:: plot_classification.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_