Exploring Hacker News Posts

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.

Introduction and Getting Data Ready

Here, we will make use of csv module and convert hackernews.csv file into a lists of lists.

  • headers list will contain the column names
  • hn list will cover the whole remaining data
In [1]:
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]
Out[1]:
[['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']]
In [2]:
# Printing Headers
headers
Out[2]:
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']

Extracting Ask HN and Show HN Posts

Using regex, we will search posts that beign with either Ask HN or Show HN posts.
And divide data into three lists:

  • show_posts
  • ask_posts
  • other_posts
In [2]:
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))
1744
1162
17194

Ask HN Posts

In [3]:
ask_posts[:3]
Out[3]:
[['12296411',
  'Ask HN: How to improve my personal website?',
  '',
  '2',
  '6',
  'ahmedbaracat',
  '8/16/2016 9:55'],
 ['10610020',
  'Ask HN: Am I the only one outraged by Twitter shutting down share counts?',
  '',
  '28',
  '29',
  'tkfx',
  '11/22/2015 13:43'],
 ['11610310',
  'Ask HN: Aby recent changes to CSS that broke mobile?',
  '',
  '1',
  '1',
  'polskibus',
  '5/2/2016 10:14']]

Show HN Posts

In [5]:
show_posts[:3]
Out[5]:
[['10627194',
  'Show HN: Wio Link  ESP8266 Based Web of Things Hardware Development Platform',
  'https://iot.seeed.cc',
  '26',
  '22',
  'kfihihc',
  '11/25/2015 14:03'],
 ['10646440',
  'Show HN: Something pointless I made',
  'http://dn.ht/picklecat/',
  '747',
  '102',
  'dhotson',
  '11/29/2015 22:46'],
 ['11590768',
  'Show HN: Shanhu.io, a programming playground powered by e8vm',
  'https://shanhu.io',
  '1',
  '1',
  'h8liu',
  '4/28/2016 18:05']]

Other Posts

In [6]:
other_posts[:3]
Out[6]:
[['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']]

Calculating the Average Number of Comments for Ask HN and Show HN Posts

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.

In [9]:
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)
Avg. Ask Comments:  14.038417431192661
Avg. Show Comments:  10.31669535283993

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.


Finding the Amount of Ask HN Posts and Comments by Hour Created

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:

  • Finding the Amount of Ask HN 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 received by Ask HN posts created every hour.
In [10]:
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

In [12]:
counts_by_hour
Out[12]:
{'09': 45,
 '13': 85,
 '10': 59,
 '14': 107,
 '16': 108,
 '23': 68,
 '12': 73,
 '17': 100,
 '15': 116,
 '21': 109,
 '20': 80,
 '02': 58,
 '18': 109,
 '03': 54,
 '05': 46,
 '19': 110,
 '01': 60,
 '22': 71,
 '08': 48,
 '04': 47,
 '00': 55,
 '06': 44,
 '07': 34,
 '11': 58}

Comments Received on Posts created per hour.

In [11]:
comments_by_hour
Out[11]:
{'09': 251,
 '13': 1253,
 '10': 793,
 '14': 1416,
 '16': 1814,
 '23': 543,
 '12': 687,
 '17': 1146,
 '15': 4477,
 '21': 1745,
 '20': 1722,
 '02': 1381,
 '18': 1439,
 '03': 421,
 '05': 464,
 '19': 1188,
 '01': 683,
 '22': 479,
 '08': 492,
 '04': 337,
 '00': 447,
 '06': 397,
 '07': 267,
 '11': 641}

Calculating the Average Number of Comments for Ask HN Posts by Hour

In [13]:
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

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

Sorting and Printing Values from a List of Lists

  • Printing the Top 5 Hours, during which if post created it will receive most comments.
In [56]:
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]))
Top 5 Hours for 'Ask HN' Comments
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

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.

Conclusion

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.