#!/usr/bin/env python # coding: utf-8 # # *Exploring Hacker News Posts* # # In this project, we'll compare two different types of posts from [Hacker News](news.ycombinator.com), 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](https://www.kaggle.com/hacker-news/hacker-news-posts) 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] # In[2]: # Printing Headers headers # ### 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)) # ***Ask HN Posts*** # In[3]: ask_posts[:3] # ***Show HN Posts*** # In[5]: show_posts[:3] # ***Other Posts*** # In[6]: other_posts[:3] # ### 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) # 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 # ***Comments Received on Posts created per hour.*** # In[11]: comments_by_hour # ### 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 # ### 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])) # 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](https://www.kaggle.com/hacker-news/hacker-news-posts/home), 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.