<jupyter_start><jupyter_text>Detecting pretraining bias with SageMaker Clarify This notebook contains the code to help readers work through one of the recipes of the book [Machine Learning with Amazon SageMaker Cookbook: 80 proven recipes for data scientists and developers to perform ML experiments and deployments](https://www.amazon.com/Machine-Learning-Amazon-SageMaker-Cookbook/dp/1800567030)  How to do it...<jupyter_code>%store -r s3_bucket_name
%store -r prefix
%store -r training_data_path
import sagemaker

session = sagemaker.Session()
region = session.boto_region_name
role = sagemaker.get_execution_role()
import pandas as pd
import numpy as np
!mkdir -p tmp
s3_training_data_path = training_data_path
s3_output_path = f"s3://{s3_bucket_name}/{prefix}/output"
!aws s3 cp {s3_training_data_path} tmp/training_data.csv
training_data = pd.read_csv("tmp/training_data.csv")
training_data
from sagemaker import clarify

processor = clarify.SageMakerClarifyProcessor(
    role=role,
    instance_count=1,
    instance_type='ml.m5.large',
    sagemaker_session=session
)
data_config = clarify.DataConfig(
    s3_data_input_path=s3_training_data_path,
    s3_output_path=s3_output_path,
    label='approved',
    headers=training_data.columns.to_list(),
    dataset_type='text/csv'
)
bias_config = clarify.BiasConfig(
    label_values_or_threshold=[1],
    facet_name='sex',
)
%%time

processor.run_pre_training_bias(
    data_config=data_config, 
    data_bias_config=bias_config,
    methods=['CI']
)
output = processor.latest_job.outputs[0]
output_destination = output.destination
output_destination
!aws s3 cp {output_destination}/ tmp/ --recursive
!ls -lahF tmp/
!cat tmp/analysis.json<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0910144927536232, 'fraction_numerical': 0.016231884057971015, 'mean_word_length': 5.7952755905511815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '256518', 'license': 'MIT', 'n_tokens_mistral': 594, 'n_tokens_neox': 567, 'n_words': 128, 'path': 'Chapter07/03 - Detecting pretraining bias with SageMaker Clarify.ipynb', 'repo_name': 'MARKOM/sagemaker-cookbook', 'size': '1725'}