-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfea_extra.py
146 lines (118 loc) · 5.18 KB
/
fea_extra.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
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
"""
@author: huangjunjie
@file: fea_extra.py
@time: 2018/12/10
"""
import os
import sys
import re
import time
import json
import pickle
import logging
import math
import random
import argparse
import subprocess
from collections import defaultdict
import numpy as np
import scipy as sp
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
from sklearn import linear_model
from sklearn import metrics
from common import DATASET_NUM_DIC
class FeaExtra(object):
def __init__(self, dataset='epinions', k=1, debug=False):
filename = './experiment-data/{}-train-{}.edgelist'.format(dataset, k)
if debug:
filename = './test.edgelists'
res = self.init_edgelists(filename=filename)
self.pos_in_edgelists, self.pos_out_edgelists, self.neg_in_edgelists, self.neg_out_edgelists = res
def init_edgelists(self, filename='./experiment-data/epinions-train-1.edgelist'):
pos_out_edgelists = defaultdict(list)
neg_out_edgelists = defaultdict(list)
pos_in_edgelists = defaultdict(list)
neg_in_edgelists = defaultdict(list)
with open(filename) as f:
for line in f.readlines():
x, y, z = line.split()
x = int(x)
y = int(y)
z = int(z)
if z == 1:
pos_out_edgelists[x].append(y)
pos_in_edgelists[y].append(x)
else:
neg_out_edgelists[x].append(y)
neg_in_edgelists[y].append(x)
return pos_in_edgelists, pos_out_edgelists, neg_in_edgelists, neg_out_edgelists
def get_pos_indegree(self, v):
return len(self.pos_in_edgelists[v])
def get_pos_outdegree(self, v):
return len(self.pos_out_edgelists[v])
def get_neg_indegree(self, v):
return len(self.neg_in_edgelists[v])
def get_neg_outdegree(self, v):
return len(self.neg_out_edgelists[v])
def common_neighbors(self, u, v):
u_neighbors = self.pos_in_edgelists[u] + self.neg_in_edgelists[u] + \
self.pos_out_edgelists[u] + self.neg_out_edgelists[u]
v_neighbors = self.pos_in_edgelists[v] + self.neg_in_edgelists[v] + \
self.pos_out_edgelists[v] + self.neg_out_edgelists[v]
return len(set(u_neighbors).intersection(set(v_neighbors)))
def feature_part1(self, u, v):
d_pos_in_u = self.get_pos_indegree(u)
d_neg_in_v = self.get_neg_indegree(v)
d_pos_out_u = self.get_pos_outdegree(u)
d_neg_out_v = self.get_neg_outdegree(v)
# d_pos_in_v = self.get_pos_indegree(v)
# d_neg_in_u = self.get_neg_indegree(u)
# d_pos_out_v = self.get_pos_outdegree(v)
# d_neg_out_u = self.get_neg_outdegree(u)
c_u_v = self.common_neighbors(u, v)
d_out_u = self.get_neg_outdegree(u) + self.get_pos_outdegree(u)
d_in_v = self.get_neg_indegree(v) + self.get_pos_indegree(v)
return d_pos_in_u, d_neg_in_v, d_pos_out_u, d_neg_out_v, c_u_v, d_out_u, d_in_v
def feature_part2(self, u, v):
"""
/^ \v /^ \^ /v \v /v ^\
++
/^ \v /^ \^ /v \v /v ^\
+-
/^ \v /^ \^ /v \v /v ^\
-+
/^ \v /^ \^ /v \v /v ^\
--
"""
d1_1 = len(set(self.pos_out_edgelists[u]).intersection(set(self.pos_in_edgelists[v])))
d1_2 = len(set(self.pos_out_edgelists[u]).intersection(set(self.neg_in_edgelists[v])))
d1_3 = len(set(self.neg_out_edgelists[u]).intersection(set(self.pos_in_edgelists[v])))
d1_4 = len(set(self.neg_out_edgelists[u]).intersection(set(self.neg_in_edgelists[v])))
d2_1 = len(set(self.pos_out_edgelists[u]).intersection(set(self.pos_out_edgelists[v])))
d2_2 = len(set(self.pos_out_edgelists[u]).intersection(set(self.neg_out_edgelists[v])))
d2_3 = len(set(self.neg_out_edgelists[u]).intersection(set(self.pos_out_edgelists[v])))
d2_4 = len(set(self.neg_out_edgelists[u]).intersection(set(self.neg_out_edgelists[v])))
d3_1 = len(set(self.pos_in_edgelists[u]).intersection(set(self.pos_out_edgelists[v])))
d3_2 = len(set(self.pos_in_edgelists[u]).intersection(set(self.neg_out_edgelists[v])))
d3_3 = len(set(self.neg_in_edgelists[u]).intersection(set(self.pos_out_edgelists[v])))
d3_4 = len(set(self.neg_in_edgelists[u]).intersection(set(self.neg_out_edgelists[v])))
d4_1 = len(set(self.pos_in_edgelists[u]).intersection(set(self.pos_in_edgelists[v])))
d4_2 = len(set(self.pos_in_edgelists[u]).intersection(set(self.neg_in_edgelists[v])))
d4_3 = len(set(self.neg_in_edgelists[u]).intersection(set(self.pos_in_edgelists[v])))
d4_4 = len(set(self.neg_in_edgelists[u]).intersection(set(self.neg_in_edgelists[v])))
return d1_1, d1_2, d1_3, d1_4, d2_1, d2_2, d2_3, d2_4, d3_1, d3_2, d3_3, d3_4, d4_1, d4_2, d4_3, d4_4
def get_features(self, u, v):
x11 = self.feature_part1(u, v)
x12 = self.feature_part2(u, v)
return x11 + x12
def main():
fea = FeaExtra(debug=False)
print(fea.get_features(0, 2))
print("test done!")
if __name__ == "__main__":
main()