{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n\"\"\"\n=========================================================\nFeature agglomeration\n=========================================================\n\nThese images how similar features are merged together using\nfeature agglomeration.\n\"\"\"\nprint(__doc__)\n\n# Code source: Gaël Varoquaux\n# Modified for documentation by Jaques Grobler\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn import datasets, cluster\nfrom sklearn.feature_extraction.image import grid_to_graph\n\ndigits = datasets.load_digits()\nimages = digits.images\nX = np.reshape(images, (len(images), -1))\nconnectivity = grid_to_graph(*images[0].shape)\n\nagglo = cluster.FeatureAgglomeration(connectivity=connectivity,\n                                     n_clusters=32)\n\nagglo.fit(X)\nX_reduced = agglo.transform(X)\n\nX_restored = agglo.inverse_transform(X_reduced)\nimages_restored = np.reshape(X_restored, images.shape)\nplt.figure(1, figsize=(4, 3.5))\nplt.clf()\nplt.subplots_adjust(left=.01, right=.99, bottom=.01, top=.91)\nfor i in range(4):\n    plt.subplot(3, 4, i + 1)\n    plt.imshow(images[i], cmap=plt.cm.gray, vmax=16, interpolation='nearest')\n    plt.xticks(())\n    plt.yticks(())\n    if i == 1:\n        plt.title('Original data')\n    plt.subplot(3, 4, 4 + i + 1)\n    plt.imshow(images_restored[i], cmap=plt.cm.gray, vmax=16,\n               interpolation='nearest')\n    if i == 1:\n        plt.title('Agglomerated data')\n    plt.xticks(())\n    plt.yticks(())\n\nplt.subplot(3, 4, 10)\nplt.imshow(np.reshape(agglo.labels_, images[0].shape),\n           interpolation='nearest', cmap=plt.cm.nipy_spectral)\nplt.xticks(())\nplt.yticks(())\nplt.title('Labels')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nBasic Picture Viewer\n====================\n\nThis simple image browser demonstrates the scatter widget. You should\nsee three framed photographs on a background. You can click and drag\nthe photos around, or multi-touch to drop a red dot to scale and rotate the\nphotos.\n\nThe photos are loaded from the local images directory, while the background\npicture is from the data shipped with kivy in kivy/data/images/background.jpg.\nThe file pictures.kv describes the interface and the file shadow32.png is\nthe border to make the images look like framed photographs. Finally,\nthe file android.txt is used to package the application for use with the\nKivy Launcher Android application.\n\nFor Android devices, you can copy/paste this directory into\n/sdcard/kivy/pictures on your Android device.\n\nThe images in the image directory are from the Internet Archive,\n`https://archive.org/details/PublicDomainImages`, and are in the public\ndomain.\n\n'''\n\nimport kivy\nkivy.require('1.0.6')\n\nfrom glob import glob\nfrom random import randint\nfrom os.path import join, dirname\nfrom kivy.app import App\nfrom kivy.logger import Logger\nfrom kivy.uix.scatter import Scatter\nfrom kivy.properties import StringProperty\n\n\nclass Picture(Scatter):\n    '''Picture is the class that will show the image with a white border and a\n    shadow. They are nothing here because almost everything is inside the\n    picture.kv. Check the rule named <Picture> inside the file, and you'll see\n    how the Picture() is really constructed and used.\n\n    The source property will be the filename to show.\n    '''\n\n    source = StringProperty(None)\n\n\nclass PicturesApp(App):\n\n    def build(self):\n\n        # the root is created in pictures.kv\n        root = self.root\n\n        # get any files into images directory\n        curdir = dirname(__file__)\n        for filename in glob(join(curdir, 'images', '*')):\n            try:\n                # load the image\n                picture = Picture(source=filename, rotation=randint(-30, 30))\n                # add to the main field\n                root.add_widget(picture)\n            except Exception as e:\n                Logger.exception('Pictures: Unable to load <%s>' % filename)\n\n    def on_pause(self):\n        return True\n\n\nif __name__ == '__main__':\n    PicturesApp().run()\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}