検証データ(Validation Data):ハイパーパラメータ調整・モデル選択に使う、 訓練データから切り分けた評価用データ
このページは「検証データ(Validation Data)」の用語解説です。 機械学習の基礎カテゴリにおける重要概念で、 機械学習の基礎 グループ教材の中で繰り返し登場します。 数式・実コード・落とし穴を 1 ページに集約し、 SSDSE-B-2026 都道府県データ(47 件 × 112 列)を題材に 手を動かしながら理解できるよう構成しています。
別称:Validation Set / Dev Set。 まず 💡 30秒結論 で全体像を、 次に 🎨 直感 → 📐 数式 → 🧮 実値 → 🐍 Python の順で読むのがおすすめ。
検証データは「モデル選びの試験会場」です。 訓練データだけでハイパーパラメータを決めると過学習する。 テストデータは最終評価専用なので途中で見てはいけない。 そこで 第 3 のデータ を用意します。
用途は (1) 学習率や木の深さなど ハイパーパラメータ調整、 (2) 複数モデルから 1 つを選ぶ、 (3) ニューラルネットの early stopping です。 検証データへ「答えを合わせ続ける」と過学習するので、 多数試行する場合は k 分割交差検証 を併用。
本概念は次のように記述されます(KaTeX で描画)。
英語名 Validation Data。 別称:Validation Set / Dev Set。
記号と意味を逐一突き合わせて読みます。 慣れないうちは式を「日本語で読む」ことが理解の近道です。
SSDSE-B の 47 都道府県データを 訓練 60% / 検証 20% / テスト 20% に分割し、 RandomForest の max_depth を検証データで選びます。
データ出典:SSDSE-B-2026(独立行政法人統計センター)。 47 都道府県 × 複数年(最新 2023)の社会統計データ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score df = pd.read_csv('data/raw/SSDSE-B-2026.csv', encoding='cp932', skiprows=1) df = df[df['年度'] == 2023].reset_index(drop=True) df['若年比率'] = df['15歳未満人口'] / df['総人口'] df['ラベル'] = (df['若年比率'] >= df['若年比率'].median()).astype(int) X = df[['総人口','65歳以上人口']].values y = df['ラベル'].values # 1段目: 訓練80% / テスト20% X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0, stratify=y) # 2段目: 訓練の中で 訓練75% / 検証25% → 全体だと 60/20/20 X_train, X_val, y_train, y_val = train_test_split(X_tr, y_tr, test_size=0.25, random_state=0, stratify=y_tr) best_d, best_acc = None, -1 for d in [2, 3, 4, 5, None]: m = RandomForestClassifier(max_depth=d, random_state=0).fit(X_train, y_train) acc = accuracy_score(y_val, m.predict(X_val)) print(f'max_depth={d}: val_acc={acc:.3f}') if acc > best_acc: best_acc, best_d = acc, d # 最終評価 final = RandomForestClassifier(max_depth=best_d, random_state=0).fit(X_tr, y_tr) print(f'best_depth={best_d}, test_acc={accuracy_score(y_te, final.predict(X_te)):.3f}') |
実行結果の要約(出力は環境依存。 概算値):
| 項目 | 値 |
|---|---|
| 訓練 | 28 県 (60%) |
| 検証 | 9 県 (20%) |
| テスト | 10 県 (20%) |
| max_depth=2 val | 0.778 |
| max_depth=3 val | 0.889 (best) |
| テスト精度 | 0.800 |
scikit-learn / pandas を使った最小実装パターン。 上の SSDSE-B 計算と同じスタイルですが、 ここでは「読み込み→前処理→学習→評価」のテンプレを 4 つのスニペットに分けます。
1 2 3 4 | import pandas as pd df = pd.read_csv('data/raw/SSDSE-B-2026.csv', encoding='cp932', skiprows=1) df = df[df['年度'] == 2023].reset_index(drop=True) print(df.shape, df.columns.tolist()[:8]) |
1 2 3 | X = df[['総人口','65歳以上人口']].values y = df['15歳未満人口'].values print('X shape =', X.shape, ', y shape =', y.shape) |
1 2 3 4 5 6 | from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=0) model = RandomForestRegressor(n_estimators=300, random_state=0).fit(X_tr, y_tr) print('R^2 (test) =', model.score(X_te, y_te)) |
1 2 3 4 5 6 | import matplotlib.pyplot as plt pred = model.predict(X_te) plt.scatter(y_te, pred) plt.plot([y_te.min(), y_te.max()], [y_te.min(), y_te.max()], 'r--') plt.xlabel('実測'); plt.ylabel('予測'); plt.title('「検証データ」関連モデルの予測精度') plt.tight_layout(); plt.savefig('out.png', dpi=150) |
※ 「検証データ」固有の本格コードは上の 🧮 SSDSE-B 実値計算 節を参照。
| データ規模 | 推奨 train/val/test |
|---|---|
| 小(n < 1000) | 60/20/20 + CVも併用 |
| 中(1000〜10万) | 70/15/15 または 80/10/10 |
| 大(10万〜100万) | 90/5/5 |
| 超大(>100万) | 98/1/1(数千件のtest) |
「test に何件あれば信頼できるか」が判断軸。 1000件あれば標準誤差はかなり小さい。
深層学習でvalデータの典型用途。 「val loss が patience エポック改善しなければ訓練停止」:
1 2 | es = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) model.fit(X_train, y_train, validation_data=(X_val, y_val), callbacks=[es]) |
これで「学習しすぎ」を自動で防げる。 経験則:patience は 5〜20