def make_scatter(congress_num): fig = plt.figure(1) arr = hs.make_similarity_array(congress_num) cls, means, steps = mlpy.kmeans(arr, k=2, plus=True) members = hs.members(congress_num) extreme_index1 = members.index(most_least.most_extreme(congress_num, 10)[0]) extreme_index2 = list(arr[extreme_index1]).index(min(arr[extreme_index1])) if members[extreme_index1].split(' ')[1] == 'R': #change color so Democrats are blue and Republicans are red for i in range(len(cls)): if cls[i] == 0: cls[i] = 1 else: cls[i] = 0 plot = plt.scatter(arr[:, extreme_index1], arr[:, extreme_index2], c=cls, alpha=0.75) plt.xlabel("Conservatism (Cosine similarity to most conservative member, " + members[extreme_index1].split(' ')[0] + ")") plt.ylabel("Liberalism (Cosine similarity to most liberal member, " + members[extreme_index2].split(' ')[0] + ")") else: plot = plt.scatter(arr[:, extreme_index2], arr[:, extreme_index1], c=cls, alpha=0.75) plt.ylabel("Liberalism (Cosine similarity to most liberal member, " + members[extreme_index1].split(' ')[0] + ")") plt.xlabel("Conservatism (Cosine similarity to most conservative member, " + members[extreme_index2].split(' ')[0] + ")") return
def most_unique(congress_num, knn): arr = hs.make_similarity_array(congress_num) congresspeople = hs.members(congress_num) #takes k-nearest members in cos similarity sorted_similarity = [sorted(arr[i])[-knn:] for i in range(len(arr[0]))] sums = [sum(sorted_similarity[i]) for i in range(len(sorted_similarity))] sorted_unique = sorted(range(len(sums)), key=lambda k: sums[k]) unique_rank = [(congresspeople[index]) for index in sorted_unique] return unique_rank
def most_extreme(congress_num, knn): arr = hs.make_similarity_array(congress_num) congresspeople = hs.members(congress_num) #takes k-furthest members in cos similarity sorted_similarity = [sorted(arr[i])[:knn] for i in range(len(arr[0]))] #sums those similarities sums = [sum(sorted_similarity[i]) for i in range(len(sorted_similarity))] #sorts members from lowest to highest sorted_extreme = sorted(range(len(sums)), key=lambda k: sums[k]) extreme_rank = [(congresspeople[index]) for index in sorted_extreme] return extreme_rank