def test_misses_cost_with_pgbs(self): # Sensitive closed itemsets whose support needs to be reduced sensitive_IS = {frozenset(['1', '2']), frozenset(['4'])} # PGBS needs input in this format sensitive_IL = pd.DataFrame( {'itemset': [list(l) for l in sensitive_IS], 'threshold': [self.sigma_min, self.sigma_min]}) original_database = self.basket_sets.copy() modified_database = self.basket_sets.copy() # No return value, instead it modifies input database in place pgbs(modified_database, sensitive_IL) # Get all itemsets and supports in D (original_database) sigma_model = 1 / len(original_database) original_IS = fpgrowth(original_database, min_support=sigma_model, use_colnames=True, verbose=False) # Get all itemsets and supports in D' (modified_database) mofidied_F_IS = fpgrowth(modified_database, min_support=sigma_model, use_colnames=True, verbose=False) # Find set of non-sensitive frequent itemsets in D a = remove_sensitive_subsets(original_IS, self.sensitive_IS) # Find set of non-sensitive frequent itemsets in D' b = remove_sensitive_subsets(mofidied_F_IS[mofidied_F_IS["support"] >= self.sigma_min], self.sensitive_IS) mc = misses_cost(a, b) self.assertEqual(mc, 0.18181818181818182)
def test_artifactual_patterns_with_pgbs(self): # PGBS needs input in this format sensitive_IL = pd.DataFrame({ 'itemset': [list(l) for l in self.sensitive_IS], 'threshold': [self.sigma_min, self.sigma_min] }) original_database = self.basket_sets.copy() modified_database = self.basket_sets.copy() # No return value, instead it modifies input database in place pgbs(modified_database, sensitive_IL) # Get all itemsets and supports in D (original_database) sigma_model = 1 / len(original_database) original_IS = fpgrowth(original_database, min_support=sigma_model, use_colnames=True, verbose=False) # Get all itemsets and supports in D' (modified_database) mofidied_F_IS = fpgrowth(modified_database, min_support=sigma_model, use_colnames=True, verbose=False) # All itemsets in original database a = set(original_IS["itemsets"]) # All itemsets in sanitised database b = set(mofidied_F_IS["itemsets"]) af = artifactual_patterns(a, b) self.assertEqual(af, 0.0)
def test_information_loss_with_pgbs(self): # Sensitive closed itemsets whose support needs to be reduced sensitive_itemsets = pd.DataFrame({ 'itemset': [['1', '2'], ['1', '2', '4'], ['4']], 'threshold': [self.sigma_min, self.sigma_min, self.sigma_min] }) original_database = self.basket_sets.copy() modified_database = self.basket_sets.copy() # No return value, instead it modifies input database in place print(sensitive_itemsets) print(sensitive_itemsets.dtypes) pgbs(modified_database, sensitive_itemsets) # Give all itemsets and supports in D (original_database) sigma_model = 1 / len(original_database) a = fpgrowth(original_database, min_support=sigma_model, use_colnames=True, verbose=False) # Give all itemsets and supports in D' (modified_database) b = fpgrowth(modified_database, min_support=sigma_model, use_colnames=True, verbose=False) il = information_loss(a, b) self.assertEqual(0.5542, round(il, 4))
def test_pgbs(self): basket_sets = im.import_dataset("chess").sample(100) # limit due to testing original_database = basket_sets.copy() modified_database = basket_sets.copy() # We partition the Chess databases into 5 bins, then randomly select 2 itemsets from each bin, # assign the minimum support threshold as the minimum support given in the support range # This takes a long time, so will just use their values. Table 3: Support ranges for databases. sigma_min = min([0.6001, 0.6136, 0.6308, 0.6555, 0.6974]) sigma_model = 0.5 original_IS = fpgrowth(original_database, min_support=sigma_model, use_colnames=True) # Get 10 sensitive itemsets sensitive_IS = original_IS.sample(10) sensitive_IS_PGBS = pd.DataFrame({ 'itemset': [list(IS) for IS in sensitive_IS["itemsets"]], 'threshold': [sigma_min for _ in sensitive_IS["support"]]}) pgbs(modified_database, sensitive_IS_PGBS) # Give all itemsets and supports in D (original_database) a = original_IS # Give all itemsets and supports in D' (modified_database) b = fpgrowth(modified_database, min_support=sigma_model, use_colnames=True) il = information_loss(a, b) self.assertEqual(0.5542, round(il, 4))
def test_hiding_failure_with_pgbs(self): # Sensitive closed itemsets whose support needs to be reduced sensitive_IS = {frozenset(['1', '2']), frozenset(['4'])} # PGBS needs input in this format sensitive_IL = pd.DataFrame({ 'itemset': [list(l) for l in sensitive_IS], 'threshold': [self.sigma_min, self.sigma_min] }) original_database = self.basket_sets.copy() modified_database = self.basket_sets.copy() # No return value, instead it modifies input database in place pgbs(modified_database, sensitive_IL) # Give all itemsets and supports in D (original_database) sigma_model = 1 / len(original_database) original_IS = fpgrowth(original_database, min_support=sigma_model, use_colnames=True, verbose=False) # Give all itemsets and supports in D' (modified_database) mofidied_F_IS = fpgrowth(modified_database, min_support=sigma_model, use_colnames=True, verbose=False) # Find set of frequent itemsets in D a = get_sensitive_subsets( original_IS.loc[original_IS["support"] > self.sigma_min], sensitive_IS)["itemsets"] # Find set of frequent itemsets in D' b = get_sensitive_subsets( mofidied_F_IS.loc[mofidied_F_IS["support"] > self.sigma_min], sensitive_IS)["itemsets"] hf = hiding_failure(a, b) self.assertEqual(0.0, hf)
def main(datasets): #Create the base of a table table_11 = pd.DataFrame(columns=['Model', 'Support threshold', 'Model threshold', 'Sensitive itemsets', 'Number of FI before sanitization', 'Information loss expected', 'Number of FI after sanitization', 'Number of FI containing an element of S after RPS', 'Hiding failure', 'Artifactual patterns', 'Misses cost', 'Side effects factor', 'Information loss', 'PGBS time']) #Loop through datasets for dataset in datasets: sigma_model = datasets[dataset][0] #Load dataset data = im.import_dataset(dataset) data = data.astype('bool') #This may be needed for some datasets print("\n", dataset, "imported\n") #Get frequent itemsets freq_model = fpgrowth(data, min_support=sigma_model, use_colnames=True) #Loop through support thresholds for sigma_min in datasets[dataset][1:]: print("\n", dataset, "FI:", sigma_min) #Find original frequent itemsets at frequency sigma min freq_original = freq_model.loc[freq_model["support"] >= sigma_min] for k_freq in [10, 30, 50]: print("-", dataset, ":", k_freq, "Sensitive itemsets") #Copy the transactions so we can edit it directly copied_data = data.copy() #We pick sensitive itemsets here sensitive_IS = get_top_k_sensitive_itemsets(freq_original, k_freq) #Start timer for PGBS portion total_time_start = time.time() #Convert to pandas format for PGBS input sensitive_IS_pandas = pd.DataFrame(data=[(sensitive_IS), np.full((len(sensitive_IS)), sigma_min)]).T sensitive_IS_pandas.columns = ['itemset', 'threshold'] #Run PGBS print("Running PGBS") pgbs(copied_data,sensitive_IS_pandas) print("PGBS run") pgbs_time = time.time() sensitive_IS = convert_to_sets(sensitive_IS) print("FPGrowth") #Reproduce frequent itemsets freq_model_sanitized = fpgrowth(copied_data, min_support=sigma_model, use_colnames=True) #Calculating metrics #Variables needed freq_sanitized = freq_model_sanitized.loc[freq_model_sanitized["support"] >= sigma_min] #Sensitive subsets of frequent itemsets freq_sanitized_sensitive = get_sensitive_subsets(freq_sanitized, sensitive_IS) freq_original_sensitive = get_sensitive_subsets(freq_original, sensitive_IS) #Non sensitive subset of frequent itemsets freq_sanitized_nonsensitive = remove_sensitive_subsets(freq_sanitized, sensitive_IS)["itemsets"] freq_original_nonsensitive = remove_sensitive_subsets(freq_original, sensitive_IS)["itemsets"] #Calculation of metrics freq_original_sensitive.to_csv("original.csv") freq_sanitized_sensitive.to_csv("sanitized.csv") print("len:", len(freq_original_sensitive["itemsets"]), len(freq_sanitized_sensitive["itemsets"])) hiding_f = hiding_failure(freq_original_sensitive["itemsets"], freq_sanitized_sensitive["itemsets"]) artifactual_p = artifactual_patterns(set(freq_original["itemsets"]), set(freq_sanitized["itemsets"])) misses_c = misses_cost(freq_original_nonsensitive.copy(), freq_sanitized_nonsensitive.copy()) side_effect_fac = side_effects_factor(set(freq_original["itemsets"]), set(freq_sanitized["itemsets"]), set(freq_original_sensitive["itemsets"])) #Information loss between frequent itemsets in original and sanitized at sigma model information_l = information_loss(freq_model.copy(), freq_model_sanitized) #Expected information loss if all sensitive frequent itemsets had their support reduced to sigma min expected_information_l = expected_information_loss(freq_model.copy(), freq_original_sensitive.copy(), sigma_min) #Calculate the end time of this iteration end_time = pgbs_time - total_time_start #Threshold sanitized database by threshold_min to get frequent itemsets print(f'- PGBS time: {end_time}') #Plot support graphs dual_support_graph_distribution(freq_model, freq_model_sanitized, sigma_model, dataset+"_PGBS_"+str(sigma_min)+"_"+str(k_freq)) #Find number of FI in sanitized database containing sensitive itemsets num_FI_containing_S_RPS = count_FI_containing_S(freq_sanitized, sensitive_IS) #Add to row of table new_row = {'Model': dataset, 'Model threshold': sigma_model, 'Support threshold': sigma_min, 'Sensitive itemsets': k_freq, 'Number of FI before sanitization': len(freq_original), 'Information loss expected': expected_information_l, 'Number of FI after sanitization': len(freq_sanitized), 'Number of FI containing an element of S after RPS': num_FI_containing_S_RPS, 'Hiding failure': hiding_f, 'Artifactual patterns': artifactual_p, 'Misses cost': misses_c, 'Side effects factor': side_effect_fac, 'Information loss': information_l, 'PGBS time': end_time} #Update after each one just so we are sure we are recording results table_11 = table_11.append(new_row, ignore_index=True) table_11.to_csv('table_pgbs.csv')
def main(datasets, experiment): for dataset in datasets: sigma_model = datasets[dataset][0] sigma_min = datasets[dataset][1] k_freq = 10 #Load dataset data = im.import_dataset(dataset) data = data.astype('bool') #This may be needed for some datasets print("\n", dataset, "imported\n") #Convert to closed itemsets current_model, freq_model = get_closed_itemsets(data, sigma_model) freq_original = freq_model.loc[freq_model["support"] >= sigma_min] sensitive_IS = get_top_k_sensitive_itemsets(freq_original, k_freq) if experiment == "MuRPS-range": #Convert to pandas format for MRPS input sensitive_IS_pandas = pd.DataFrame( data=[(sensitive_IS), np.array([ 0.8, 0.79, 0.78, 0.77, 0.76, 0.75, 0.74, 0.73, 0.72, 0.71 ]), np.array([ 0.795, 0.785, 0.775, 0.765, 0.755, 0.745, 0.735, 0.725, 0.715, 0.705 ])]).T elif experiment == "MuRPS-set": #Convert to pandas format for MRPS input thresholds = [ 0.7975, 0.7875, 0.7775, 0.7675, 0.7575, 0.7475, 0.7375, 0.7275, 0.7175, 0.7075 ] sensitive_IS_pandas = pd.DataFrame(data=[(sensitive_IS), np.array(thresholds), np.array(thresholds)]).T elif experiment == "SWA-set": db = im.convert_to_transaction(data) thresholds = [ 0.7975, 0.7875, 0.7775, 0.7675, 0.7575, 0.7475, 0.7375, 0.7275, 0.7175, 0.7075 ] #Convert to pandas format for SWA input sensitive_rules = get_disclosures(sensitive_IS, freq_model, thresholds) print(sensitive_rules) #Run SWA SWA(db, sensitive_rules, db.shape[0]) #Convert to frequent itemsets sensitive_IS = convert_to_sets(sensitive_IS) data = im.convert_to_matrix(db) freq_model_sanitized = fpgrowth(data, min_support=sigma_model, use_colnames=True) freq_sanitized = freq_model_sanitized.loc[ freq_model_sanitized["support"] >= sigma_min] elif experiment == "PGBS-set": thresholds = [ 0.7975, 0.7875, 0.7775, 0.7675, 0.7575, 0.7475, 0.7375, 0.7275, 0.7175, 0.7075 ] sensitive_IS_pandas = pd.DataFrame( data=[(sensitive_IS), np.full((len(sensitive_IS)), thresholds)]).T sensitive_IS_pandas.columns = ['itemset', 'threshold'] #Run PGBS pgbs(data, sensitive_IS_pandas) #Convert to frequent itemsets sensitive_IS = convert_to_sets(sensitive_IS) freq_model_sanitized = fpgrowth(data, min_support=sigma_model, use_colnames=True) freq_sanitized = freq_model_sanitized.loc[ freq_model_sanitized["support"] >= sigma_min] if experiment[0] == "M": sensitive_IS_pandas.columns = [ 'itemset', 'upper_threshold', 'lower_threshold' ] print(sensitive_IS_pandas) #Run RPS random threshold sanitized_closed_IS = rps_two_thresholds( model=current_model, sensitiveItemsets=sensitive_IS_pandas) #Reproduce frequent itemsets freq_model_sanitized = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=freq_model['itemsets']) #Plot support graphs dual_support_graph_distribution( freq_model, freq_model_sanitized, sigma_model, dataset + "_presentation_" + experiment + "_" + str(k_freq)) #Calculate and print information loss information_l = information_loss(freq_model.copy(), freq_model_sanitized) print("Information loss:", information_l)