We are going to analyse comment response in Hacker News data set specifically on 'Ask HN'and 'Show HN'postings. We want to know which one receive more comments on average. We also want to know whether posts created on certain time receive more comments on average? To do this, we will download the data sets from here. The data set has been reduced from almost 300,000 rows to approximately 20,000 rows by removing all postings that did not receive any comments and then randomly sampling from the remaining submmissions.
The descriptions of the columns are as follow:
* id: The unique identifier from HN for the post
* title: The title of the post
* url : The URL that the posts link to, if the post has the URL
* num_points : The number of points the post acquired, calculated as the total number of upvotes minus the total number of downvotes
* num_comments: The number of comments that were made on the post
* author : The username of the person who submitted the post
* created_at: The data and time at which the post was submitted
opened_file = open('hacker_news.csv')
from csv import reader
read_file = reader(opened_file)
hn = list(read_file)
headers = hn[0]
hn = hn[1:]
# separating headers and the rest of the rows
print(headers)
hn[:5]
First thing to do, is to separate the postings that we are going to analyse from the rest of the data sets. To do that we are going to create three empty lists called: ask_posts, show_posts and other_post.
We will calculate the length of posting that start with Ask HN, Show HN and Others.
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row[1]
title = title.lower() # the output will be in lowercase
if title.startswith('ask hn'):
ask_posts.append (row)
elif title.startswith('show hn'):
show_posts.append(row)
else:
other_posts.append(row)
print('Number of posts that start with ask hn:', len(ask_posts))
print('Number of posts that start with show hn:',len(show_posts))
print('Number of post that start with other:', len(other_posts))
Let's see the first five rows of the ask_posts list of lists:
ask_posts[:5]
And we can also see the first five rows of show_posts list of lists as follow:
show_posts[:5]
Now we will determine between ask posts and show posts which one receive more comments on average.
total_ask_comments = 0
for row in ask_posts:
num_comments = int(row[4])
total_ask_comments += num_comments
avg_ask_comments = total_ask_comments/len(ask_posts)
print(avg_ask_comments)
There is 14.0384 average number of comments for Ask HN postings. Let's check the show_posts list of lists:
total_show_comments = 0
for row in show_posts:
num_comments = int(row[4])
total_show_comments += num_comments
avg_show_comments = total_show_comments/len(show_posts)
print(avg_show_comments)
So, there is 10.3167 average number of comments for Show HN postings. Hence, it appears that the Ask HN receive more comments than the Show HN.
Knowing ask posts receives more comments, we will be focusing our analysis on ask posts and at this time our attention would be centered on the timing when ask posts is created and how many comments is generated at each time it is created. To do this, we will do the following steps:
1. Calculate the number of ask posts created at each hour of the days
along with the number of comments received
2. Calculate the average number of comments ask posts receive by hour created.
import datetime as dt # importing the datetime module
result_list = []
for row in ask_posts:
created_at = row[6]
comments_at = int(row[4])
result_list.append(created_at)
result_list.append(comments_at)
print(result_list[:1])
counts_by_hour = {}
comments_by_hour = {}
date_format = '%m/%d/%Y %H:%M'
for value in result_list:
date = value[0]
comment = value[1]
time = dt.datetime.strptime(creation_date, date_format).strftime('%H')
if time in counts_by_hour:
comments_by_hour[time] += comment
counts_by_hour[time] += 1
else:
comments_by_hour[time] = comment
coounts_by_hour[time] = 1
print(comments_by_hour)
print(counts_by_hour)