-
Notifications
You must be signed in to change notification settings - Fork 87
/
EDA.py
232 lines (195 loc) · 10.5 KB
/
EDA.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
220
221
222
223
224
225
226
227
228
229
230
231
# *_*coding:utf-8 *_*
'''Writen by YAN Xu'''
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
import pandas as pd
import datetime
comment = pd.read_csv('./data/comment20181201_03.csv')
tweet = pd.read_csv('./data/tweet20181201_03.csv')
experiment_dir = Path('./result/')
experiment_dir.mkdir(exist_ok=True)
'''合并数据集'''
comment['merge_id'] = comment['weibo_id']
tweet['merge_id'] = tweet['_id']
merge = comment.merge(tweet, on='merge_id')
merge7 = pd.read_csv('./data/merge7.csv',encoding='gbk')
merge_nohaze = pd.read_csv('./data/merge12.csv',encoding='gbk')
merge_nohaze['haze'] = 0
merge_haze = merge.append(merge7)
merge_haze['haze'] = 1
merge = merge_nohaze.append(merge_haze)
merge.index = range(len(merge))
merge['comment_time'] = merge['created_at_x'].map(lambda x: datetime.datetime(int(x[0:4]),int(x[5:7]),int(x[8:10]),int(x[11:13]),int(x[14:16])))
merge['weibo_time'] = merge['created_at_y'].map(lambda x: datetime.datetime(int(x[0:4]),int(x[5:7]),int(x[8:10]),int(x[11:13]),int(x[14:16])))
merge['is_beijing'] = merge['province'].map(lambda x: 1 if x == '北京' else 0)
merge['is_guangdong'] = merge['province'].map(lambda x: 1 if x == '广东' else 0)
merge = merge[(merge['province']!='其他')&(merge['province']!='海外')]
'''描述统计'''
# Draw
plt.style.use('seaborn-white')
comment_counts = merge.groupby('merge_id').size()
plt.hist(comment_counts.values,bins=60, alpha=0.5,
color='steelblue', edgecolor='white')
plt.xlim(100,1000)
plt.ylim(0,25)
plt.xlabel("comment counts")
plt.ylabel("proportion")
plt.title('Distribution of number of comments of one weibo',fontsize=15)
# plt.savefig('./result/%s.png'%name)
plt.show()
'''评论中北京的占比'''
merge_guangdong = merge[merge.is_guangdong == 1]
merge_beijing = merge[merge.is_beijing == 1]
a,b = len(merge_beijing[merge_beijing.haze==1]),len(merge_beijing[merge_beijing.haze==0])
c,d = len(merge_guangdong[merge_guangdong.haze==1]),len(merge_guangdong[merge_guangdong.haze==0])
beijing = [a/(a+c),b/(b+d)]
guangdong = [c/(a+c),d/(b+d)]
x = ['haze', 'no haze']
# Draw
width = 0.5
idx = np.arange(len(x))
plt.figure(figsize=(5,5))
plt.bar(idx, beijing, width, color='red',alpha=0.5, label='Beijing')
plt.bar(idx, guangdong, width, bottom=beijing, color='steelblue',alpha=0.5, label='Guangdong')
plt.xlabel('Haze or not')
plt.ylabel('Proportion')
plt.text(-0.11,0.5,"39.8%",fontdict={'size':'16','color':'black'})
plt.text(0.9,0.5,"41.6%",fontdict={'size':'16','color':'black'})
plt.xticks(idx + width / 2, x, rotation=40)
plt.title('Comment ratio between Guangdong and Beijing',fontsize = 13)
plt.legend()
plt.show()
beijing_comment = merge[merge['is_beijing']==1]
'''时间段统计'''
beijing_comment['comment_time'] = beijing_comment['created_at_x'].map(lambda x: datetime.datetime(int(x[0:4]),int(x[5:7]),int(x[8:10]),int(x[11:13]),int(x[14:16])))
beijing_comment['weibo_time'] = beijing_comment['created_at_y'].map(lambda x: datetime.datetime(int(x[0:4]),int(x[5:7]),int(x[8:10]),int(x[11:13]),int(x[14:16])))
beijing_comment['hour'] = beijing_comment['comment_time'].apply(lambda x: x.hour)
beijing_comment['weekday'] = beijing_comment['comment_time'].apply(lambda x: x.weekday())
beijing_comment_weekday = beijing_comment[(beijing_comment['weekday']!=6) & (beijing_comment['weekday']!=0)]
beijing_comment_weekend = beijing_comment[(beijing_comment['weekday']==6) | (beijing_comment['weekday']==0)]
# Draw
plt.hist(beijing_comment_weekend['hour'][beijing_comment_weekend.haze==1], alpha=0.5,
color='steelblue', label='Haze', edgecolor='white',normed=True)
plt.hist(beijing_comment_weekend['hour'][beijing_comment_weekend.haze==0], alpha=0.5,
color='red',label='No Haze', edgecolor='white',normed=True)
plt.xlabel("Haze or not")
plt.ylabel("Proportion")
plt.title('Time Distribution of comments on weekend',fontsize=15)
plt.legend()
# plt.savefig('./result/%s.png'%name)
plt.show()
plt.hist(beijing_comment_weekday['hour'][beijing_comment_weekday.haze==1], alpha=0.5,
color='steelblue', label='Haze', edgecolor='white',normed=True)
plt.hist(beijing_comment_weekday['hour'][beijing_comment_weekday.haze==0], alpha=0.5,
color='red',label='No Haze', edgecolor='white',normed=True)
plt.xlabel("Haze or not")
plt.ylabel("Proportion")
plt.title('Time Distribution of comments on weekday',fontsize=15)
plt.legend()
# plt.savefig('./result/%s.png'%name)
plt.show()
'''评论间隔'''
beijing_comment_weekend['time_interval'] = beijing_comment['comment_time'] - beijing_comment['weibo_time']
beijing_comment_weekend['day3'] = beijing_comment['time_interval'].map(lambda x: 1 if x.days<3 else 0)
beijing_comment_weekday['time_interval'] = beijing_comment['comment_time'] - beijing_comment['weibo_time']
beijing_comment_weekday['day3'] = beijing_comment['time_interval'].map(lambda x: 1 if x.days<3 else 0)
day3_weekend = beijing_comment_weekend[beijing_comment_weekend['day3']==1]
day3_weekend['interval_hour'] = day3_weekend.time_interval.apply(lambda x: int(x.seconds/3600)+1)
day3_weekday = beijing_comment_weekday[beijing_comment_weekday['day3']==1]
day3_weekday['interval_hour'] = day3_weekday.time_interval.apply(lambda x: int(x.seconds/3600)+1)
plt.hist(day3_weekend['interval_hour'][day3_weekend.haze==1], alpha=0.5,
color='steelblue', label='No Haze', edgecolor='white',normed=True)
plt.hist(day3_weekend['interval_hour'][day3_weekend.haze==0], alpha=0.5,
color='red', label='Haze', edgecolor='white',normed=True)
plt.xlabel("Comment interval")
plt.ylabel("Proportion")
plt.title('Time interval before comment on weekend',fontsize=15)
plt.legend()
# plt.savefig('./result/%s.png'%name)
plt.show()
plt.hist(day3_weekday['interval_hour'][day3_weekday.haze==1], alpha=0.5,
color='steelblue', label='No Haze', edgecolor='white',normed=True)
plt.hist(day3_weekday['interval_hour'][day3_weekday.haze==0], alpha=0.5,
color='red', label='Haze', edgecolor='white',normed=True)
plt.xlabel("Comment interval")
plt.ylabel("Proportion")
plt.title('Time interval before comment on weekday',fontsize=15)
plt.legend()
# plt.savefig('./result/%s.png'%name)
plt.show()
plt.figure(figsize=(10,6))
labels = ['Within 2 hours','2-10 hours','More than 10 hours']
a = len(day3_weekday[(day3_weekday.interval_hour<5) & (day3_weekday.haze==1)])
b = len(day3_weekday[(day3_weekday.interval_hour>5)&(day3_weekday.interval_hour<10)&(day3_weekday.haze==1)])
c = len(day3_weekday[(day3_weekday.interval_hour>10)&(day3_weekday.haze==1)])
sizes = [a,b,c]
colors = ['red','yellowgreen','lightskyblue']
explode = (0.05,0,0)
patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors,
labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
startangle = 90,pctdistance = 0.6)
for t in l_text:
t.set_size=(80)
for t in p_text:
t.set_size=(80)
plt.axis('equal')
plt.legend()
plt.title('NO HAZE')
plt.savefig('./result/picture1.png')
plt.show()
'''缺失/水评论统计'''
beijing_comment_weekday['water_rate'] = beijing_comment_weekday['content_x'].map(lambda x: 1 if pd.isnull(x) or len(str(x))<2 else 0)
total_water_rate = beijing_comment_weekday['water_rate'][beijing_comment_weekday.haze==1].mean()
print('雾霾时间段北京地区工作日的评论的水评论占比:%.2f%%'%(total_water_rate*100))
total_water_rate = beijing_comment_weekday['water_rate'][beijing_comment_weekday.haze==0].mean()
print('非雾霾时间段北京地区工作日的评论的水评论占比:%.2f%%'%(total_water_rate*100))
beijing_comment_weekend['water_rate'] = beijing_comment_weekend['content_x'].map(lambda x: 1 if pd.isnull(x) or len(str(x))<2 else 0)
total_water_rate = beijing_comment_weekend['water_rate'][beijing_comment_weekend.haze==1].mean()
print('雾霾时间段北京地区周末的评论的水评论占比:%.2f%%'%(total_water_rate*100))
total_water_rate = beijing_comment_weekend['water_rate'][beijing_comment_weekend.haze==0].mean()
print('非雾霾时间段北京地区周末的评论的水评论占比:%.2f%%'%(total_water_rate*100))
plt.figure(figsize=(10,6))
#定义饼状图的标签,标签是列表
labels = ['Robotic Comment ','Normal']
a = len(beijing_comment_weekday[(beijing_comment_weekday.haze==0)&(beijing_comment_weekday.water_rate==1)])
b = len(beijing_comment_weekday[(beijing_comment_weekday.haze==0)&(beijing_comment_weekday.water_rate==0)])
sizes = [a,b]
colors = ['red','lightskyblue']
explode = (0.05,0)
patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors,
labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
startangle = 90,pctdistance = 0.6)
for t in l_text:
t.set_size=(80)
for t in p_text:
t.set_size=(80)
plt.axis('equal')
plt.legend()
plt.savefig('./result/picture1.png')
plt.show()
plt.bar(range(4), [3.4,6.67,6.05,6.63], width, alpha=0.5)
plt.ylabel('Proportion')
plt.title('Ratio of robotic comment')
plt.xticks(range(4), ('Haze on weekend', 'No haze on weekend', 'Haze on weekday', 'No haze on weekday'))
plt.yticks(np.arange(0, 10, 10))
plt.show()
'''评论的平均长度'''
beijing_comment_weekday['content_x'] = beijing_comment_weekday['content_x'].apply(lambda x: str(x).strip('@').strip('回复') if pd.notnull else x)
beijing_comment_weekday['word_lenth'] = beijing_comment_weekday['content_x'].map(lambda x: 0 if pd.isnull(x) else len(str(x)))
word_lenth1 = np.mean(beijing_comment_weekday['word_lenth'][beijing_comment_weekday.haze==1])
print('雾霾时间段北京地区工作的评论的平均长度:%.2f' % word_lenth1)
word_lenth1 = np.mean(beijing_comment_weekday['word_lenth'][beijing_comment_weekday.haze==0])
print('非雾霾时间段北京地区工作的评论的平均长度:%.2f' % word_lenth1)
beijing_comment_weekend['content_x'] = beijing_comment_weekend['content_x'].apply(lambda x: str(x).strip('@').strip('回复') if pd.notnull else x)
beijing_comment_weekend['word_lenth'] = beijing_comment_weekend['content_x'].map(lambda x: 0 if pd.isnull(x) else len(str(x)))
word_lenth1 = np.mean(beijing_comment_weekend['word_lenth'][beijing_comment_weekend.haze==1])
print('雾霾时间段北京地区周末的评论的平均长度:%.2f' % word_lenth1)
word_lenth1 = np.mean(beijing_comment_weekend['word_lenth'][beijing_comment_weekend.haze==0])
print('非雾霾时间段北京地区周末的评论的平均长度:%.2f' % word_lenth1)
plt.bar(range(4), [24.34,18.53,18.25,19.53], width, alpha=0.5)
plt.ylabel('Counts')
plt.title('Average length of comment')
plt.xticks(range(4), ('Haze on weekend', 'No haze on weekend', 'Haze on weekday', 'No haze on weekday'))
plt.yticks(np.arange(0, 20, 10))
plt.show()