第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']
Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
!mkdir data
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_N.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_N.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_noise_scale.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_noise_scale.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_a.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_a.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_b.npy
!wget -P data https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_b.npy
mkdir: data: File exists
--2025-01-08 12:47:11-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_N.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.
HTTP request sent, awaiting response...
200 OK
Length: 208 [application/octet-stream]
Saving to: 'data/data_N.npy.1'
data_N.npy.1 0%[ ] 0 --.-KB/s
data_N.npy.1 100%[===================>] 208 --.-KB/s in 0s
2025-01-08 12:47:11 (5.51 MB/s) - 'data/data_N.npy.1' saved [208/208]
--2025-01-08 12:47:12-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_N.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.
HTTP request sent, awaiting response...
200 OK
Length: 208 [application/octet-stream]
Saving to: 'data/target_N.npy.1'
target_N.npy.1 0%[ ] 0 --.-KB/s
target_N.npy.1 100%[===================>] 208 --.-KB/s in 0s
2025-01-08 12:47:12 (5.67 MB/s) - 'data/target_N.npy.1' saved [208/208]
--2025-01-08 12:47:12-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_noise_scale.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443...
connected.
HTTP request sent, awaiting response...
200 OK
Length: 368 [application/octet-stream]
Saving to: 'data/data_noise_scale.npy.1'
data_noise_scale.np 0%[ ] 0 --.-KB/s
data_noise_scale.np 100%[===================>] 368 --.-KB/s in 0s
2025-01-08 12:47:13 (4.81 MB/s) - 'data/data_noise_scale.npy.1' saved [368/368]
--2025-01-08 12:47:13-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_noise_scale.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443...
connected.
HTTP request sent, awaiting response...
200 OK
Length: 368 [application/octet-stream]
Saving to: 'data/target_noise_scale.npy.1'
target_noise_scale. 0%[ ] 0 --.-KB/s
target_noise_scale. 100%[===================>] 368 --.-KB/s in 0s
2025-01-08 12:47:13 (9.24 MB/s) - 'data/target_noise_scale.npy.1' saved [368/368]
--2025-01-08 12:47:13-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_a.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443...
connected.
HTTP request sent, awaiting response...
200 OK
Length: 368 [application/octet-stream]
Saving to: 'data/data_a.npy.1'
data_a.npy.1 0%[ ] 0 --.-KB/s
data_a.npy.1 100%[===================>] 368 --.-KB/s in 0s
2025-01-08 12:47:14 (9.00 MB/s) - 'data/data_a.npy.1' saved [368/368]
--2025-01-08 12:47:14-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_a.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.
HTTP request sent, awaiting response...
200 OK
Length: 368 [application/octet-stream]
Saving to: 'data/target_a.npy.1'
target_a.npy.1 0%[ ] 0 --.-KB/s
target_a.npy.1 100%[===================>] 368 --.-KB/s in 0s
2025-01-08 12:47:14 (3.08 MB/s) - 'data/target_a.npy.1' saved [368/368]
--2025-01-08 12:47:14-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/data_b.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.
HTTP request sent, awaiting response...
200 OK
Length: 368 [application/octet-stream]
Saving to: 'data/data_b.npy.1'
data_b.npy.1 0%[ ] 0 --.-KB/s
data_b.npy.1 100%[===================>] 368 --.-KB/s in 0s
2025-01-08 12:47:15 (13.0 MB/s) - 'data/data_b.npy.1' saved [368/368]
--2025-01-08 12:47:15-- https://raw.githubusercontent.com/ground-zero-programming/zero-pro2024-public/main/docs/15/data/target_b.npy
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.
HTTP request sent, awaiting response...
200 OK
Length: 368 [application/octet-stream]
Saving to: 'data/target_b.npy.1'
target_b.npy.1 0%[ ] 0 --.-KB/s
target_b.npy.1 100%[===================>] 368 --.-KB/s in 0s
2025-01-08 12:47:15 (4.39 MB/s) - 'data/target_b.npy.1' saved [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
を使うと実現できる.
# 解答用セル