def learn_voting_rule(self, voting_rule, one_vs_one=None): headers = [] sample_set = [] labels = [] for i in range(len(self.preference_profiles)): preference_profile = self.preference_profiles[i] positional_score_matrix = data_generator.positional_score_matrix( self.candidates, preference_profile) weighted_majority_graph = data_generator.weighted_majority_graph( self.candidates, preference_profile) histogram = data_generator.histogram(self.candidates, preference_profile) x = data_generator.get_feature_vector(positional_score_matrix, weighted_majority_graph, histogram, preference_profile, self.candidates) y = voting_rule(self.candidates, preference_profile) feature_names = data_generator.get_feature_names( positional_score_matrix, weighted_majority_graph) if i == 0: headers = feature_names sample_set.append(x) labels.append(y) print(len(labels)) x_data = learn.load_x(headers, sample_set) y_data = learn.load_y(self.candidates, labels) #print(x_data) #print(y_data) if (one_vs_one): learn.one_vs_one(x_data, y_data) else: learn.train(x_data, y_data)
def test_distance_calculation_time(candidates, preference_profiles, n, distance_type): histograms = [] for pp in preference_profiles: hist = data_generator.histogram(candidates, pp) histograms.append([e[1] for e in hist]) start_time = time.time() for i in range(n): h1 = random.choice(histograms) h2 = random.choice(histograms) distance_type(h1, h2) print("--- seconds ---", (time.time() - start_time) / n)
def ball_tree_test(candidates, preference_profiles, preference_profiles_test, voting_rule, distance_type, k): labels = [] test_labels = [] data_histograms = [] test_histograms = [] for pp in preference_profiles: labels.append(voting_rule(candidates, pp)) hist = data_generator.histogram(candidates, pp) data_histograms.append([e[1] for e in hist]) for pp in preference_profiles_test: hist = data_generator.histogram(candidates, pp) test_histograms.append([e[1] for e in hist]) print(len(data_histograms)) tree = KNN.build_ball_tree(data_histograms, distance_type) count = 0 n = 1000 for i in range(n): indexs = KNN.ball_tree_query(tree, test_histograms[i], k) winners = [labels[index] for index in indexs] print(winners) winner = max(set(winners), key=indexs.count) print(winner) print("this histogram is:", test_histograms[i]) #print("closest_histogram is:",data_histograms[index]) if winner == voting_rule(candidates, preference_profiles_test[i]): count += 1 print('correct') else: print('wrong') print(i / n, "%") print(count / n) return (count / n)
def get_Xs(candidates, preference_profiles): Xs = [] for i in range(len(preference_profiles)): preference_profile = preference_profiles[i] positional_score_matrix = data_generator.positional_score_matrix( candidates, preference_profile) weighted_majority_graph = data_generator.weighted_majority_graph( candidates, preference_profile) histogram = data_generator.histogram(candidates, preference_profile) X = data_generator.get_feature_vector(positional_score_matrix, weighted_majority_graph, histogram, preference_profile, candidates) Xs.append(X) return Xs
def MPSR_tiebreaking(candidates, preference_profile, winners): max_rankings = [] max = 0 histogram = data_generator.histogram(candidates, preference_profile) #print(histogram) for e in histogram: if e[1] > max: max_rankings = [] max_rankings.append(e[0]) max = e[1] elif e[1] == max: max_rankings.append(e[0]) #print(max_rankings) if len(max_rankings) == 1: max_ranking = max_rankings[0] for i in range(len(max_ranking)): if max_ranking[i] in winners: return max_ranking[i] else: return None
def get_histograms(candidates, preference_profiles): histograms = [] for pp in preference_profiles: hist = data_generator.histogram(candidates, pp) histograms.append([e[1] for e in hist]) return histograms