-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster_utils.py
186 lines (152 loc) · 5.57 KB
/
cluster_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import division
import cPickle
import datetime as dt
from collections import defaultdict
import tldextract
import numpy as np
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram
'''
functions to cluster domains by quotation pattern.
'''
def load_mentions(mentions):
'''
reads list of mentions (as outputted by postprocess_matches.py) and returns:
list of citations as (domain, id of quote group cited)
dict of (domain, id) to representative article
dict of quote group id to most frequent representative quote of that group
(listed as a quote aligment, not raw text)
'''
fid_to_counts = defaultdict(dict)
dom_fid_to_article = {}
dom_fid_set = set()
for m in mentions:
fid = m[0]
alignment = m[1]
article = m[2]
domain = tldextract.extract(article[1]).domain
count = fid_to_counts[fid].get(alignment, 0)
fid_to_counts[fid][alignment] = count + 1
timestamp = article[2]
rep = dom_fid_to_article.get((domain, fid), None)
if rep is None or timestamp < rep[0][2]:
dom_fid_to_article[(domain, fid)] = (article, alignment)
dom_fid_set.add((domain, fid))
fid_freq_reps = {}
for fid, countdict in fid_to_counts.iteritems():
sorted_by_count = sorted(countdict.iteritems(), key=lambda x: x[1])
fid_freq_reps[fid] = sorted_by_count[-1][0]
return list(dom_fid_set), dom_fid_to_article, fid_freq_reps
def get_domain_fid_matrix(dom_fid, fid_set = None):
'''
given list of citations [(domain, quote group id)]
produces binary matrix of domains to quotes, with values = 1 (domain cited quote)
in binary matrix, domains ordered by frequency; quotes ordered by group id value.
returns binary matrix, list of (domain, frequency) ordered by frequency,
dict of domain name to index of domain in matrix.
'''
if fid_set is None:
df_list = dom_fid
else:
df_list = [x for x in dom_fid if x[1] in fid_set]
domain_counts = defaultdict(int)
for dom, fid in df_list:
domain_counts[dom] += 1
top_domains = sorted(domain_counts.iteritems(), key=lambda x: x[1], reverse=True)
num_domains = len(top_domains)
domain_to_index = {top_domains[i][0]:i for i in range(len(top_domains))}
num_fids = len(set([x[1] for x in dom_fid]))
dom_fid_matrix = np.zeros((num_domains, num_fids))
for dom, fid in df_list:
dom_index = domain_to_index[dom]
dom_fid_matrix[dom_index,fid] = 1
if fid_set is None:
toreturn = dom_fid_matrix
else:
toreturn = dom_fid_matrix[:,fid_set]
return toreturn, np.array(top_domains), domain_to_index
def get_sim_matrix(bin_matrix):
'''
returns similarity matrix between domains.
'''
col_norm = bin_matrix / np.sqrt(bin_matrix.sum(axis=0))
normed_by_row = col_norm / np.linalg.norm(col_norm, axis=1)[:,np.newaxis]
sim = np.dot(normed_by_row, normed_by_row.T)
return sim
def get_dist_matrix(bin_matrix):
'''
returns distance matrix between domains
'''
return 1-get_sim_matrix(bin_matrix)
def hier_cluster_and_display(dist_matrix, leaf_labels, colorthresh, to_cluster = 'all', m = 'complete',
imgsize = 25, fontsize=16):
'''
clusters domains using hierarchical clustering and displays dendrogram.
arguments:
dist_matrix : distance matrix between domains
leaf_labels: list of domain names
colorthresh: threshold to color dendrogram nodes
to_cluster (list of ints, optional, default='all'):
if 'all', clusters all domains
else clusters only domains corresponding to indices in list
m (default='complete'): method used in hierarchical clustering.
'single' and 'average' also work; as in scipy.
imgsize (default=25): size of image (imgsize,imgsize) of dendrogram to produce.
fontsize (default=16): font size of dendrogram leaf labels.
returns:
result as outputted by scipy's hierarchical clustering.
'''
if to_cluster == 'all':
cluster_indices = range(dist_matrix.shape[0])
else:
cluster_indices = to_cluster
plt.figure(figsize=(imgsize,imgsize))
result = hier_cluster(dist_matrix,cluster_indices,m)
dendrogram(result,orientation='left',
labels=leaf_labels[cluster_indices], color_threshold=colorthresh, leaf_font_size=fontsize)
return result
def display_cluster_result(result, leaf_labels, colorthresh, to_cluster = 'all',
imgsize = 25, fontsize = 16):
'''
displays dendrogram corresponding to a clustering result.
'''
if to_cluster == 'all':
cluster_indices = range(len(leaf_labels))
else:
cluster_indices = to_cluster
plt.figure(figsize=(imgsize,imgsize))
dendrogram(result, orientation='left',labels=leaf_labels[cluster_indices], color_threshold=colorthresh,
leaf_font_size=fontsize)
def hier_cluster(dist_matrix, to_cluster = 'all', m='complete'):
'''
uses hierarchical clustering with specified distance matrix.
'''
if to_cluster == 'all':
cluster_indices = range(dist_matrix.shape[0])
else:
cluster_indices = to_cluster
dist_submatrix = dist_matrix[cluster_indices,:][:,cluster_indices]
result = linkage(dist_submatrix, method=m)
return result
def convert_to_indices(domain_list, domain_to_index):
'''
converts a list of domain names in domain_list to a list of indices given by
domain_to_index.
'''
index_list = []
for d in domain_list:
try:
index_list.append(domain_to_index[d])
except KeyError:
print 'No domain '+d
return
return index_list
def get_avg_indistance(dist_matrix, index_list):
'''
computes the average distance between all domains in index_list, where
index list is a list of indices into the distance matrix.
'''
dist_submatrix = dist_matrix[index_list,:][:,index_list]
dist_sum = sum(sum(dist_submatrix))
num_items = len(index_list)
return dist_sum / (num_items * (num_items-1))