-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-k-regular-graphs.py
220 lines (119 loc) · 5.57 KB
/
generate-k-regular-graphs.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import pandas as pd
import csv
import igraph
import implicit
import glob
from tqdm import tqdm
from random import shuffle
from utils import get_sparse_adj_martrix
def generate_graph(filename, topk=False):
input_edges = open(filename, "r")
edges_reader = csv.reader(input_edges, delimiter="\t")
mapping_outlinks = {}
for (n1,n2, weight) in edges_reader:
if n1 not in mapping_outlinks:
mapping_outlinks[n1] = {}
mapping_outlinks[n1][n2] = weight
# generate top-k regular graph
edgelist = []
for source_node in mapping_outlinks:
if topk:
one_el = sorted(mapping_outlinks[source_node].items(), key = lambda x: x[1], reverse=True)
if len(one_el) >= topk:
edgelist += [(source_node, new_dest, int(weight)) for (new_dest, weight) in one_el][:topk]
else:
one_el = mapping_outlinks[source_node].items()#[:topk]
edgelist += [(source_node, new_dest, int(weight)) for (new_dest, weight) in one_el]
input_edges.close()
# generate graph
G_video = igraph.Graph.TupleList(edgelist, weights=True, directed=True)
return G_video
def load_graph_and_category(filename, info_videos, topk):
videograph = generate_graph(filename, topk)
mapping_video_id_to_category = {vec[0]: vec[2].strip() for vec in info_videos.values}
# big graph
for n in videograph.vs:
if n["name"] in mapping_video_id_to_category:
n["category"] = mapping_video_id_to_category[n["name"]]
else:
n["category"] = "unknown"
selected_nodes = [n for n in videograph.vs
if n["category"] in ["Alt-lite", "Alt-right", "Intellectual Dark Web", "Media", "unknown"]
]
# selected-graph
papergraph = videograph.subgraph(selected_nodes)
for n in papergraph.vs:
if n["category"] in ["Alt-lite", "Alt-right", "Intellectual Dark Web"]:
n["category"] = "Bad"
return papergraph
###############################################################################
# LOAD GRAPH
###############################################################################
# input-parameters
PATH = "../data/youtube/"
ITERATIONS = 10
FACTORS = 300
ALL_TOPK = [5,10,20]
filename = PATH + "no_sink_video_recommendations.tsv"
scores_out_fn = PATH + "final/yt-scores-distribution.tsv"
topk = False
info_videos = pd.read_csv(PATH + "videos.tsv", sep="\t")
initial_graph_with_weights = load_graph_and_category(filename, info_videos, topk)
initial_graph_with_weights.summary()
if glob.glob(scores_out_fn) == []:
edgelist = [(e.source, e.target) for e in initial_graph_with_weights.es]
edge_weights = [e["weight"] for e in initial_graph_with_weights.es]
edge_weights = 1/(1+np.exp(-np.log2(edge_weights)))
N = initial_graph_with_weights.vcount()
#help(implicit.als.AlternatingLeastSquares)
mm = get_sparse_adj_martrix(edgelist, weights=edge_weights, N=N)
print("Training of ALS")
model = implicit.als.AlternatingLeastSquares(factors=FACTORS,
calculate_training_loss=True,
iterations=ITERATIONS,
#use_native=True
)
# train the model on a sparse matrix of item/user/confidence weights
model.fit(mm.T)
all_nodes = [n.index for n in initial_graph_with_weights.vs]
shuffle(all_nodes)
upper_bound = 100
new_edgelist = {topk: [] for topk in ALL_TOPK}
# score-distribution
scores_distribution = open(scores_out_fn, "w")
writer_scores_distribution = csv.writer(scores_distribution, delimiter="\t")
writer_scores_distribution.writerow(["node", "scores"])
for source in tqdm(all_nodes):
one_lst = model.recommend(source, user_items=mm, N=upper_bound)
# write the scores
writer_scores_distribution.writerow([source] + [(dest, round(score, 5)) for dest,score in one_lst])
one_lst = [(source, new_dest, scores) for new_dest, scores in one_lst if source != new_dest]
for topk in ALL_TOPK:
new_edgelist[topk] += one_lst[:topk]
scores_distribution.close()
else:
scores_distribution = open(scores_out_fn, "r")
reader_scores_distribution = csv.reader(scores_distribution, delimiter="\t")
header = next(reader_scores_distribution)
new_edgelist = {topk: [] for topk in ALL_TOPK}
for row in reader_scores_distribution:
source = row[0]
one_lst = [eval(t) for t in row[1:]]
one_lst = [(source, new_dest, scores) for new_dest, scores in one_lst]
for topk in ALL_TOPK:
new_edgelist[topk] += one_lst[:topk]
for topk in ALL_TOPK:
df_new = pd.DataFrame(new_edgelist[topk], columns=["source","target","weight"])
one_output_filename = PATH + "final/yt-top-%s-edges.tsv"%topk
df_new.to_csv(one_output_filename, sep="\t", index=False)
# nodes
nodes_file = open(PATH + "final/yt-nodes.tsv", "w")
writer_nodes = csv.writer(nodes_file, delimiter="\t")
header_nodes = ["id", "name", "category"]
writer_nodes.writerow(header_nodes)
for n in initial_graph_with_weights.vs:
writer_nodes.writerow([n.index, n["name"], n["category"]])
nodes_file.close()