-
-
Save takatakamanbou/49272f0479025ac5d5aa76f0b252302f to your computer and use it in GitHub Desktop.
PIP2020-12-note2.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "PIP2020-12-note2.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"toc_visible": true, | |
"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/49272f0479025ac5d5aa76f0b252302f/pip2020-12-note2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NI3mY1FGZWgV", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# 2020年度パターン情報処理第12回講義資料その2\n", | |
"\n", | |
"この科目のウェブサイトへ https://www-tlab.math.ryukoku.ac.jp/wiki/?PIP/2020\n", | |
"\n", | |
"![hoge](https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/PIP-logo-96x96.png)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ffaGVK2o5VWe", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## はじめに" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "0IpujRN_Wuib", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### これは何?\n", | |
"\n", | |
"これは,Google Colaboratory(以下 Google Colab) という,Google が提供しているサービスを利用して作成した資料です.Notebook と呼びます.クラウド上に仮想的に構築された Linux マシン上で Python のプログラムを実行することができます." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "QRNjWashW4pr", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Notebook の動かし方\n", | |
"\n", | |
"この Notebook では,上の方のセルで作った変数や関数を後のセルで使うことがあります.そのため,上の方のセルを実行せずに下の方を実行すると,エラーになることがあります.上から順に実行していきましょう.\n", | |
"\n", | |
"また,メニューの「ランタイム」から,「すべてのセルを実行」したりすることも可能です." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "1jfSXIY6VNxP", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## 準備\n", | |
"\n", | |
"以下のセルは,プログラムを実行するための準備を行うためのものです.このセルを実行してから,先へ進みましょう." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "IFYQk7ONZENz", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# 科学技術計算のライブラリ NumPy のモジュールを np という名前で使えるようにする\n", | |
"import numpy as np\n", | |
"# グラフを描くためのライブラリ matplotlib の pyplot を plt という名前でインポート\n", | |
"import matplotlib.pyplot as plt\n", | |
"# コンピュータビジョン・画像処理のためのライブラリ OpenCV のモジュール cv2 をインポート\n", | |
"#import cv2\n", | |
"# Google Colab 上へ/からのファイルのアップロード/ダウンロードのためのほげ\n", | |
"#from google.colab import files" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Xx-2MzFHeAfD", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## ★ ゴリゴリ君" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "XE0FwT1QeTFh", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### データの入手と表示" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "PFmCtyoteJE5", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# ゴリゴリ君データの入手\n", | |
"!wget -nc https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/gorigori-mac.csv\n", | |
"! ls -l" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UxlkqQNbeWgd", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# データ先頭5行を表示.数値がカンマで区切られた CSV (Comma-Separated Values)という形式.\n", | |
"! head -n 5 gorigori-mac.csv" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "vwh_T6W2eYHG", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# データを読み込む\n", | |
"data = np.loadtxt('gorigori-mac.csv', delimiter = ',')\n", | |
"print(data.shape) # 配列 data の大きさを表示\n", | |
"print(data[:5, :]) # 最初の5行を表示" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VvLmKu8ZeZtO", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# 0列目をx軸,1列目をy軸としてグラフを描く\n", | |
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n", | |
"ax.scatter(data[:, 0], data[:, 1], color=\"red\")\n", | |
"ax.set_xlim(0, 40) # x 軸の範囲指定\n", | |
"plt.show()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "IstgYcaWedy6", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### 問題設定\n", | |
"\n", | |
"上記のグラフの元となっているデータは,ほげおくんがある店でアイス「ゴリゴリ君」を売るアルバイトを50日間やった際の,1日毎の「気温」(単位は度,記号 $x$ で表すことにする)と「ゴリゴリ君売上数」(単位は個,記号 $y$ で表すことにする)を表している.データの0列目およびグラフの横軸が気温,1列目および縦軸が売上数である.\n", | |
"\n", | |
"\n", | |
"50日間アルバイトをやったほげおくんは,将来を見込まれてこの店の店長をまかされることになった.大学で機械学習を学んだほげおくんは,その知識を活かして売上を予測してみようと思い立った.気温 $x$ から売上数 $y$ を予測できれば,翌日の予想気温からその日の売上数を予測し,ゴリゴリ君を適切な数仕入れることができるだろう,というわけである.\n", | |
"「ゴリゴリ売って大儲けほげ〜」\n", | |
"\n", | |
"\n", | |
"ほげおくんに代わって予測式を求めてみよう." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "maXJjeG6ewEx", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### 最小二乗法による直線のあてはめ\n", | |
"\n", | |
"$ y = ax+b $ という関係が成り立つと仮定して,パラメータ $(a, b)$ として最適な値を求める." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "FCBDka0CesQu", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# 最小二乗法\n", | |
"coeff = np.polyfit(data[:, 0], data[:, 1], 1) # 第3引数はあてはめる多項式の次数.戻り値はあてはめた多項式の係数.\n", | |
"# パラメータ(係数)を a, b という変数に入れておく\n", | |
"a, b = coeff\n", | |
"print(a, b)\n", | |
"f_est = np.poly1d(coeff)\n", | |
"print(f_est) # 得られた係数を使ってあてはめた直線の式を表示" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VZSNNMoqeziH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# グラフを描く\n", | |
"xr = np.linspace(0, 40, 100)\n", | |
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n", | |
"ax.scatter(data[:, 0], data[:, 1], color=\"red\")\n", | |
"ax.plot(xr, f_est(xr), '-')\n", | |
"ax.set_xlim(0, 40) # x 軸の範囲指定\n", | |
"plt.show()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "9FhW4JeTe6U-", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### 予測してみる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "xksAuNSoe08v", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"x = 5\n", | |
"y = a * x + b\n", | |
"print(f'気温 {x:.1f} 度ならゴリゴリ君の売上予想は {y:.1f} 個ほげ')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dkIjaWsme9CH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"y = 10\n", | |
"x = (y - b) / a\n", | |
"print(f'ゴリゴリ君の売上予想が {y:.1f} 個になるには気温が {x:.1f} 度になればいいほげ')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "LWfc6M54fQpj", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"**★★★ 次のことをやりましょう★★★**\n", | |
"\n", | |
"(1) 上記のセルを実行して,得られたパタメータの値をコピペするなりしてメモしておこう.\n", | |
"\n", | |
"(2) 上記のセルをいじって,気温 0度,10度,20度 および 30 度のときのゴリゴリ君の売上予想個数をそれぞれ求めなさい.実行結果はどこかにコピペするなりしてメモしておこう.\n", | |
"\n", | |
"(3) 上記のセルをいじって,ゴリゴリ君の売上予想が 100 個および 200 個ちょうどになる時の気温をそれぞれ求めなさい.実行結果はどこかにコピペするなりしてメモしておこう.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2y64PiSeQX_a", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment