-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_analysis.py
126 lines (86 loc) · 3.58 KB
/
user_analysis.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
import json
import networkx as nx
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from utils import *
users_location = "../dataset_tweego/users"
screen_name = "all_10k"
# graph = nx.read_gml("{}/{}_features.gml".format(dump_location, screen_name))
# graph_nodes = list(graph.nodes(data=True))
# data = []
# for index, node in enumerate(tqdm(graph_nodes)):
# user_id = node[1]["userid"]
# label = node[0]
# user_data = []
# if os.path.exists("{}/{}.json".format(users_location, str(user_id))):
# user = json.load(open("{}/{}.json".format(users_location, str(user_id)), "r"))
# user_data.append(user["id_str"])
# user_data.append(user["screen_name"])
# user_data.append(node[1]["verified"])
# user_data.append(user["friends_count"])
# user_data.append(user["followers_count"])
# user_data.append(user["listed_count"])
# user_data.append(user["statuses_count"])
# user_data.append(node[1]["total_count"])
# user_data.append(node[1]["fake"])
# user_data.append(node[1]["political"])
# user_data.append(user["created_at"])
# user_data.append(user["location"])
# data.append(user_data)
# df = pd.DataFrame(data, columns=["userId", "ScreenName", "Verified", "FriendsCount", "FollowersCount", "ListedCount", "StatusesCount", "NewsCount", "Fake", "Political", "CreatedAt", "Location"])
# df.to_csv('{}/{}_features.csv'.format(dump_location, screen_name), index=False)
df = pd.read_csv('{}/{}_features.csv'.format(dump_location, screen_name))
pd.options.display.float_format = '{:.2f}'.format
# print(df[['FriendsCount', 'FollowersCount', 'ListedCount', 'StatusesCount', 'NewsCount', 'Fake']].describe())
# fig = plt.figure(figsize=(15,5))
# ax = fig.gca()
# fol_max = 1000000
# df[df['FollowersCount'] < fol_max].hist(column='FollowersCount', bins=200)
# plt.xticks(np.arange(0, fol_max+1, fol_max/10))
# plt.ylabel("Number of users", fontsize=12)
# plt.xlabel("Number of followers",fontsize=12)
# plt.show()
# fr_max = 5000
# df[df['FriendsCount'] < fr_max].hist(column='FriendsCount', bins=50, ax=ax)
# plt.xticks(np.arange(0, fr_max+1, fr_max/10))
# plt.ylabel("Number of users", fontsize=12)
# plt.xlabel("Number of friends",fontsize=12)
# plt.show()
# status_max = 100000
# df[df['StatusesCount'] < status_max].hist(column='StatusesCount', bins=200, ax=ax)
# plt.xticks(np.arange(0, status_max+1, status_max/10))
# plt.ylabel("Number of users", fontsize=12)
# plt.xlabel("Number of satuses",fontsize=12)
# plt.show()
# news_max = 1000
# df[df['NewsCount'] < news_max].hist(column='StatusesCount', bins=200, ax=ax)
# plt.xticks(np.arange(0, news_max+1, news_max/10))
# plt.ylabel("Number of users", fontsize=12)
# plt.xlabel("Number of news articles",fontsize=12)
# plt.show()
# df["Location"].value_counts()[:50].plot.bar(figsize=(20,7))
# plt.xlabel("Location", fontsize=13)
# plt.ylabel("Number of users",fontsize=13)
# plt.show()
# dates = list(df['CreatedAt'].values)
# created = []
# for date in dates:
# created.append(date.split()[-1])
# df['year'] = created
# df['year'].groupby(df["year"]).count().plot(kind = 'bar', figsize=(15,5))
# plt.xlabel("Year", fontsize=13)
# plt.ylabel("Number of users",fontsize=13)
# plt.show()
# df["Political"].value_counts().plot.bar()
# plt.show()
# df["Verified"].value_counts().plot.bar()
# plt.show()
# df["Political"].value_counts().plot.bar()
# plt.show()
df['Fake_bin'] = [1 if x >= 0.5 else 0 for x in df['Fake']]
# df.hist(column='Fake', bins=20)
# plt.show()
df["Fake_bin"].value_counts().plot.bar()
plt.show()