Exploring Hacker News Posts

We're specifically interested in posts whose titles begin with either Ask HN or Show HN. Users submit Ask HN posts to ask the Hacker News community a specific question. Below are a couple examples:

code
Ask HN: How to improve my personal website?
Ask HN: Am I the only one outraged by Twitter shutting down share counts?
Ask HN: Aby recent changes to CSS that broke mobile?

Likewise, users submit Show HN posts to show the Hacker News community a project, product, or just generally something interesting. Below are a couple of examples:

code
Show HN: Wio Link  ESP8266 Based Web of Things Hardware Development Platform'
Show HN: Something pointless I made
Show HN: Shanhu.io, a programming playground powered by e8vm

We'll 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?

Let's start by importing the libraries we need and reading the data set into a list of lists

In [1]:
from csv import reader

file = open('hacker_news.csv')
read = reader(file)
hn = list(read)

print(hn[:5])
[['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at'], ['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01']]
In [2]:
headers = hn[0]

hn = hn[1:]

print(headers)
print(hn[:5])
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']
[['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01'], ['10301696', 'Note by Note: The Making of Steinway L1037 (2007)', 'http://www.nytimes.com/2007/11/07/movies/07stein.html?_r=0', '8', '2', 'walterbell', '9/30/2015 4:12']]

Filtering Posts by Type

In [3]:
ask_posts = []
show_posts = []
other_posts = []

for post in hn:
    title = post[1]
    if title.lower().startswith('ask hn'):
        ask_posts.append(post)
    elif title.lower().startswith('show hn'):
        show_posts.append(post)
    else:
        other_posts.append(post)

print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
1744
1162
17194

Counting Comments per Type

In [5]:
# Calculating Average comments for Ask Post Type
total_ask_comments = 0
for post in ask_posts:
    num_comments = int(post[4])
    total_ask_comments += num_comments

avg_ask_comments = total_ask_comments / len(ask_posts)
print('Average Comments in Ask HN post Type: {:,.2f}'.format(avg_ask_comments))

# Calculating Average comments for Show Post Type
total_show_comments = 0
for post in show_posts:
    num_comments = int(post[4])
    total_show_comments += num_comments

avg_show_comments = total_show_comments / len(show_posts)
print('Average Comments in Show HN post Type: {:,.2f}'.format(avg_show_comments))
Average Comments in Ask HN post Type: 14.04
Average Comments in Show HN post Type: 10.32

As we can see by the above results, the Ask HN posts receives average more comments than Show HN posts, the one reason may be is that, because in Ask HN post, author was asking for some advice from the HN community.

Calculating Amount of Ask Posts created daily per hour with comments received

In [12]:
import datetime as dt

result_list = []

for post in ask_posts:
    created_at = post[6]
    num_comments = int(post[4])
    result_list.append([created_at, num_comments])

counts_by_hour = {}
comments_by_hour = {}

for row in result_list:
    post_created_at = dt.datetime.strptime(row[0], '%m/%d/%Y %H:%M')
    hour = post_created_at.strftime('%H') 
    
    if hour in counts_by_hour:
        counts_by_hour[hour] += 1
        comments_by_hour[hour] += row[1]
    else:
        counts_by_hour[hour] = 1
        comments_by_hour[hour] = row[1]

print(counts_by_hour)
print(comments_by_hour)
{'20': 80, '19': 110, '12': 73, '23': 68, '04': 47, '22': 71, '05': 46, '07': 34, '06': 44, '15': 116, '13': 85, '00': 55, '02': 58, '11': 58, '08': 48, '17': 100, '14': 107, '01': 60, '21': 109, '16': 108, '18': 109, '09': 45, '10': 59, '03': 54}
{'20': 1722, '19': 1188, '12': 687, '23': 543, '04': 337, '22': 479, '05': 464, '07': 267, '06': 397, '15': 4477, '13': 1253, '00': 447, '02': 1381, '11': 641, '08': 492, '17': 1146, '14': 1416, '01': 683, '21': 1745, '16': 1814, '18': 1439, '09': 251, '10': 793, '03': 421}

Calculate the average number of comments for posts created during each hour of the day

In [19]:
avg_by_hour = []
for hour in counts_by_hour:
    comments_avg_per_post = comments_by_hour[hour]/counts_by_hour[hour]
    avg_by_hour.append([hour, comments_avg_per_post])

print(avg_by_hour)
[['20', 21.525], ['19', 10.8], ['12', 9.41095890410959], ['23', 7.985294117647059], ['04', 7.170212765957447], ['22', 6.746478873239437], ['05', 10.08695652173913], ['07', 7.852941176470588], ['06', 9.022727272727273], ['15', 38.5948275862069], ['13', 14.741176470588234], ['00', 8.127272727272727], ['02', 23.810344827586206], ['11', 11.051724137931034], ['08', 10.25], ['17', 11.46], ['14', 13.233644859813085], ['01', 11.383333333333333], ['21', 16.009174311926607], ['16', 16.796296296296298], ['18', 13.20183486238532], ['09', 5.5777777777777775], ['10', 13.440677966101696], ['03', 7.796296296296297]]

Sort the Result

In [21]:
swap_avg_by_hour = []
for row in avg_by_hour:
    swap_avg_by_hour.append([row[1], row[0]])

print(swap_avg_by_hour)
sorted_swap = sorted(swap_avg_by_hour, reverse=True)
[[21.525, '20'], [10.8, '19'], [9.41095890410959, '12'], [7.985294117647059, '23'], [7.170212765957447, '04'], [6.746478873239437, '22'], [10.08695652173913, '05'], [7.852941176470588, '07'], [9.022727272727273, '06'], [38.5948275862069, '15'], [14.741176470588234, '13'], [8.127272727272727, '00'], [23.810344827586206, '02'], [11.051724137931034, '11'], [10.25, '08'], [11.46, '17'], [13.233644859813085, '14'], [11.383333333333333, '01'], [16.009174311926607, '21'], [16.796296296296298, '16'], [13.20183486238532, '18'], [5.5777777777777775, '09'], [13.440677966101696, '10'], [7.796296296296297, '03']]

Top 5 Hours for Ask Post Comments

In [29]:
for row in sorted_swap[:5]:
    print('{}:00: {:.2f} average comments per post'.format(row[1], row[0]))
15:00: 38.59 average comments per post
02:00: 23.81 average comments per post
20:00: 21.52 average comments per post
16:00: 16.80 average comments per post
21:00: 16.01 average comments per post

Conclusion

As we can see in the above results, if we want to get the most number of comments or response on over post we should post around 3:00 PM.

In [ ]: