第15回:機械学習入門(演習)#
学籍番号:…
氏名:…
必要なライブラリのimportとデータのダウンロード#
以下のセルで演習に必要なライブラリのimportとデータをダウンロードしています.初めに実行してください.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Hiragino Sans', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']
!mkdir -p data
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_N.npy -O data/data_N.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_N.npy -O data/target_N.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_noise_scale.npy -O data/data_noise_scale.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_noise_scale.npy -O data/target_noise_scale.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_a.npy -O data/data_a.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_a.npy -O data/target_a.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_b.npy -O data/data_b.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_b.npy -O data/target_b.npy
--2026-01-08 16:50:12-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_N.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 208 [application/octet-stream]
`data/data_N.npy' に保存中
data_N.npy 0%[ ] 0 --.-KB/s
data_N.npy 100%[===================>] 208 --.-KB/s 時間 0s
2026-01-08 16:50:12 (6.01 MB/s) - `data/data_N.npy' へ保存完了 [208/208]
--2026-01-08 16:50:12-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_N.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています...
接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 208 [application/octet-stream]
`data/target_N.npy' に保存中
target_N.npy 0%[ ] 0 --.-KB/s
target_N.npy 100%[===================>] 208 --.-KB/s 時間 0s
2026-01-08 16:50:13 (6.20 MB/s) - `data/target_N.npy' へ保存完了 [208/208]
--2026-01-08 16:50:13-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_noise_scale.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています...
接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 368 [application/octet-stream]
`data/data_noise_scale.npy' に保存中
data_noise_scale.np 0%[ ] 0 --.-KB/s
data_noise_scale.np 100%[===================>] 368 --.-KB/s 時間 0s
2026-01-08 16:50:13 (7.98 MB/s) - `data/data_noise_scale.npy' へ保存完了 [368/368]
--2026-01-08 16:50:13-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_noise_scale.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています...
接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 368 [application/octet-stream]
`data/target_noise_scale.npy' に保存中
target_noise_scale. 0%[ ] 0 --.-KB/s
target_noise_scale. 100%[===================>] 368 --.-KB/s 時間 0s
2026-01-08 16:50:14 (10.6 MB/s) - `data/target_noise_scale.npy' へ保存完了 [368/368]
--2026-01-08 16:50:14-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_a.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています...
接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 368 [application/octet-stream]
`data/data_a.npy' に保存中
data_a.npy 0%[ ] 0 --.-KB/s
data_a.npy 100%[===================>] 368 --.-KB/s 時間 0s
2026-01-08 16:50:14 (8.77 MB/s) - `data/data_a.npy' へ保存完了 [368/368]
--2026-01-08 16:50:14-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_a.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています...
接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 368 [application/octet-stream]
`data/target_a.npy' に保存中
target_a.npy 0%[ ] 0 --.-KB/s
target_a.npy 100%[===================>] 368 --.-KB/s 時間 0s
2026-01-08 16:50:15 (10.6 MB/s) - `data/target_a.npy' へ保存完了 [368/368]
--2026-01-08 16:50:15-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/data_b.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています...
接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 368 [application/octet-stream]
`data/data_b.npy' に保存中
data_b.npy 0%[ ] 0 --.-KB/s
data_b.npy 100%[===================>] 368 --.-KB/s 時間 0s
2026-01-08 16:50:15 (11.0 MB/s) - `data/data_b.npy' へ保存完了 [368/368]
--2026-01-08 16:50:15-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro-data/refs/heads/main/data/machine_learning/exercise/target_b.npy
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.110.133,
185.199.108.133, 185.199.109.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています...
200 OK
長さ: 368 [application/octet-stream]
`data/target_b.npy' に保存中
target_b.npy 0%[ ] 0 --.-KB/s
target_b.npy 100%[===================>] 368 --.-KB/s 時間 0s
2026-01-08 16:50:16 (21.9 MB/s) - `data/target_b.npy' へ保存完了 [368/368]
演習1: 線形回帰#
演習1-1: データの読み込みと可視化#
以下に示すようにデータ数 N,ノイズの大きさ noise_scale,データを生成するときの関数の傾きと切片 aとbのパラメータを講義資料から変更したデータを用意した.
データ数
Nを変更したデータのパスX: ‘data/data_N.npy’
y: ‘data/target_N.npy’
ノイズの大きさ
noise_scaleを変更したデータのパスX: ‘data/data_noise_scale.npy’
y: ‘data/target_noise_scale.npy’
データを生成するときの関数の傾き
aを変更したデータのパスX: ‘data/data_a.npy’
y: ‘data/target_a.npy’
データを生成するときの関数の切片
bを変更したデータのパスX: ‘data/data_b.npy’
y: ‘data/target_b.npy’
どれか一つで良いので np.load関数を使って読み込み,読み込んだデータを可視化せよ.
# 解答用セル
演習1-2: 線形回帰#
読み込んだデータを使って線形回帰を実行せよ.また得られた傾きと切片を元に結果を可視化せよ.
# 解答用セル
発展演習1: Numpyの操作#
発展演習1-1: ベクトルの定義#
Numpyを用いて以下に示すベクトル \(\mathbf{a},\mathbf{b}\) を定義して.shapeでベクトルの形状を以下の目標出力に従って出力せよ.
目標出力
a.shape: (4, 1), b.shape: (4, 1)
# 解答用セル
発展演習1-2: ベクトルの内積#
定義したベクトル \(\mathbf{a},\mathbf{b}\) の内積 \(\mathbf{a}^\top \mathbf{b}\) をNumpyを用いて計算せよ.
# 解答用セル
発展演習1-3: Numpyを用いない実装#
演習1-1,1-2をNumpyを用いずリストと繰り返し文のみを使って実装せよ.
# 解答用セル
発展演習1-4: 行列の定義#
Numpyを用いて以下に示す行列 \(\mathbf{A},\mathbf{B}\) を定義し,.shapeで行列の形状を以下の目標出力に従って出力せよ.
目標出力
A.shape: (2, 3), B.shape: (3, 2)
# 解答用セル
発展演習1-5: 行列の積#
定義した行列 \(\mathbf{A},\mathbf{B}\) の積 \(\mathbf{A}\mathbf{B}\) をNumpyを用いて計算せよ.
# 解答用セル
発展演習1-6: 固有値計算#
以下の行列 \(\mathbf{C}\) の固有値と固有ベクトルを計算せよ.ただし,Numpy, Sympy, Scipy,…など利用するライブラリや計算方法は自由で良い.講義中で固有値計算の話題に触れていないので各自利用するライブラリのリファレンス等を参考に取り組むこと.
# 解答用セル
発展演習2: 線形回帰#
講義で扱った線形回帰は \(y=ax+b\) という線形のモデルを仮定してデータを当てはめる.利用する入出力のペア \((x,y)\) が \(y=ax+b\) ではなく二次関数 \(y=ax^2+bx+c\) から生成されるときどのような結果となるかPythonのプログラミングを通して検証せよ.ただし,パラメータN, noise_scale, a, b, cは自由に設定して良い.
# 解答用セル
発展演習3: 多項式回帰#
発展演習2で作成した二次関数から生成されるデータに対して多項式回帰というアルゴリズムを考える.二次の多項式回帰は,線形回帰
から,変数 \(x\) について
という二次関数を使って回帰を行う.
scikit-learnにおける多項式回帰の実装方法について調べ,発展演習2で作成したデータに対して多項式回帰を実装せよ.
ヒント:多項式回帰は PolynomialFeatures を使うと実現できる.
# 解答用セル