<jupyter_start><jupyter_text>Run Ad-Hoc Data Bias Analysis Run Bias Analysis In The Notebook using `smclarify`https://github.com/aws/amazon-sagemaker-clarify<jupyter_code>!pip install -q smclarify==0.1
from smclarify.bias import report
from typing import Dict
from collections import defaultdict
import pandas as pd
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Read Dataset From S3<jupyter_code>%store -r bias_data_s3_uri
print(bias_data_s3_uri)
%store -r balanced_bias_data_s3_uri
print(balanced_bias_data_s3_uri)
!aws s3 cp $bias_data_s3_uri ./data-clarify/
!aws s3 cp $balanced_bias_data_s3_uri ./data-clarify/<jupyter_output><empty_output><jupyter_text>Analyze Unbalanced Data<jupyter_code>df = pd.read_csv("./data-clarify/amazon_reviews_us_giftcards_software_videogames.csv")
df.shape
sns.countplot(data=df, x="star_rating", hue="product_category")<jupyter_output><empty_output><jupyter_text>Calculate Bias Metrics on Unbalanced Data  Define * Facet Column (= Product Category), * Label Column (= Star Rating), * Positive Label Value (= 5,4)<jupyter_code>facet_column = report.FacetColumn(name="product_category")
label_column = report.LabelColumn(name="star_rating", data=df["star_rating"], positive_label_values=[5, 4])
group_variable = df["product_category"]<jupyter_output><empty_output><jupyter_text>Run SageMaker Clarify Bias Report<jupyter_code>report.bias_report(
    df, facet_column, label_column, stage_type=report.StageType.PRE_TRAINING, group_variable=group_variable
)<jupyter_output><empty_output><jupyter_text>Balance the data<jupyter_code>df_grouped_by = df.groupby(["product_category", "star_rating"])[["product_category", "star_rating"]]
df_balanced = df_grouped_by.apply(lambda x: x.sample(df_grouped_by.size().min()).reset_index(drop=True))
df_balanced.shape
import seaborn as sns

sns.countplot(data=df_balanced, x="star_rating", hue="product_category")<jupyter_output><empty_output><jupyter_text>Calculate Bias Metrics on Balanced Data  Define * Facet Column (= Product Category), * Label Column (= Star Rating), * Positive Label Value (= 5,4)<jupyter_code>from smclarify.bias import report

facet_column = report.FacetColumn(name="product_category")
label_column = report.LabelColumn(name="star_rating", data=df_balanced["star_rating"], positive_label_values=[5, 4])

group_variable = df_balanced["product_category"]<jupyter_output><empty_output><jupyter_text>Run SageMaker Clarify Bias Report<jupyter_code>report.bias_report(
    df_balanced, facet_column, label_column, stage_type=report.StageType.PRE_TRAINING, group_variable=group_variable
)
%%html

<p><b>Shutting down your kernel for this notebook to release resources.</b></p>
<button class="sm-command-button" data-commandlinker-command="kernelmenu:shutdown" style="display:none;">Shutdown Kernel</button>
        
<script>
try {
    els = document.getElementsByClassName("sm-command-button");
    els[0].click();
}
catch(err) {
    // NoOp
}    
</script>
%%javascript

try {
    Jupyter.notebook.save_checkpoint();
    Jupyter.notebook.session.delete();
}
catch(err) {
    // NoOp
}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11987179487179488, 'fraction_numerical': 0.00641025641025641, 'mean_word_length': 8.179411764705883, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '141558', 'license': 'Apache-2.0', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1084, 'n_words': 186, 'path': '05_explore/03_Run_Data_Bias_Analysis_AdHoc.ipynb', 'repo_name': 'NRauschmayr/workshop', 'size': '3120'}