.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_dict_face_patches.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_cluster_plot_dict_face_patches.py: 在线学习人脸部件的字典 ========================= 本示例使用一个大型人脸数据集来学习一组20 x 20的图像块,这些图像块构成人脸。 从编程的角度来看,这很有趣,因为它展示了如何使用scikit-learn的在线API通过分块处理一个非常大的数据集。我们的处理方式是一次加载一张图像,并从这张图像中随机提取50个图像块。当我们积累了500个这样的图像块(使用10张图像)后,我们运行在线KMeans对象MiniBatchKMeans的:func:`~sklearn.cluster.MiniBatchKMeans.partial_fit` 方法。 MiniBatchKMeans的详细设置使我们能够看到在连续调用partial_fit时,一些聚类被重新分配。这是因为它们所代表的图像块数量变得太少,选择一个新的随机聚类会更好。 .. GENERATED FROM PYTHON SOURCE LINES 14-16 加载数据 ------------- .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python from sklearn import datasets faces = datasets.fetch_olivetti_faces() .. GENERATED FROM PYTHON SOURCE LINES 23-25 学习图像的字典 ----------------- .. GENERATED FROM PYTHON SOURCE LINES 25-62 .. code-block:: Python import time import numpy as np from sklearn.cluster import MiniBatchKMeans from sklearn.feature_extraction.image import extract_patches_2d print("Learning the dictionary... ") rng = np.random.RandomState(0) kmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True, n_init=3) patch_size = (20, 20) buffer = [] t0 = time.time() # 在线学习部分:对整个数据集循环6次 index = 0 for _ in range(6): for img in faces.images: data = extract_patches_2d(img, patch_size, max_patches=50, random_state=rng) data = np.reshape(data, (len(data), -1)) buffer.append(data) index += 1 if index % 10 == 0: data = np.concatenate(buffer, axis=0) data -= np.mean(data, axis=0) data /= np.std(data, axis=0) kmeans.partial_fit(data) buffer = [] if index % 100 == 0: print("Partial fit of %4i out of %i" % (index, 6 * len(faces.images))) dt = time.time() - t0 print("done in %.2fs." % dt) .. rst-class:: sphx-glr-script-out .. code-block:: none Learning the dictionary... [MiniBatchKMeans] Reassigning 8 cluster centers. [MiniBatchKMeans] Reassigning 5 cluster centers. Partial fit of 100 out of 2400 [MiniBatchKMeans] Reassigning 3 cluster centers. Partial fit of 200 out of 2400 [MiniBatchKMeans] Reassigning 1 cluster centers. Partial fit of 300 out of 2400 [MiniBatchKMeans] Reassigning 3 cluster centers. Partial fit of 400 out of 2400 Partial fit of 500 out of 2400 Partial fit of 600 out of 2400 Partial fit of 700 out of 2400 Partial fit of 800 out of 2400 Partial fit of 900 out of 2400 Partial fit of 1000 out of 2400 Partial fit of 1100 out of 2400 Partial fit of 1200 out of 2400 Partial fit of 1300 out of 2400 Partial fit of 1400 out of 2400 Partial fit of 1500 out of 2400 Partial fit of 1600 out of 2400 Partial fit of 1700 out of 2400 Partial fit of 1800 out of 2400 Partial fit of 1900 out of 2400 Partial fit of 2000 out of 2400 Partial fit of 2100 out of 2400 Partial fit of 2200 out of 2400 Partial fit of 2300 out of 2400 Partial fit of 2400 out of 2400 done in 0.83s. .. GENERATED FROM PYTHON SOURCE LINES 63-65 绘制结果 ---------------- .. GENERATED FROM PYTHON SOURCE LINES 65-83 .. code-block:: Python import matplotlib.pyplot as plt plt.figure(figsize=(4.2, 4)) for i, patch in enumerate(kmeans.cluster_centers_): plt.subplot(9, 9, i + 1) plt.imshow(patch.reshape(patch_size), cmap=plt.cm.gray, interpolation="nearest") plt.xticks(()) plt.yticks(()) plt.suptitle( "Patches of faces\nTrain time %.1fs on %d patches" % (dt, 8 * len(faces.images)), fontsize=16, ) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) plt.show() .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_dict_face_patches_001.png :alt: Patches of faces Train time 0.8s on 3200 patches :srcset: /auto_examples/cluster/images/sphx_glr_plot_dict_face_patches_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.289 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_dict_face_patches.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/cluster/plot_dict_face_patches.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dict_face_patches.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dict_face_patches.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_dict_face_patches.zip ` .. include:: plot_dict_face_patches.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_