def test_hiding_failure_with_rps(self): # Sensitive closed itemsets whose support needs to be reduced sensitive_IS = {frozenset(['1', '2']), frozenset(['4'])} # Produce a sanitised DB with sensitive IS's support below sigma_min sanitized_closed_IS = rps(model=self.original_Closed_IS, sensitiveItemsets=sensitive_IS, supportThreshold=self.sigma_min) # Convert from closed to frequent itemsets sanitised_F_IS = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=self.original_IS['itemsets']) # Old working to look back on # # Find sensitive itemsets in original database # a = set(self.original_IS["itemsets"]).intersection(set(sensitive_IS)) # # # Find sensitive itemsets in sanitised database # b = set(sanitised_F_IS["itemsets"]).intersection(set(sensitive_IS)) # Find set of frequent itemsets in D a = get_sensitive_subsets( self.original_IS.loc[self.original_IS["support"] > self.sigma_min], sensitive_IS)["itemsets"] # Find set of frequent itemsets in D' b = get_sensitive_subsets( sanitised_F_IS.loc[sanitised_F_IS["support"] > self.sigma_min], sensitive_IS)["itemsets"] hf = hiding_failure(a, b) self.assertEqual(0.0, hf)
def test_side_effects_factor_with_rps(self): # Sensitive closed itemsets whose support needs to be reduced sensitive_IS = {frozenset(['1', '2']), frozenset(['4'])} # Produce a sanitised DB with sensitive IS's support below sigma_min sanitized_closed_IS = rps(model=self.original_Closed_IS, sensitiveItemsets=sensitive_IS, supportThreshold=self.sigma_min) # Convert from closed to frequent itemsets sanitised_F_IS = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=self.original_IS['itemsets']) # Set of all itemsets in original database D a = set(self.original_Freq_IS["itemsets"]) # Set of all itemsets in sanitised database D' b = set(sanitised_F_IS[sanitised_F_IS["support"] >= self.sigma_min] ["itemsets"]) # Set of all sensitive itemsets in original database D c = set( get_sensitive_subsets(self.original_Freq_IS, sensitive_IS)["itemsets"]) sef = side_effects_factor(a, b, c) print(len(a), len(b), len(c)) self.assertEqual(0.0, sef)
def test_misses_cost_with_rps(self): # Produce a sanitised DB with sensitive IS's support below sigma_min sanitized_closed_IS = rps(model=self.original_Closed_IS, sensitiveItemsets=self.sensitive_IS, supportThreshold=self.sigma_min) # Convert from closed to frequent itemsets sanitised_F_IS = itemsets_from_closed_itemsets(closed_itemsets=sanitized_closed_IS, possible_itemsets=self.original_IS['itemsets']) # Find set of non-sensitive frequent itemsets in D a = remove_sensitive_subsets(self.original_Freq_IS, self.sensitive_IS) # Find set of non-sensitive frequent itemsets in D' b = remove_sensitive_subsets(sanitised_F_IS[sanitised_F_IS["support"] >= self.sigma_min], self.sensitive_IS) mc = misses_cost(a, b) self.assertEqual(mc, 0.0)
def main(datasets): for dataset in datasets: sigma_model = datasets[dataset][0] sigma_min = datasets[dataset][1] k_freq = 30 for sigma_lower in [0.7, 0.725, 0.75, 0.775]: #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) #Convert to pandas format for MRPS input sensitive_IS_pandas = pd.DataFrame( data=[(sensitive_IS), np.full((len(sensitive_IS)), sigma_min), np.full((len(sensitive_IS)), sigma_lower)]).T sensitive_IS_pandas.columns = [ 'itemset', 'upper_threshold', 'lower_threshold' ] #Run RPS random threshold sanitized_closed_IS = rps_two_thresholds( model=current_model, sensitiveItemsets=sensitive_IS_pandas) #Reproduce frequent itemsets sanitized_DB = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=freq_model['itemsets']) #Plot support graphs dual_support_graph_distribution( freq_model, sanitized_DB, sigma_model, dataset + "_presentation_" + str(sigma_lower) + "_" + str(k_freq)) information_l = information_loss(freq_model.copy(), sanitized_DB) print(sigma_lower, information_l)
def test_artifactual_patterns_with_rps(self): # Produce a sanitised DB with sensitive IS's support below sigma_min sanitized_closed_IS = rps(model=self.original_Closed_IS, sensitiveItemsets=self.sensitive_IS, supportThreshold=self.sigma_min) # Convert from closed to frequent itemsets sanitised_F_IS = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=self.original_IS['itemsets']) # All itemsets in original database a = set(self.original_Freq_IS["itemsets"]) # All itemsets in sanitised database b = set(sanitised_F_IS[sanitised_F_IS["support"] >= self.sigma_min] ["itemsets"]) af = artifactual_patterns(a, b) self.assertEqual(af, 0.0)
def test_information_loss_with_rps(self): # Sensitive closed itemsets whose support needs to be reduced sensitive_IS = {frozenset(['1', '2']), frozenset(['4'])} # Produce a sanitised DB with sensitive IS's support below sigma_min sanitized_closed_IS = rps(model=self.original_Closed_IS, sensitiveItemsets=sensitive_IS, supportThreshold=self.sigma_min) # Convert from closed to frequent itemsets sanitised_F_IS = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=self.original_IS['itemsets']) # Give all itemsets and supports in D a = self.original_IS #We want frequent itemsets only # Give all itemsets and supports in D' b = sanitised_F_IS il = information_loss(a, b) self.assertEqual(0.3133, round(il, 4)) #Using expected IL found in excel
def main(datasets, algorithm, i): #Create the base of a table table_11 = pd.DataFrame(columns=[ 'Model', 'Support threshold', 'Model threshold', 'Sensitive itemsets', 'Number of FI before sanitization', 'Number of FI containing an element of S 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', 'RPS Time' ]) table_10 = pd.DataFrame(columns=[ 'Dataset', 'Model threshold', 'Number of Closed frequent itemsets', 'Number of frequent itemsets', 'Time closed itemsets' ]) #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") #Start total timer total_time_start = time.time() #Convert to closed itemsets current_model, freq_model = get_closed_itemsets(data, sigma_model) new_row = { 'Dataset': dataset, 'Model threshold': sigma_model, 'Number of Closed frequent itemsets': len(current_model), 'Number of frequent itemsets': len(freq_model), 'Time closed itemsets': time.time() - total_time_start } print(new_row) table_10 = table_10.append(new_row, ignore_index=True) table_10.to_csv('table_10.csv') #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]: print("-", dataset, ":", k_freq, "Sensitive itemsets") #Copy the model so we can edit it directly copied_model = current_model.copy() #We pick sensitive itemsets here sensitive_IS = get_top_k_sensitive_itemsets( freq_original, k_freq) num_FI_containing_S = count_FI_containing_S( freq_original, sensitive_IS) if algorithm == "RPS": #Start timer for RPS portion total_time_start = time.time() #Run RPS sanitized_closed_IS = rps(model=copied_model, sensitiveItemsets=sensitive_IS, supportThreshold=sigma_min) elif algorithm == "MRPS": #Convert to pandas format for MRPS input sensitive_IS_pandas = pd.DataFrame( data=[(sensitive_IS), np.full((len(sensitive_IS)), sigma_min), np.full((len(sensitive_IS)), sigma_min - 0.5 * (sigma_min - sigma_model))]).T sensitive_IS_pandas.columns = [ 'itemset', 'upper_threshold', 'lower_threshold' ] #Start timer for RPS portion total_time_start = time.time() #Run RPS random threshold sanitized_closed_IS = rps_two_thresholds( model=copied_model, sensitiveItemsets=sensitive_IS_pandas) #Reproduce frequent itemsets sanitized_DB = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=freq_model['itemsets']) rps_time = time.time() #Calculating metrics #Variables needed freq_sanitized = sanitized_DB.loc[ sanitized_DB["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 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(), sanitized_DB) #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 = rps_time - total_time_start #Threshold sanitized database by threshold_min to get frequent itemsets print(f'- RPS time: {end_time}') #Plot support graphs dual_support_graph_distribution( freq_model, sanitized_DB, sigma_model, dataset + "_" + str(i) + "_" + 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), 'Number of FI containing an element of S before sanitization': num_FI_containing_S, '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, 'RPS 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_11_' + str(i) + '.csv')
def main(): min_support = 0.01 #Support threshold used min_confidence = 0.05 #Confidence threshold used print('========== Importing Dataset ==========') basket_sets = im.import_dataset( "toydata" ) #Insert any of the datasets listed above here to import them print('=======================================\n') # Gather all itemsets power_set_of_items = fpgrowth(basket_sets, min_support=(1 / len(basket_sets)), use_colnames=True) # Find frequent itemsets above support threshold min_support frequent_itemsets = fpgrowth(basket_sets, min_support=min_support, use_colnames=True) # Compute closed itemsets from database closed_itemsets, _ = get_closed_itemsets(basket_sets, 1 / len(basket_sets)) # Recover the original itemsets from the list of closed itemsets recovered_itemsets = itemsets_from_closed_itemsets( closed_itemsets=closed_itemsets, possible_itemsets=power_set_of_items['itemsets']) assert recovered_itemsets.equals(power_set_of_items) # Sanitize database sanitized_closed_itemsets = rps( reference_model=closed_itemsets, sensitiveItemsets={frozenset(['1', '2']), frozenset(['4'])}, supportThreshold=0.3) sanitized_database = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_itemsets, possible_itemsets=power_set_of_items['itemsets']) print('Raw Database:') print(power_set_of_items) print() print('Sanitized Database:') print(sanitized_database) print() print(f'Frequent Itemsets above min_sup {min_support}:') print(frequent_itemsets) print() # print(frequent_itemsets) if frequent_itemsets.shape[0] > 0: rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=min_confidence) if rules.shape[0] > 0: print(rules[rules['confidence'] >= 0.0]) else: print("Confidence too low, no rules were found") else: print("Support too low, no frequent item sets found")
def main(datasets): 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) #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 sensitive_IS_pandas = pd.DataFrame( data=[(sensitive_IS), np.array( [0.8, 0.74, 0.8, 0.74, 0.8, 0.74, 0.8, 0.74, 0.8, 0.74]), np.array([ 0.78, 0.72, 0.78, 0.72, 0.78, 0.72, 0.78, 0.72, 0.78, 0.72, ])]).T print(sensitive_IS_pandas) sensitive_IS_pandas.columns = [ 'itemset', 'upper_threshold', 'lower_threshold' ] #Run RPS random threshold sanitized_closed_IS = rps_two_thresholds( model=current_model, sensitiveItemsets=sensitive_IS_pandas) #Reproduce frequent itemsets sanitized_DB = itemsets_from_closed_itemsets( closed_itemsets=sanitized_closed_IS, possible_itemsets=freq_model['itemsets']) #Plot support graphs dual_support_graph_distribution( freq_model, sanitized_DB, sigma_model, dataset + "_presentation_10_bins_" + str(k_freq)) for sensitive in sensitive_IS: print( sensitive, ":", sanitized_DB.loc[sanitized_DB['itemsets'] == sensitive] ["support"].values[0], ":", freq_model.loc[ freq_model['itemsets'] == sensitive]["support"].values[0]) information_l = information_loss(freq_model.copy(), sanitized_DB) print(information_l)
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)