For this project we're going to explore some Hacker News Data using mostly vanilla python. Hacker News is a reddit like news website that allows users to submit posts, and then vote and comment on them.
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:
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.
Documentation for the dataset can be found here.
opened_file = open('hacker_news.csv')
from csv import reader
read_file = reader(opened_file)
hn = list(read_file)
# extract headers
headers = hn[0]
# reassign list of rows without header row
hn = hn[1:]
print(headers)
id: The unique identifier from Hacker News for the post
title: The title of the post
url: The URL that the posts links to, if it the post has a 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 date and time at which the post was submitted
hn[:5]
First, we'll identify posts that begin with either Ask HN or Show HN and separate the data for those two types of posts into different lists. Separating the data makes it easier to analyze in the following steps.
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row[1]
if title.lower().startswith("ask hn"):
ask_posts.append(row)
elif title.lower().startswith("show hn"):
show_posts.append(row)
else:
other_posts.append(row)
print("# Ask Posts: ", len(ask_posts))
print("# Show Posts: ", len(show_posts))
print("# Other Posts:", len(other_posts))
Now that we separated ask posts and show posts into different lists, we'll calculate the average number of comments each type of post receives.
# calculate average number of Show HN Comments
total_ask_comments = 0
for row in ask_posts:
comments = int(row[4])
total_ask_comments += comments
avg_ask_comments = total_ask_comments / len(ask_posts)
total_show_comments = 0
# calculate average number of Show HN Comments
for row in show_posts:
comments= int(row[4])
total_show_comments += comments
avg_show_comments = total_show_comments / len(show_posts)
print("Avg Ask HN Comments:", avg_ask_comments)
print("Avg Show HN Comments:", avg_show_comments)
On average, ask posts in our sample receive approximately 14 comments, whereas show posts receive approximately 10. Since ask posts are more likely to receive comments, we'll focus our remaining analysis just on these posts.
Next, we'll determine if we can maximize the amount of comments an ask post receives by creating it at a certain time. First, we'll find the amount of ask posts created during each hour of day, along with the number of comments those posts received. Then, we'll calculate the average amount of comments ask posts created at each hour of the day receive.
import datetime as dt
result_list = []
# extract 'created_at' and 'num_comments' columns from ask_posts into new list
for row in ask_posts:
result_list.append([row[6], int(row[4])])
counts_by_hour = {}
comments_by_hour = {}
date_format = "%m/%d/%Y %H:%M"
# calculate how many comments / counts there are per hour, sort into frequency tables
for row in result_list:
date = row[0]
comments = row[1]
hour = dt.datetime.strptime(date, date_format).strftime("%H")
if hour not in counts_by_hour:
counts_by_hour[hour] = 1
comments_by_hour[hour] = comments
else:
counts_by_hour[hour] += 1
comments_by_hour[hour] += comments
comments_by_hour
avg_by_hour = []
for hr in comments_by_hour:
avg_by_hour.append([hr, comments_by_hour[hr] / counts_by_hour[hr]])
avg_by_hour
swap_avg_by_hour = []
for row in avg_by_hour:
one = row[0]
two = row[1]
swap_avg_by_hour.append([two, one])
sorted_swap = sorted(swap_avg_by_hour, reverse=True)
sorted_swap
print("Top 5 Hours for Ask Posts Comments")
for avg, hr in sorted_swap[:5]:
print(
"{}: {: .2f} average comments per post".format(dt.datetime.strptime(hr,"%H").strftime("%H:%M"), avg)
)
The hour that has the most comments per post on average is 15:00 (3:00 pm EST) with an average of 38.59 comments per post. There's roughly a 60% increase from the 2nd highest ranking hour 2:00 (2:00am EST).
According to the documentation, the timezone is for certain EST
In this project, we analyzed ask posts and show 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 post and created between 15:00 and 16:00 (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 posts received more comments on average and ask posts created between 15:00 and 16:00 (3:00 pm est - 4:00 pm est) received the most comments on average.