In this project, we'll compare two different types of posts from Hacker News, a popular site where technology related stories (or 'posts') are voted and commented upon. The two types of posts we'll explore begin with either Ask HN or Show HN.
Users submit Ask HN posts to ask the Hacker News community a specific question, such as "What is the best online course you've ever taken?" Likewise, users submit Show HN posts to show the Hacker News community a project, product, or just generally something interesting.
We'll specifically compare these two types of posts to determine the following:
Do Ask HN or Show HN receive more comments on average?
Do posts created at a certain time receive more comments on average?
It should be noted that the data set we're working with was reduced from almost 300,000 rows to approximately 20,000 rows by removing all submissions that did not receive any comments, and then randomly sampling from the remaining submissions.
Here, we will make use of csv module and convert hackernews.csv file into a lists of lists.
f = open('hacker_news.csv')
from csv import reader
read_file = reader(f)
data = list(read_file)
headers = data[0]
hn = data[1:]
# Printing Data
hn[:5]
# Printing Headers
headers
Using regex, we will search posts that beign with either Ask HN or Show HN posts.
And divide data into three lists:
import re
patterna = r"^Ask HN"
patterns = r"^Show HN"
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row[1]
match1 = re.search(patterna, title, re.I)
match2 = re.search(patterns, title, re.I)
if match1:
ask_posts.append(row)
elif match2:
show_posts.append(row)
else:
other_posts.append(row)
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
Ask HN Posts
ask_posts[:3]
Show HN Posts
show_posts[:3]
Other Posts
other_posts[:3]
Now that we separated Ask HN and Show HN posts into different lists, we'll calculate the Average Number of Comments each type of post receives.
ask_comments = [int(i[4]) for i in ask_posts]
show_comments = [int(i[4]) for i in show_posts]
avg_ask_comments = sum(ask_comments)/len(ask_comments)
avg_show_comments = sum(show_comments)/len(show_comments)
print('Avg. Ask Comments: ', avg_ask_comments)
print('Avg. Show Comments: ', avg_show_comments)
On Average, the Ask HN posts received 40% comments more then the Show HN posts.
We will continue our further analysis, using the Ask HN posts only.
Next, we'll determine if we can maximize the amount of comments an Ask HN post receives by creating it at a certain time.
We'll do this by doing the following:
import datetime as dt
created_at = []
for row in ask_posts:
created_at.append(row[6])
result_list = list(zip(ask_comments, created_at))
counts_by_hour = {}
comments_by_hour = {}
for row in result_list:
date = dt.datetime.strptime(row[1], '%m/%d/%Y %H:%M')
hour = date.strftime("%H")
if hour in counts_by_hour:
counts_by_hour[hour] += 1
comments_by_hour[hour] += row[0]
else:
counts_by_hour[hour] = 1
comments_by_hour[hour] = row[0]
Posts created per hour
counts_by_hour
Comments Received on Posts created per hour.
comments_by_hour
avg_by_hour = [[i, comments_by_hour[i]/counts_by_hour[i]] for i in counts_by_hour]
Average Comments for Ask HN Posts by Hour
avg_by_hour
avg_by_hour = sorted(avg_by_hour, key=lambda x: x[1], reverse=True)
print("Top 5 Hours for 'Ask HN' Comments")
for row in avg_by_hour[:5]:
time = dt.datetime.strptime(row[0], "%H")
print("{}: {:.2f} average comments per post".format(time.strftime("%H:%M"), row[1]))
The hour that receives the most comments per post on average is 15:00, with an average of 38.59 comments per post. There's about a 60% increase in the number of comments between the hours with the highest and second highest average number of comments.
According to the data set documentation, the timezone used is Eastern Time in the US.
So, we (in UAE) should post at 00:00 AM GST.
In this project, we analyzed Ask HN and Show HN posts to determine which type of post and time receive the most comments on average. Based on our analysis, to maximize the amount of comments a post receives, we'd recommend the post be categorized as Ask HN and created between 00:00 and 01:00 GST (3:00 pm EST - 4:00 pm EST).
However, it should be noted that the data set we analyzed excluded posts without any comments. Given that, it's more accurate to say that of the posts that received comments, Ask HN posts received more comments on average and Ask HN posts created between 00:00 and 01:00 GST received the most comments on average.