<jupyter_start><jupyter_text>ARX Example<jupyter_code>import pandas as pd
from pyaaas import ARXaaS,Dataset,AttributeType<jupyter_output><empty_output><jupyter_text>Importing the data to be anonymized<jupyter_code>df = pd.read_csv("../../data/ARX-example/example.csv", sep=";")
df<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.IDENTIFYING,'race','ldl')
dataset.set_attribute_type(AttributeType.SENSITIVE,'workclass','salary-class')<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
risk_profile.attacker_success_rate
risk_profile.quasi_identifiers
risk_profile.population_model
risk_profile.distribution_of_risk<jupyter_output><empty_output><jupyter_text>Anonymize  Import hierarchy files<jupyter_code>sex_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_sex.csv", sep=';', header=None)
age_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_age.csv", sep=';', header=None)
#race_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_race.csv, sep=';', header=None)
marital_status_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_martial_status.csv", sep=';', header=None)
education_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_education.csv", sep=';', header=None)
native_country_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_native_country.csv", sep=';', header=None)
#workclass_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_workclass.csv", sep=';', header=None)
occupation_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_occupation.csv", sep=';', header=None)
#salary_class_hierarchy = pd.read_csv("../../hierarchies/ARX-example/example_hierarchy_salary-class.csv", sep=';', header=None)<jupyter_output><empty_output><jupyter_text>Setting the hierarchies<jupyter_code>dataset.set_hierarchy('sex',sex_hierarchy)
dataset.set_hierarchy('age',age_hierarchy)
#dataset.set_hierarchy('race',race_hierarchy)
dataset.set_hierarchy('marital-status',marital_status_hierarchy)
dataset.set_hierarchy('education',education_hierarchy)
dataset.set_hierarchy('native-country',native_country_hierarchy)
#dataset.set_hierarchy('workclass',workclass_hierarchy)
dataset.set_hierarchy('occupation',occupation_hierarchy)
#dataset.set_hierarchy('salary-class',salary_class_hierarchy)<jupyter_output><empty_output><jupyter_text>Setting the privacy model<jupyter_code>from pyaaas.privacy_models import KAnonymity,LDiversityDistinct,TClosenessEqualDistance
kanon = KAnonymity(2)
lanon = LDiversityDistinct(2,'workclass')
tanon = TClosenessEqualDistance(0.0001,'salary-class')
anon_result = arxaas.anonymize(dataset,[kanon,lanon,tanon],0.002)
anon_result.dataset.to_dataframe()<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>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.13668880940214614, 'fraction_numerical': 0.0048543689320388345, 'mean_word_length': 11.548076923076923, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '248862', 'license': 'MIT', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1394, 'n_words': 130, 'path': 'notebooks/ARX-example/ARX-example.ipynb', 'repo_name': 'oslomet-arx-as-a-service/example-pyaaas-project', 'size': '3914'}