.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/preprocessing/plot_discretization_strategies.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_preprocessing_plot_discretization_strategies.py: ========================================================== 展示KBinsDiscretizer的不同策略 ========================================================== 此示例展示了KBinsDiscretizer中实现的不同策略: - 'uniform':每个特征的离散化是均匀的,这意味着每个维度的箱宽是恒定的。 - 'quantile':离散化基于分位数值,这意味着每个箱中的样本数量大致相同。 - 'kmeans':离散化基于KMeans聚类过程的质心。 该图显示了离散编码恒定的区域。 .. GENERATED FROM PYTHON SOURCE LINES 15-101 .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_discretization_strategies_001.png :alt: Input data, strategy='uniform', strategy='quantile', strategy='kmeans' :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_discretization_strategies_001.png :class: sphx-glr-single-img .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_blobs from sklearn.preprocessing import KBinsDiscretizer strategies = ["uniform", "quantile", "kmeans"] n_samples = 200 centers_0 = np.array([[0, 0], [0, 5], [2, 4], [8, 8]]) centers_1 = np.array([[0, 0], [3, 1]]) # 构建数据集 random_state = 42 X_list = [ np.random.RandomState(random_state).uniform(-3, 3, size=(n_samples, 2)), make_blobs( n_samples=[ n_samples // 10, n_samples * 4 // 10, n_samples // 10, n_samples * 4 // 10, ], cluster_std=0.5, centers=centers_0, random_state=random_state, )[0], make_blobs( n_samples=[n_samples // 5, n_samples * 4 // 5], cluster_std=0.5, centers=centers_1, random_state=random_state, )[0], ] figure = plt.figure(figsize=(14, 9)) i = 1 for ds_cnt, X in enumerate(X_list): ax = plt.subplot(len(X_list), len(strategies) + 1, i) ax.scatter(X[:, 0], X[:, 1], edgecolors="k") if ds_cnt == 0: ax.set_title("Input data", size=14) xx, yy = np.meshgrid( np.linspace(X[:, 0].min(), X[:, 0].max(), 300), np.linspace(X[:, 1].min(), X[:, 1].max(), 300), ) grid = np.c_[xx.ravel(), yy.ravel()] ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) i += 1 # 使用KBinsDiscretizer对数据集进行转换 for strategy in strategies: enc = KBinsDiscretizer(n_bins=4, encode="ordinal", strategy=strategy) enc.fit(X) grid_encoded = enc.transform(grid) ax = plt.subplot(len(X_list), len(strategies) + 1, i) # 水平条纹 horizontal = grid_encoded[:, 0].reshape(xx.shape) ax.contourf(xx, yy, horizontal, alpha=0.5) # 竖条纹 vertical = grid_encoded[:, 1].reshape(xx.shape) ax.contourf(xx, yy, vertical, alpha=0.5) ax.scatter(X[:, 0], X[:, 1], edgecolors="k") ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) if ds_cnt == 0: ax.set_title("strategy='%s'" % (strategy,), size=14) i += 1 plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.303 seconds) .. _sphx_glr_download_auto_examples_preprocessing_plot_discretization_strategies.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/preprocessing/plot_discretization_strategies.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_discretization_strategies.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_discretization_strategies.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_discretization_strategies.zip ` .. include:: plot_discretization_strategies.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_