forked from LuoUndergradXJTU/TwiBot-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
172 lines (146 loc) · 5.45 KB
/
process.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
import pandas as pd
import numpy as np
import math
import json
from sklearn.metrics import roc_auc_score as auc
from numpy import histogram_bin_edges as bin
from sklearn.ensemble import RandomForestClassifier
import torch
from tqdm import tqdm
from scipy.stats import entropy
import argparse
from datetime import datetime as dt
t0 = dt.strptime('Tue Sep 1 00:00:00 +0000 2020 ','%a %b %d %X %z %Y ')
def handle_data(df):
for i in range(len(df)):
#print(i)
if 'protected' not in df[i].keys() or df[i]['protected'] == 'False' :
df[i]['protected'] = 0
else:
df[i]['protected'] = 1
if 'verified' not in df[i].keys() or df[i]['verified'] == 'False' :
df[i]['verified'] = 0
else:
df[i]['verified'] = 1
if type(df[i]['id']) == type('str'):
df[i]['id'] = int(df[i]['id'][1:])
if 'name' not in df[i].keys() or df[i]['name'] is None:
df[i]['name'] = 0
if type(df[i]['name']) == type('str'):
df[i]['name'] = len(df[i]['name'])
else:
df[i]['name'] = 0
if 'description' not in df[i].keys() or df[i]['description'] is None:
df[i]['description'] = 0
else:
df[i]['description'] = len(df[i]['description'])
if 'created_at' not in df[i].keys() or df[i]['created_at'] is None:
df[i]['created_at'] = 0
else:
if args.datasets == 'Twibot-22':
df[i]['created_at'] = (t0-dt.strptime(data[i]['created_at'],'%Y-%m-%d %X%z')).days
elif args.datasets == 'cresci-2015':
df[i]['created_at'] = (t0-dt.strptime(data[i]['created_at'],'%a %b %d %X %z %Y')).days
else:
df[i]['created_at'] = (t0-dt.strptime(data[i]['created_at'],'%a %b %d %X %z %Y ')).days
if 'url' not in df[i].keys() or df[i]['url'] is None or df[i]['url'] == '':
df[i]['url'] = 0
else:
df[i]['url'] = 1
if 'location' not in df[i].keys() or df[i]['location'] is None or df[i]['location'] == '':
df[i]['location'] = 0
else:
df[i]['location'] = 1
return df
parser = argparse.ArgumentParser(description="Reproduction of Kudugunta et al. with SMOTENN and rain forest")
parser.add_argument("--datasets", type=str, default="Twibot-22", help="dataset name")
args = parser.parse_args()
dir = "../../datasets/"
print("loading data...")
labels = pd.read_csv(dir+args.datasets+"/label.csv")
user_num = len(labels)
y = np.zeros(user_num)
dict={}
if args.datasets == "Twibot-22":
data = json.load(open(dir+args.datasets+"/user.json"))
else:
data = json.load(open(dir+args.datasets+"/node.json"))
split= pd.read_csv(dir+args.datasets+'/split.csv')
for i in range(user_num):
dict[split['id'][i]] = i
for i in range(user_num):
if split['split'][i] == "valid" or split['split'][i] == 'val':
j=i
break
for i in range(j,user_num):
if split['split'][i] == "test":
k=i
break
for i in range(user_num):
if labels['label'][i]=="bot":
y[dict[labels['id'][i]]] = 1
train_mask=range(j)
val_mask=range(j,k)
test_mask=range(k,user_num)
print("begin to process data...")
data = handle_data(data)
feature_num = 11
tot = user_num
print(len(data))
feature = ['followers_count','following_count','tweet_count','listed_count',
'id','name', 'description','created_at','url','verified','location']
X = np.zeros((tot,feature_num))
for i in tqdm(range(tot)):
s ='u'+str(data[i]['id'])
if s not in dict.keys():
s = data[i]['id']
for j in range(4):
X[dict[s]][j] = data[i]['public_metrics'][feature[j]]
for j in range(4,feature_num):
X[dict[s]][j]= data[i][feature[j]]
print("begin to load edge...")
edge = pd.read_csv(dir+args.datasets+'/edge.csv')
edge_fr = edge.loc[edge['relation']=="friend"]
friend = np.zeros((user_num,1000))
count = np.zeros(user_num,dtype=np.int)
for i in tqdm(range(len(edge_fr))):
if edge_fr['source_id'].iloc[i] in dict.keys() and edge_fr['target_id'].iloc[i] in dict.keys():
#print(edge['source_id'][i])
friend[dict[edge_fr['source_id'].iloc[i]]][count[dict[edge_fr['source_id'].iloc[i]]]] = dict[edge_fr['target_id'].iloc[i]]
count[dict[edge_fr['source_id'].iloc[i]]] +=1
b = bin(X[:,10],bins='fd')
l = np.zeros(11,dtype=np.int)
for i in range(11):
l[i] = len(bin(X[:,i],bins='fd'))
b = []
for i in range(11):
b.append(bin(X[:,i],bins='fd'))
p = []
for i in range(11):
p.append(np.zeros(l[i]))
for i in tqdm(range(11)):
for j in range(user_num):
pos = np.searchsorted(b[i],X[j][i],side='right')
p[i][pos-1] += 1
for i in range(11):
sum = p[i].sum()
for j in range(l[i]):
p[i][j] /= sum
friend = friend.astype(np.int)
print("begin to calculate friend preference...")
fp = X.copy()
for i in tqdm(range(user_num)):
for j in range(11):
op = np.zeros(l[j])
fp[i][j]=0
for k in range(count[i]):
pos = np.searchsorted(b[j],X[friend[i][k]][j],side='right')
op[pos-1] += 1
sum = op.sum()
if sum != 0:
for k in range(l[j]):
op[k] /= sum
fp[i][j] = entropy(op,p[j])
true_X = np.concatenate((fp,X[:,5].reshape((-1,1)),X[:,6].reshape((-1,1))
,X[:,1].reshape((-1,1)),X[:,0].reshape((-1,1))),axis=1)
np.save('./'+args.datasets+'.npy',true_X)