Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vision2020-ex01.ipynb
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Vision2020-ex01.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPEw3KydawExSjza5OflOPx",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/takatakamanbou/55ee0af00777c5595bd46d60afb207b1/vision2020-ex01.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9dZcF12ojSrJ",
"colab_type": "text"
},
"source": [
"# Vision2020-ex01"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SrIIY0RtpBYO",
"colab_type": "text"
},
"source": [
"課題の期限や提出の方法などについては,Visionチーム内に書いてます."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZSx7Ng2FkdUB",
"colab_type": "text"
},
"source": [
"## 準備"
]
},
{
"cell_type": "code",
"metadata": {
"id": "BXtjmCk3jNaH",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ioHPZxlrn_Nw",
"colab_type": "text"
},
"source": [
"## 課題A\n",
"\n",
"以下のプログラムは,[PIP/2019](https://www-tlab.math.ryukoku.ac.jp/wiki/?PIP/2020) 第12回講義資料のQ1(の一部)を解くようにできている.その動作を理解しなさい.ただし...\n",
"- データ行列の要素の並べ方が,授業で説明しているのと違う(転置になってる)ことに注意.\n",
"- 関数 solve で呼んでる np.linalg.solve は,連立方程式を解く関数である.詳しくは https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html へどうぞ.\n",
"- 関数 solve2 については現時点では無視して構わない.\n",
"\n",
"提出するもの: Q1を解くプログラムの実行結果にコメントをつけたもの."
]
},
{
"cell_type": "code",
"metadata": {
"id": "zd9VnEQRjtgq",
"colab_type": "code",
"colab": {}
},
"source": [
"##### 最小二乗法の正規方程式を解く関数\n",
"### solving the linear least squares problem (1)\n",
"#\n",
"# X.shape is assumed to be (N, D+1)\n",
"# y.shape is assumed to be (N,)\n",
"#\n",
"def solve(X, y):\n",
"\n",
" A = np.dot(X.T, X)\n",
" b = np.dot(X.T, y)\n",
"\n",
" # x is the solution of the equation Ax = b\n",
" x = np.linalg.solve(A, b)\n",
"\n",
" return x\n",
"\n",
"\n",
"### solving the linear least squares problem (2)\n",
"#\n",
"def solve2(X, y):\n",
"\n",
" A = np.dot(X.T, X)\n",
" b = np.dot(X.T, y)\n",
"\n",
" # rv[0] is the solution minimizing ||Ax - b||^2\n",
" rv = np.linalg.lstsq(A, b)\n",
"\n",
" return rv[0]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "e62dgLVTkOHR",
"colab_type": "code",
"colab": {}
},
"source": [
"##### PIP2019-12 の Q1 を解くプログラム\n",
"\n",
"x = np.array([0, 4, 8, 20])\n",
"y = np.array([30, 31, 29, 28])\n",
"\n",
"y -= y[0]\n",
"\n",
"N = x.shape[0]\n",
"print('# N =', N)\n",
"\n",
"X = np.vstack((np.ones(N), x)).T\n",
"print(X)\n",
"print('# X.shape =', X.shape)\n",
"\n",
"print(y)\n",
"print('# y.shape =', y.shape)\n",
" \n",
"w = solve(X, y)\n",
"#w = solve2(X, y)\n",
"print('# estimated parameters =', w)\n",
"\n",
"b, a = w # w[0] is b and w[1] is a\n",
"print('# fitted equation is y =', a, '*x +', b)\n",
"\n",
"print(a * 36 + b + 30)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "rzAVumnxmCki",
"colab_type": "text"
},
"source": [
"## 課題B & C 用のデータとその可視化\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pIwsIvDHmEOe",
"colab_type": "code",
"colab": {}
},
"source": [
" ##### 課題B & C 用のデータ\n",
" dat = np.array(\n",
" [[0, 0.7550326945263522],\n",
" [.05, 1.007767522485821],\n",
" [0.1, 0.6361953553367321],\n",
" [0.15, 0.6959238424062661],\n",
" [0.2, 0.595485181368133],\n",
" [0.25, 0.4831892240046355],\n",
" [0.3, 0.159575705011379],\n",
" [0.35, 0.186501916036992],\n",
" [0.4, 0.1567768077057974],\n",
" [0.45, 0.04182438372911931],\n",
" [0.5, -0.06982713339289053],\n",
" [0.55, 0.08544102980639576],\n",
" [0.6, -0.1007904952533244],\n",
" [0.65, -0.1277469644723259],\n",
" [0.7, -0.02542025832150743],\n",
" [0.75, -0.411322875976739],\n",
" [0.8, 0.01309389848625916],\n",
" [0.85, -0.1196592829342933],\n",
" [0.9, 0.07144698650328071],\n",
" [0.95, -0.1983213666516035]]\n",
" )\n",
"\n",
" print(dat.shape)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hSb0K-S7nZJA",
"colab_type": "code",
"colab": {}
},
"source": [
"##### データの可視化\n",
"\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.set_xlim(-0.1, 1.1) # X軸の範囲\n",
"ax.set_ylim(-1, 2) # Y軸の範囲\n",
"ax.scatter(dat[:, 0], dat[:, 1], color=\"red\", label=\"data\")\n",
"ax.legend()\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "yac7v3I7tW5o",
"colab_type": "text"
},
"source": [
"## 課題B\n",
"\n",
"以下のプログラムは,上記のデータに対して最小二乗法によって D 次元の多項式をあてはめたときのパラメータ(多項式の係数)を求めるものである.このプログラムの動作を理解しなさい.\n",
"\n",
"D = 1 として実行すると,直線のあてはめとなる.このときの結果を提出しなさい.提出するもの: 得られるグラフと,当てはめられた直線の式."
]
},
{
"cell_type": "code",
"metadata": {
"id": "_0b1CPSMnLjJ",
"colab_type": "code",
"colab": {}
},
"source": [
"##### dat に対する多項式あてはめ\n",
"\n",
"N = dat.shape[0]\n",
"print('# N =', N)\n",
"\n",
"x = dat[:, 0]\n",
"D = 13 # D is the degree of the polynomial to be fitted\n",
"X = np.zeros((N, D+1))\n",
"X[:, 0] = 1\n",
"for i in range(1, D+1):\n",
" X[:, i] = x**i\n",
"print(X)\n",
"print('# X.shape =', X.shape)\n",
"\n",
"y = dat[:, 1]\n",
"print(y)\n",
"print('# y.shape =', y.shape)\n",
"\n",
"w = solve(X, y)\n",
"print(w)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "1Ehs5FAzpwN0",
"colab_type": "code",
"colab": {}
},
"source": [
"##### 曲線上の点の座標を求める\n",
"\n",
"xmin, xmax = -0.1, 1.1\n",
"xx = np.linspace(xmin, xmax, 1000)\n",
"XX = np.zeros((xx.shape[0], D+1))\n",
"XX[:, 0] = 1\n",
"print(\"# XX.shape =\", XX.shape)\n",
"for i in range(1, D+1):\n",
" XX[:, i] = xx**i\n",
"\n",
"yy = XX @ w"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "7LeCYpJlvqAn",
"colab_type": "code",
"colab": {}
},
"source": [
"##### 結果の可視化\n",
"\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.set_xlim(xmin, xmax) # X軸の範囲\n",
"ax.set_ylim(-1, 2) # Y軸の範囲\n",
"ax.scatter(dat[:, 0], dat[:, 1], color=\"red\", label=\"data\")\n",
"ax.plot(xx, yy, color=\"blue\", label=\"D={}\".format(D))\n",
"ax.legend()\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "jsxIIys6uRcR",
"colab_type": "text"
},
"source": [
"## 課題C\n",
"\n",
"上記のプログラムで D を何通りか変えて,課題Bと同様にグラフを提出しなさい.考察を添えること."
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment