<jupyter_start><jupyter_text>German Credit Rating  Re-Indentification risk analysis and De-Indentification<jupyter_code>import pandas as pd
from pyaaas import ARXaaS, Dataset, AttributeType
df = pd.read_csv("data/german_credit_data.csv")
df.dropna(inplace=True)
df.head()<jupyter_output><empty_output><jupyter_text>Create Dataset and set attribute types<jupyter_code>dataset = Dataset.from_pandas(df)
dataset.describe()
dataset.set_attribute_type(AttributeType.INSENSITIVE, 'Unnamed: 0')
dataset.set_attribute_type(AttributeType.IDENTIFYING, 'Housing', 'Checking account', 'Duration', 'Purpose', 'Risk')<jupyter_output><empty_output><jupyter_text>Create RiskProfile<jupyter_code>arxaas = ARXaaS("http://localhost:8080")
risk_profile = arxaas.risk_profile(dataset)
risk_profile.re_identification_risk<jupyter_output><empty_output><jupyter_text>Estimated prosecutor risk is 1.0. The dataset re-Identification risk is high.  Anonymize  Create hierarchiesBefore we can attempt to anonymize the dataset we need to create generalization hierarchies for the quasiidentifying attributes.<jupyter_code>from pyaaas.hierarchy import OrderHierarchyBuilder<jupyter_output><empty_output><jupyter_text>Sex<jupyter_code>ob = OrderHierarchyBuilder()
sexes = df["Sex"].unique().tolist()
sex_hierarchy = arxaas.hierarchy(ob, sexes)
sex_hierarchy<jupyter_output><empty_output><jupyter_text>Age<jupyter_code>from pyaaas.hierarchy import IntervalHierarchyBuilder
ages = df["Age"].tolist()
iterval_builder = IntervalHierarchyBuilder()
iterval_builder.add_interval(0, 18, "child")
iterval_builder.add_interval(18, 30, "young-adult")
iterval_builder.add_interval(30, 60, "adult")
iterval_builder.add_interval(60, 120, "old")
iterval_builder.level(0).add_group(2, "young")
iterval_builder.level(0).add_group(2, "old")
age_hierarchy = arxaas.hierarchy(iterval_builder, ages)
age_hierarchy[:10]<jupyter_output><empty_output><jupyter_text>Credit amount<jupyter_code>credit_amounts = df["Credit amount"].tolist()
df["Credit amount"].describe()
credit_interval = IntervalHierarchyBuilder()
credit_interval.add_interval(0, 1300, "low")
credit_interval.add_interval(1300, 2400, "medium")
credit_interval.add_interval(2400, 20000, "high")

credit_interval.level(0)\
    .add_group(2, "low-medium")\
    .add_group(2, "medium-high")

credit_hierarchy = arxaas.hierarchy(credit_interval, credit_amounts)
credit_hierarchy[:10]<jupyter_output><empty_output><jupyter_text>Job<jupyter_code>jobs = df["Job"].tolist()
df["Job"].describe()
job_interval = IntervalHierarchyBuilder()
job_interval.add_interval(0,1,"un-employed")
job_interval.add_interval(1,4,"employed")
job_hierarchy = arxaas.hierarchy(job_interval, jobs)
job_hierarchy[:10]<jupyter_output><empty_output><jupyter_text>Set hierarchies<jupyter_code>dataset.set_hierarchy("Sex", sex_hierarchy)
dataset.set_hierarchy("Age", age_hierarchy)
dataset.set_hierarchy("Credit amount", credit_hierarchy)
dataset.set_hierarchy("Job", job_hierarchy)
from pyaaas.privacy_models import KAnonymity
kanon = KAnonymity(2)<jupyter_output><empty_output><jupyter_text>Attempt anonymization<jupyter_code>anon_result = arxaas.anonymize(dataset, [kanon])
anon_result.dataset.to_dataframe().head()<jupyter_output><empty_output><jupyter_text>Hierarchy level applied<jupyter_code>anon_metrics = anon_result.anonymization_metrics
anon_metrics.attribute_generalization<jupyter_output><empty_output><jupyter_text>New re-identification risk<jupyter_code>anon_risk_profile = anon_result.risk_profile
anon_risk_profile.re_identification_risk
before_risk = risk_profile.re_identification_risk_dataframe()[['estimated_prosecutor_risk', 'highest_prosecutor_risk', 'lowest_risk']].copy()
before_risk["from"] = "before"
after_risk = anon_risk_profile.re_identification_risk_dataframe()[['estimated_prosecutor_risk', 'highest_prosecutor_risk', 'lowest_risk']].copy()
after_risk["from"] = "after"
risks_df = pd.concat([before_risk, after_risk])
risks_df.plot(kind="bar", x="from", title="Re-Identification risks before and after anonymization", figsize=(10, 10));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12098159509202454, 'fraction_numerical': 0.016441717791411042, 'mean_word_length': 12.022364217252397, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '176314', 'license': 'MIT', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1441, 'n_words': 197, 'path': 'notebooks/german-credit-data/German-credit-rating-anonymization.ipynb', 'repo_name': 'oslomet-arx-as-a-service/example-pyaaas-project', 'size': '4075'}