Analysing Comment Responses in Hacker News (HN)

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

['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']
  'Interactive Dynamic Video',
  '8/4/2016 11:52'],
  'How to Use Open Source and Shut the Fuck Up at the Same Time',
  '1/26/2016 19:30'],
  "Florida DJs May Face Felony for April Fools' Water Joke",
  '6/23/2016 22:20'],
  'Technology ventures: From Idea to Enterprise',
  '6/17/2016 0:01'],
  'Note by Note: The Making of Steinway L1037 (2007)',
  '9/30/2015 4:12']]

Separating 'Ask HN' and 'Show HN' lists from the rest of the Data Sets

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.

In [18]:
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'):
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))
Number of posts that start with ask hn: 1744
Number of posts that start with show hn: 1162
Number of post that start with other: 17194

Let's see the first five rows of the ask_posts list of lists:

In [19]:
  'Ask HN: How to improve my personal website?',
  '8/16/2016 9:55'],
  'Ask HN: Am I the only one outraged by Twitter shutting down share counts?',
  '11/22/2015 13:43'],
  'Ask HN: Aby recent changes to CSS that broke mobile?',
  '5/2/2016 10:14'],
  'Ask HN: Looking for Employee #3 How do I do it?',
  '8/2/2016 14:20'],
  'Ask HN: Someone offered to buy my browser extension from me. What now?',
  '10/15/2015 16:38']]

And we can also see the first five rows of show_posts list of lists as follow:

In [20]:
  'Show HN: Wio Link  ESP8266 Based Web of Things Hardware Development Platform',
  '11/25/2015 14:03'],
  'Show HN: Something pointless I made',
  '11/29/2015 22:46'],
  'Show HN:, a programming playground powered by e8vm',
  '4/28/2016 18:05'],
  'Show HN: Webscope  Easy way for web developers to communicate with Clients',
  '7/28/2016 7:11'],
  'Show HN: GeoScreenshot  Easily test Geo-IP based web pages',
  '1/9/2016 20:45']]

Now we will determine between ask posts and show posts which one receive more comments on average.

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

There is 14.0384 average number of comments for Ask HN postings. Let's check the show_posts list of lists:

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

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.
In [24]:
import datetime as dt # importing the datetime module
result_list = []
for row in ask_posts:
    created_at = row[6]
    comments_at = int(row[4])

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
        comments_by_hour[time] = comment
        coounts_by_hour[time] = 1
['8/16/2016 9:55']
ValueErrorTraceback (most recent call last)
<ipython-input-24-7d69c3b9ef60> in <module>()
     14     date = value[0]
     15     comment = value[1]
---> 16     time = dt.datetime.strptime(creation_date, date_format).strftime('%H')
     17     if time in counts_by_hour:
     18         comments_by_hour[time] += comment

/usr/lib/python3.4/ in _strptime_datetime(cls, data_string, format)
    498     """Return a class cls instance based on the input string and the
    499     format string."""
--> 500     tt, fraction = _strptime(data_string, format)
    501     tzname, gmtoff = tt[-2:]
    502     args = tt[:6] + (fraction,)

/usr/lib/python3.4/ in _strptime(data_string, format)
    335     if not found:
    336         raise ValueError("time data %r does not match format %r" %
--> 337                          (data_string, format))
    338     if len(data_string) != found.end():
    339         raise ValueError("unconverted data remains: %s" %

ValueError: time data '8' does not match format '%m/%d/%Y %H:%M'