Hikaru's notes

A collection of notes and resources

View the Project on GitHub HikaruMizoe/notes

1.3 データの完全性を確保し、モデリングに向けてデータを準備する

タスクステートメント

データの完全性を確保し、モデリングに向けてデータを準備する

対象知識

トレーニング前のバイアスメトリクス

数値、テキスト、画像データのバイアス指標

クラス不均衡(CI)に対処するための戦略

合成データの生成

リサンプリング

データを暗号化する手法

データの分類、匿名化、マスキング

コンプライアンス要件の影響

対象スキル

1. データ品質を検証する

2. データのバイアスの原因を特定して対策を講じる

SageMaker Clarify を使用:

3. 予測バイアスを軽減するためにデータを準備する

4. モデルトレーニングリソースにロードするようにデータを設定する

実践例

SageMaker Clarifyでバイアス検出

from sagemaker import clarify

bias_config = clarify.BiasConfig(
    label_values_or_threshold=[1],
    facet_name='gender',
    facet_values_or_threshold=[0]
)

clarify_processor = clarify.SageMakerClarifyProcessor(
    role=role,
    instance_count=1,
    instance_type='ml.m5.xlarge',
    sagemaker_session=session
)

clarify_processor.run_pre_training_bias(
    data_config=data_config,
    data_bias_config=bias_config,
    methods='all'
)

データセットの分割

from sklearn.model_selection import train_test_split

# 層化分割
X_train, X_test, y_train, y_test = train_test_split(
    X, y, 
    test_size=0.2, 
    stratify=y,
    random_state=42
)

# さらに検証セットを分割
X_train, X_val, y_train, y_val = train_test_split(
    X_train, y_train,
    test_size=0.25,  # 0.25 x 0.8 = 0.2 of total
    stratify=y_train,
    random_state=42
)

Glue Data Qualityの使用

import boto3

glue_client = boto3.client('glue')

# データ品質ルールセットの作成
response = glue_client.create_data_quality_ruleset(
    Name='customer-data-quality',
    Ruleset='''
        Rules = [
            RowCount > 1000,
            IsComplete "customer_id",
            IsUnique "customer_id",
            ColumnValues "age" between 0 and 120,
            ColumnValues "email" matches "^[\\w\\.]+@[\\w\\.]+$"
        ]
    ''',
    TargetTable={
        'DatabaseName': 'customer_db',
        'TableName': 'customers'
    }
)

EFSをSageMakerトレーニングで使用

from sagemaker.tensorflow import TensorFlow

estimator = TensorFlow(
    entry_point='train.py',
    role=role,
    instance_type='ml.p3.2xlarge',
    instance_count=2,
    framework_version='2.12',
    subnets=['subnet-xxx'],
    security_group_ids=['sg-xxx'],
    file_system_id='fs-xxx',  # EFS ID
    file_system_type='EFS',
    file_system_directory_path='/ml-data',
    file_system_access_mode='ro'
)

重要なポイント

バイアス軽減のベストプラクティス

  1. データ収集段階
    • 多様なデータソースから収集
    • 代表的なサンプリング
  2. データ準備段階
    • バイアス指標の測定
    • クラス不均衡の対処
  3. 検証
    • SageMaker Clarify でバイアス検出
    • 継続的なモニタリング

データ品質チェックリスト

関連リンク