forked from dominikmn/one-million-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheda_posts_vs_categories.py
198 lines (148 loc) · 5.55 KB
/
eda_posts_vs_categories.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.11.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# # Analyze comments vs article categories #2
# Issue link: https://github.com/dominikmn/one-million-posts/issues/2
# %%
from utils import loading, feature_engineering
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
# %%
df_posts = loading.load_extended_posts()
# %%
df_articles = loading.load_articles()
# %% [markdown]
# ## Data preparation
# %% [markdown]
# ### Encode post labels
# %%
cols_label = [c for c in df_posts.columns if c.startswith('label_')]
# %%
df_posts[cols_label] = df_posts[cols_label].replace({0.0:'no',1.0:'yes'})
# %% [markdown]
# ### Add path features
# %%
df_articles = feature_engineering.add_column_path_split(df_articles, base_col='path', target_col='path_split')
df_articles = feature_engineering.add_column_path_depth(df_articles, base_col='path', target_col='path_depth')
# %%
df_articles.head()
# %%
df_posts = feature_engineering.add_column_path_split(df_posts, base_col='article_path', target_col='article_path_split')
df_posts = feature_engineering.add_column_path_depth(df_posts, base_col='article_path', target_col='article_path_depth')
# %%
df_posts.head()
# %% [markdown]
# ## Articles per category
# %% [markdown]
# How many unique categories do we have?
# %%
df_articles['path_split'].nunique()
# %% [markdown]
# How many different paths are there per depth?
# %% tags=[]
df_articles[['path_split','path_depth']].drop_duplicates()['path_depth'].value_counts()\
.reset_index().rename(columns={'index':'path_depth', 'path_depth':'count'})
# %% [markdown]
# What are the paths with depth 7 and 8?
# %%
df_articles[df_articles['path_depth'] >= 7]['path_split'].unique()
# %% [markdown]
# How many articles do we have per parent category?
# %%
df_articles['path_split'].apply(lambda x:x[0]).value_counts()
# %% [markdown]
# For which paths have the most articles been written?
# %%
df_articles['path_split'].value_counts()
# %% [markdown]
# ## Comments per category
# %% [markdown]
# How many _commments_ do we have per category?
# %%
df_posts['article_path_split'].value_counts().reset_index().head(10)
# %% [markdown]
# How many _comments_ do we have per parent category?
# %% tags=[]
df_posts['article_path_split'].apply(lambda x:x[0]).value_counts()
# %% [markdown]
# ## Comments vs Articles per path
# %% [markdown]
# How many articles and posts do we have per path?
# %%
cross_compare = df_posts.pivot_table(index='article_path', values=['id_post', 'id_article'], aggfunc='nunique')\
.rename(columns={'id_article':'article_count', 'id_post':'post_count'})
cross_compare.sort_values(by=['article_count', 'post_count'], ascending=False, inplace=True)
cross_compare
# %%
d=cross_compare[['post_count','article_count']].head(20)
plot = plt.scatter(y=d.index, x=d['post_count'], c=d['post_count'], cmap='Reds')
plt.clf()
plt.colorbar(plot)
ax = sns.barplot(data=d, y=d.index, x=d['article_count'], hue='post_count', palette='Reds', dodge=False)
ax.set_ylabel('Categories')
ax.set_xlabel('Number of articles')
ax.legend_.remove()
ax.figure.set_size_inches(12,10)
# %%
# Calculate the percentage of both columns
#cross_compare['article_portion'] = cross_compare['article_count']/cross_compare['article_count'].sum()*100
#cross_compare['article_portion'] = cross_compare['article_portion'].round(3)
#cross_compare['post_portion'] = cross_compare['post_count']/cross_compare['post_count'].sum()*100
#cross_compare['post_portion'] = cross_compare['post_portion'].round(3)
#cross_compare
# %% [markdown]
# ### colored by comments per article
# %%
df_posts["article_path_3"] = df_posts.article_path_split.apply(lambda x: "/".join(x[1:3]))
cross_compare = df_posts.pivot_table(index='article_path_3', values=['id_post', 'id_article'], aggfunc='nunique')\
.rename(columns={'id_article':'article_count', 'id_post':'post_count'})
cross_compare["posts_per_article"] = (cross_compare.post_count / cross_compare.article_count).astype(int)
cross_compare.sort_values(by=['article_count', 'posts_per_article'], ascending=False, inplace=True)
cross_compare
# %%
font = {'family' : 'normal',
'size' : 16}
matplotlib.rc('font', **font)
cmap = sns.color_palette("light:#EC008E", as_cmap=True)
palette = sns.color_palette("light:#EC008E", as_cmap=False)
d=cross_compare[['article_count', "posts_per_article"]].head(20)
plot = plt.scatter(x=d.index, y=d['posts_per_article'], c=d['posts_per_article'], cmap=cmap)
plt.clf()
plt.colorbar(plot)
ax = sns.barplot(data=d, y=d.index, x=d['article_count'], hue='posts_per_article', palette=palette, dodge=False)
ax.set_ylabel('')
ax.set_xlabel('Number of articles')
ax.legend_.remove()
ax.figure.set_size_inches(12,10)
plt.savefig("./pictures/num_articles_per_category.png", bbox_inches="tight")
# %% [markdown]
# ## Comment-label per path
# %%
df_posts_reduced = df_posts.dropna(how='all', subset=cols_label)[['article_path']+cols_label]
# %%
def yes(x): return np.sum(x == 'yes')
def no(x): return np.sum(x == 'no')
path_vs_label = df_posts_reduced.groupby('article_path').agg([no, yes])
# %%
sel1= [('label_inappropriate', 'yes'), ('label_discriminating', 'yes'), ('label_sentimentnegative', 'yes')]
sel2 = [(c, 'yes') for c in cols_label]
# %% [markdown]
# What are the top 'negative' categories?
# %%
path_vs_label[sel1].sort_values(by=sel1, ascending=False).head(10)
# %%