{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "

1  課題:ページランク
1.1  固有値,固有ベクトルから求める場合.
1.2  ベクトルと行列の掛け算から求める場合.
2  課題:行列の対角化
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 課題:ページランク\n", "1. テキスト手順を参考にして,pythonでページランクを求めよ.\n", "1. このような問題ではすべての固有値・固有ベクトルを求める必要はなく,最大の固有値を示す固有ベクトルを求めるだけでよい.初期ベクトルを適当に決めて,何度も推移確率行列を掛ける反復法でページランクを求めよ." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from pprint import pprint\n", "from numpy import array, zeros, diagflat, dot, transpose\n", "from scipy.linalg import eig\n", "\n", "A = array([[0,1,1,1,1,0,1],\n", " [1,0,0,0,0,0,0],\n", " [1,1,0,0,0,0,0],\n", " [0,1,1,0,1,0,0],\n", " [1,0,1,1,0,1,0],\n", " [1,0,0,0,1,0,0],\n", " [0,0,0,0,1,0,0]])\n", "\n", "diag = []\n", "for i in range(0,7):\n", " tmp = 0.0\n", " for j in range(0,7):\n", " tmp += A[i,j]\n", " diag.append(1.0/tmp)\n", "\n", "D = diagflat(diag)\n", "tA = dot(transpose(A),D)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([[ 0.000, 1.000, 0.500, 0.000, 0.250, 0.500, 0.000],\n", " [ 0.200, 0.000, 0.500, 0.333, 0.000, 0.000, 0.000],\n", " [ 0.200, 0.000, 0.000, 0.333, 0.250, 0.000, 0.000],\n", " [ 0.200, 0.000, 0.000, 0.000, 0.250, 0.000, 0.000],\n", " [ 0.200, 0.000, 0.000, 0.333, 0.000, 0.500, 1.000],\n", " [ 0.000, 0.000, 0.000, 0.000, 0.250, 0.000, 0.000],\n", " [ 0.200, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000]])\n", "array([ 0.69945653+0.j, 0.38286042+0.j, 0.32395882+0.j, 0.24296911+0.j,\n", " 0.41231122+0.j, 0.10307780+0.j, 0.13989131+0.j])\n" ] } ], "source": [ "import numpy as np\n", "from pprint import pprint\n", "\n", "np.set_printoptions(formatter={'float': '{: 0.3f}'.format}) \n", "#pprint(A)\n", "#pprint(D)\n", "#pprint(transpose(A))\n", "pprint(dot(transpose(A),D))\n", "x = array([1,0,0,0,0,0,0])\n", "#pprint(x)\n", "#pprint(dot(tA,dot(tA,dot(tA,x))))\n", "l, V = eig(tA)\n", "v0 = V[:,0]\n", "\n", "pprint(v0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 固有値,固有ベクトルから求める場合." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 0.69945653+0.j, 0.38286042+0.j, 0.32395882+0.j, 0.24296911+0.j,\n", " 0.41231122+0.j, 0.10307780+0.j, 0.13989131+0.j])\n" ] } ], "source": [ "l, V = eig(tA)\n", "v0 = V[:,0]\n", "pprint(v0)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ベクトルと行列の掛け算から求める場合." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 0.350, 0.167, 0.117, 0.050, 0.267, 0.050, 0.000])\n", "[ 0.000 0.200 0.200 0.200 0.200 0.000 0.200]\n", "[ 0.350 0.167 0.117 0.050 0.267 0.050 0.000]\n", "[ 0.317 0.145 0.153 0.137 0.112 0.067 0.070]\n", "[ 0.283 0.186 0.137 0.091 0.212 0.028 0.063]\n", "[ 0.321 0.155 0.140 0.110 0.164 0.053 0.057]\n", "[ 0.293 0.171 0.142 0.105 0.184 0.041 0.064]\n", "[ 0.308 0.165 0.140 0.105 0.178 0.046 0.059]\n", "[ 0.302 0.166 0.141 0.106 0.178 0.045 0.062]\n", "[ 0.304 0.166 0.140 0.105 0.180 0.045 0.060]\n", "[ 0.304 0.166 0.141 0.106 0.178 0.045 0.061]\n" ] } ], "source": [ "x = array([1,0,0,0,0,0,0])\n", "pprint(dot(tA,dot(tA,x)))\n", "\n", "for i in range(0,10):\n", " x = dot(tA,x)\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 課題:行列の対角化\n", "\n", "1. 次の行列$A$ の固有値をpythonで求めよ.\n", "また,対角化行列$P$ を求めて,対角化せよ.\n", "$$ A\\, = \\, \\left[ \\begin {array}{ccc} 0&1&-2\\\\\n", "-3&7&-3 \\\\\n", "3&-5&5\n", "\\end {array} \\right]\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 1., 9., 2.])\n", "array([[-0.905, 0.229, -0.707],\n", " [-0.302, 0.688, 0. ],\n", " [ 0.302, -0.688, 0.707]])\n" ] } ], "source": [ "import numpy as np\n", "from pprint import pprint\n", "np.set_printoptions(precision=3, suppress=True)\n", "\n", "A = np.array([[0,1,-2],[-3,7,-3],[3,-5,5]])\n", "l, P = np.linalg.eig( A )\n", "pprint(l)\n", "pprint(P)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([[ 1., 0., 0.],\n", " [-0., 9., -0.],\n", " [ 0., 0., 2.]])\n" ] } ], "source": [ "dA = np.dot(np.linalg.inv(P),np.dot(A,P))\n", "pprint(dA)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.6.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "30px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }