-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayes.py
202 lines (178 loc) · 13 KB
/
bayes.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
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 23:55:10 2017
@author: Lei Su
"""
import numpy as np
import pandas as pd
from pandas import DataFrame
DataLoad_bot = pd.read_csv('bots_data.csv')
DataSet_bot = DataLoad_bot[['followers_count','friends_count','listedcount','favourites_count','verified','statuses_count','default_profile','has_extended_profile','bot']]
DataLoad_nonbot = pd.read_csv('nonbots_data.csv')
DataSet_nonbot = DataLoad_nonbot[['followers_count','friends_count','listedcount','favourites_count','verified','statuses_count','default_profile','has_extended_profile','bot']]
DataSet = pd.concat([DataSet_bot,DataSet_nonbot])
DataSet = np.random.permutation(DataSet)
startnum_train = 1800
endnum_train = 2197
train_data = DataFrame(DataSet[:startnum_train,:9],columns=['followers_count','friends_count','listed_count','favourites_count','verified','statuses_count','default_profile','has_extended_profile','bot'])
train_label = DataFrame(DataSet[:startnum_train,8:9],columns=['bot'])
test_data = DataFrame(DataSet[startnum_train:endnum_train,:8],columns=['followers_count','friends_count','listed_count','favourites_count','verified','statuses_count','default_profile','has_extended_profile'])
test_label = DataFrame(DataSet[startnum_train:endnum_train,8:9],columns=['bot'])
#train
p1 = float(train_label.apply(lambda x:x.sum()))/train_label.shape[0]
p0 = 1-p1
#followers_count attribute
range_followers = 100
p1_followers_0 = float(train_data.loc[(train_data['followers_count']<=range_followers)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_followers_1 = float(train_data.loc[(train_data['followers_count']>range_followers)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_followers_0 = float(train_data.loc[(train_data['followers_count']<=range_followers)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_followers_1 = float(train_data.loc[(train_data['followers_count']>range_followers)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
#friends_count attribute
range_friends = 100
p1_friends_0 = float(train_data.loc[(train_data['friends_count']<=range_friends)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_friends_1 = float(train_data.loc[(train_data['friends_count']>range_friends)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_friends_0 = float(train_data.loc[(train_data['friends_count']<=range_friends)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_friends_1 = float(train_data.loc[(train_data['friends_count']>range_friends)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
#listed_count attribute
range_listed = 100
p1_listed_0 = float(train_data.loc[(train_data['listed_count']<=range_listed)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_listed_1 = float(train_data.loc[(train_data['listed_count']>range_listed)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_listed_0 = float(train_data.loc[(train_data['listed_count']<=range_listed)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_listed_1 = float(train_data.loc[(train_data['listed_count']>range_listed)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
range_favourites = 100
p1_favourites_0 = float(train_data.loc[(train_data['favourites_count']<=range_favourites)&(train_data['bot']==1)+1].shape[0])/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_favourites_1 = float(train_data.loc[(train_data['favourites_count']>range_favourites)&(train_data['bot']==1)+1].shape[0])/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_favourites_0 = float(train_data.loc[(train_data['favourites_count']<=range_favourites)&(train_data['bot']==0)+1].shape[0])/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_favourites_1 = float(train_data.loc[(train_data['favourites_count']>range_favourites)&(train_data['bot']==0)+1].shape[0])/(train_data.loc[train_data['bot']==0].shape[0]+1)
#verified attribute
p1_verified_0 = float(train_data.loc[(train_data['verified']==1)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_verified_1 = float(train_data.loc[(train_data['verified']==0)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_verified_0 = float(train_data.loc[(train_data['verified']==1)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_verified_1 = float(train_data.loc[(train_data['verified']==0)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
#statuses_count attribute
range_statuses = 100
p1_statuses_0 = float(train_data.loc[(train_data['statuses_count']<=range_statuses)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_statuses_1 = float(train_data.loc[(train_data['statuses_count']>range_statuses)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_statuses_0 = float(train_data.loc[(train_data['statuses_count']<=range_statuses)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_statuses_1 = float(train_data.loc[(train_data['statuses_count']>range_statuses)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
#default_profile attribute
p1_profile_0 = float(train_data.loc[(train_data['default_profile']==1)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_profile_1 = float(train_data.loc[(train_data['default_profile']==0)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_profile_0 = float(train_data.loc[(train_data['default_profile']==1)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_profile_1 = float(train_data.loc[(train_data['default_profile']==0)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
#has_extended_profile attribute
p1_extendedprofile_0 = float(train_data.loc[(train_data['has_extended_profile']==1)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p1_extendedprofile_1 = float(train_data.loc[(train_data['has_extended_profile']==0)&(train_data['bot']==1)].shape[0]+1)/(train_data.loc[train_data['bot']==1].shape[0]+1)
p0_extendedprofile_0 = float(train_data.loc[(train_data['has_extended_profile']==1)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
p0_extendedprofile_1 = float(train_data.loc[(train_data['has_extended_profile']==0)&(train_data['bot']==0)].shape[0]+1)/(train_data.loc[train_data['bot']==0].shape[0]+1)
#classify
test_bak1 = DataFrame(test_data)
#followers_count
for a0 in test_bak1.loc[test_bak1['followers_count']<=range_followers].index:
test_bak1['followers_count'].loc[a0] = p1_followers_0
for a1 in test_bak1.loc[test_bak1['followers_count']>range_followers].index:
test_bak1['followers_count'].loc[a1] = p1_followers_1
#friends_count
for a0 in test_bak1.loc[test_bak1['friends_count']<=range_friends].index:
test_bak1['friends_count'].loc[a0] = p1_friends_0
for a1 in test_bak1.loc[test_bak1['friends_count']>range_friends].index:
test_bak1['friends_count'].loc[a1] = p1_friends_1
#listed_count
for a0 in test_bak1.loc[test_bak1['listed_count']<=range_listed].index:
test_bak1['listed_count'].loc[a0] = p1_listed_0
for a1 in test_bak1.loc[test_bak1['listed_count']>range_listed].index:
test_bak1['listed_count'].loc[a1] = p1_listed_1
#favourites_count
for a0 in test_bak1.loc[test_bak1['favourites_count']<=range_favourites].index:
test_bak1['favourites_count'].loc[a0] = p1_favourites_0
for a1 in test_bak1.loc[test_bak1['favourites_count']>range_favourites].index:
test_bak1['favourites_count'].loc[a1] = p1_favourites_1
#verified
for a0 in test_bak1.loc[test_bak1['verified']==1].index:
test_bak1['verified'].loc[a0] = p1_verified_0
for a1 in test_bak1.loc[test_bak1['verified']==0].index:
test_bak1['verified'].loc[a1] = p1_verified_1
#status
for a0 in test_bak1.loc[test_bak1['statuses_count']<=range_statuses].index:
test_bak1['statuses_count'].loc[a0] = p1_statuses_0
for a1 in test_bak1.loc[test_bak1['statuses_count']>range_statuses].index:
test_bak1['statuses_count'].loc[a1] = p1_statuses_1
#default_profile
for a0 in test_bak1.loc[test_bak1['default_profile']==1].index:
test_bak1['default_profile'].loc[a0] = p1_profile_0
for a1 in test_bak1.loc[test_bak1['default_profile']==0].index:
test_bak1['default_profile'].loc[a1] = p1_profile_1
#has_extended_profile
for a0 in test_bak1.loc[test_bak1['has_extended_profile']==1].index:
test_bak1['has_extended_profile'].loc[a0] = p1_extendedprofile_0
for a1 in test_bak1.loc[test_bak1['has_extended_profile']==0].index:
test_bak1['has_extended_profile'].loc[a1] = p1_extendedprofile_1
test_bak1['prob'] = test_bak1['followers_count']*test_bak1['friends_count']*test_bak1['listed_count']*test_bak1['favourites_count']*test_bak1['verified']*test_bak1['statuses_count']*test_bak1['default_profile']*test_bak1['has_extended_profile']*p1
test_bak0 = DataFrame(test_data)
#followers_count
for a0 in test_bak0.loc[test_bak0['followers_count']<=range_followers].index:
test_bak0['followers_count'].loc[a0] = p0_followers_0
for a1 in test_bak0.loc[test_bak0['followers_count']>range_followers].index:
test_bak0['followers_count'].loc[a1] = p0_followers_1
#friends_count
for a0 in test_bak0.loc[test_bak0['friends_count']<=range_friends].index:
test_bak0['friends_count'].loc[a0] = p0_friends_0
for a1 in test_bak0.loc[test_bak0['friends_count']>range_friends].index:
test_bak0['friends_count'].loc[a1] = p0_friends_1
#listed_count
for a0 in test_bak0.loc[test_bak0['listed_count']<=range_listed].index:
test_bak0['listed_count'].loc[a0] = p0_listed_0
for a1 in test_bak0.loc[test_bak0['listed_count']>range_listed].index:
test_bak0['listed_count'].loc[a1] = p0_listed_1
#favourites_count
for a0 in test_bak0.loc[test_bak0['favourites_count']<=range_favourites].index:
test_bak0['favourites_count'].loc[a0] = p0_favourites_0
for a1 in test_bak0.loc[test_bak0['favourites_count']>range_favourites].index:
test_bak0['favourites_count'].loc[a1] = p0_favourites_1
#verified
for a0 in test_bak0.loc[test_bak0['verified']==1].index:
test_bak0['verified'].loc[a0] = p0_verified_0
for a1 in test_bak0.loc[test_bak0['verified']==0].index:
test_bak0['verified'].loc[a1] = p0_verified_1
#status
for a0 in test_bak0.loc[test_bak0['statuses_count']<=range_statuses].index:
test_bak0['statuses_count'].loc[a0] = p0_statuses_0
for a1 in test_bak0.loc[test_bak0['statuses_count']>range_statuses].index:
test_bak0['statuses_count'].loc[a1] = p0_statuses_1
#default_profile
for a0 in test_bak0.loc[test_bak0['default_profile']==1].index:
test_bak0['default_profile'].loc[a0] = p0_profile_0
for a1 in test_bak0.loc[test_bak0['default_profile']==0].index:
test_bak0['default_profile'].loc[a1] = p0_profile_1
#has_extended_profile
for a0 in test_bak0.loc[test_bak0['has_extended_profile']==1].index:
test_bak0['has_extended_profile'].loc[a0] = p0_extendedprofile_0
for a1 in test_bak0.loc[test_bak0['has_extended_profile']==0].index:
test_bak0['has_extended_profile'].loc[a1] = p0_extendedprofile_1
test_bak0['prob'] = test_bak0['followers_count']*test_bak0['friends_count']*test_bak0['listed_count']*test_bak0['favourites_count']*test_bak0['verified']*test_bak0['statuses_count']*test_bak0['default_profile']*test_bak0['has_extended_profile']*p0
result = list(test_bak1['prob']>test_bak0['prob'])
# result_pos = list(test_bak1['prob']>test_bak0['prob'])
# result_neg = list(test_bak1['prob']<test_bak0['prob'])
pred = DataFrame(result,columns=['bot'])
# pred_pos = DataFrame(result_pos, columns=['bot'])
# pred_neg = DataFrame(result_neg, columns=['bot'])
# test_pos = DataFrame(list(test_label['bot']==1), columns=['bot'])
# test_neg = DataFrame(list(test_label['bot']==0), columns=['bot'])
# print(test_label)
# print(test_pos)
# print(test_neg)
# print(pred_pos)
# print(pred_neg)
# print(pred)
# #precision = TP/(TP+FP)
# precision = float(pred_pos.loc[pred_pos['bot']==test_pos['bot']].shape[0])/pred_pos.shape[0]
# print('Precision:', precision)
# #recall = TP/(TP+FN)
# recall = float(pred_pos.loc[pred_pos['bot']==test_pos['bot']].shape[0])/test_pos.shape[0]
# print('Recall:', recall)
# # error_rate = (FN+FP)/N
# # error_rate = 1 - (TP+TN)/N
# error_rate = 1 - float(pred_pos.loc[pred_pos['bot']==test_pos['bot']].shape[0] + pred_neg.loc[pred_neg['bot']==test_neg['bot']].shape[0])/test_data.shape[0]
# print('Error rate:', error_rate)
accuracy = float(pred.loc[pred['bot']==test_label['bot']].shape[0])/pred.shape[0]
print('Predict Accuracy:',accuracy)