{ "cells": [ { "cell_type": "code", "execution_count": 33, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:43:08.537640Z", "start_time": "2019-12-27T02:43:08.533650Z" } }, "outputs": [], "source": [ "from scipy import *\n", "from pylab import *" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:43:09.656647Z", "start_time": "2019-12-27T02:43:09.640690Z" } }, "outputs": [], "source": [ "img = imread(\"images/initial.png\")[:,:,0]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:43:10.377720Z", "start_time": "2019-12-27T02:43:10.337827Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gray()\n", "figure(1) # prepare the canvas for figure 1.\n", "imshow(img)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:43:14.446841Z", "start_time": "2019-12-27T02:43:14.309209Z" } }, "outputs": [], "source": [ "m, n = img.shape\n", "U, S, Vt = svd(img)\n", "S = resize(S, [m,1]) * eye(m,n) # see below for a demo." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:43:15.148964Z", "start_time": "2019-12-27T02:43:15.105081Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = 20\n", "figure(2)\n", "imshow(dot(U[:,1:k], dot(S[1:k,1:k], Vt[1:k,:])))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demo for multiplying array with \"\\*\" operation" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:42:42.627910Z", "start_time": "2019-12-27T02:42:42.623921Z" } }, "outputs": [], "source": [ "col_v = resize(asarray([1,2]), [2,1])\n", "eye_mat = eye(2,3)\n", "result = col_v * eye_mat" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:42:49.248210Z", "start_time": "2019-12-27T02:42:49.244221Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "col_v" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:42:52.084627Z", "start_time": "2019-12-27T02:42:52.080638Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 1., 0.]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eye_mat" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2019-12-27T02:42:55.141454Z", "start_time": "2019-12-27T02:42:55.137465Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 2., 0.]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }