{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## EE16A: Homework 4\n", "This notebook contains the code for Homework 4 problem 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%pylab inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "from scipy import ndimage as nd\n", "from scipy import misc\n", "from scipy import io" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4 (b)\n", "Load Pattern Image. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pattern = np.load('pattern.npy')\n", "plt.imshow(pattern, cmap='gray', interpolation='nearest')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the command [shape](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html) to find the dimensions of the image. How many eigenvalues do you expect? \n", "\n", "Run the code below to find the eigenvector and eigenvalues of ``pattern`` and sort them in descending order (first eigenvalue/vector corresponds to the largest eigenvalue)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "eig_vals, eig_vectors = np.linalg.eig(pattern)\n", "idx = (eig_vals.argsort())\n", "idx = idx[::-1]\n", "eig_vals = eig_vals[idx]\n", "eig_vectors = eig_vectors[:,idx] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4 (c)\n", "Find the pattern approximation using 100 largest eigenvalues/eigenvectors.\n", "\n", "* Index into above variables to choose the first 100 eigenvalues and eigenvectors.\n", "* You can use the command [np.outer](http://docs.scipy.org/doc/numpy/reference/generated/numpy.outer.html) to find the outer product of two vectors\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rank = 100\n", "S = np.zeros(pattern.shape)\n", "for i in range(rank):\n", " vec_i = eig_vectors[:,i] # i-th largest eigenvector\n", " val_i = eig_vals[i] # i-th largest eigenvalue\n", " S += # Your Code Here\n", " \n", "plt.imshow(S, cmap='gray', vmin=0, vmax=255)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4 (d)\n", "Find the pattern approximation using 50 largest eigenvalues/eigenvectors" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rank = 50\n", "S = np.zeros(pattern.shape)\n", "for i in range(rank):\n", " vec_i = eig_vectors[:,i] # i-th largest eigenvector\n", " val_i = eig_vals[i] # i-th largest eigenvalue\n", " S += # Your Code Here\n", " \n", "plt.imshow(S, cmap='gray', vmin=0, vmax=255)" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }