|
@@ -142,18 +142,15 @@
|
|
|
],
|
|
|
"source": [
|
|
|
"# Identify posts that begin with either `Ask HN` or `Show HN` and separate the data into different lists.\n",
|
|
|
- "import re\n",
|
|
|
"ask_posts = []\n",
|
|
|
"show_posts =[]\n",
|
|
|
"other_posts = []\n",
|
|
|
"\n",
|
|
|
"for post in hn:\n",
|
|
|
" title = post[1]\n",
|
|
|
- " match1 = re.search(r\"^Ask HN\", title, re.I)\n",
|
|
|
- " match2 = re.search(r\"^Show HN\", title, re.I)\n",
|
|
|
- " if match1:\n",
|
|
|
+ " if title.lower().startswith(\"ask hn\"):\n",
|
|
|
" ask_posts.append(post)\n",
|
|
|
- " elif match2:\n",
|
|
|
+ " elif title.lower().startswith(\"show hn\"):\n",
|
|
|
" show_posts.append(post)\n",
|
|
|
" else:\n",
|
|
|
" other_posts.append(post)\n",
|
|
@@ -169,7 +166,7 @@
|
|
|
"source": [
|
|
|
"# Calculating the Average Number of Comments for Ask HN and Show HN Posts\n",
|
|
|
"\n",
|
|
|
- "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."
|
|
|
+ "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."
|
|
|
]
|
|
|
},
|
|
|
{
|
|
@@ -187,8 +184,12 @@
|
|
|
],
|
|
|
"source": [
|
|
|
"# Calculate the average number of comments `Ask HN` posts receive.\n",
|
|
|
- "ask_comments = [int(post[4]) for post in ask_posts]\n",
|
|
|
- "avg_ask_comments = sum(ask_comments) / len(ask_posts)\n",
|
|
|
+ "total_ask_comments = 0\n",
|
|
|
+ "\n",
|
|
|
+ "for post in ask_posts:\n",
|
|
|
+ " total_ask_comments += int(post[4])\n",
|
|
|
+ " \n",
|
|
|
+ "avg_ask_comments = total_ask_comments / len(ask_posts)\n",
|
|
|
"print(avg_ask_comments)"
|
|
|
]
|
|
|
},
|
|
@@ -206,9 +207,12 @@
|
|
|
}
|
|
|
],
|
|
|
"source": [
|
|
|
- "# Calculate the average number of comments `Show HN` posts receive.\n",
|
|
|
- "show_comments = [int(post[4]) for post in show_posts]\n",
|
|
|
- "avg_show_comments = sum(show_comments) / len(show_posts)\n",
|
|
|
+ "total_show_comments = 0\n",
|
|
|
+ "\n",
|
|
|
+ "for post in show_posts:\n",
|
|
|
+ " total_show_comments += int(post[4])\n",
|
|
|
+ " \n",
|
|
|
+ "avg_show_comments = total_show_comments / len(show_posts)\n",
|
|
|
"print(avg_show_comments)"
|
|
|
]
|
|
|
},
|
|
@@ -216,16 +220,16 @@
|
|
|
"cell_type": "markdown",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
- "On average, `Ask HN` posts in our sample receive approximately 14 comments, whereas `Show HN` posts receive approximately 10. Since `Ask HN` posts are more likely to receive comments, we'll focus our remaining analysis just on these posts. "
|
|
|
+ "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. "
|
|
|
]
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
- "# Finding the Amount of Ask HN Posts and Comments by Hour Created\n",
|
|
|
+ "# Finding the Amount of Ask Posts and Comments by Hour Created\n",
|
|
|
"\n",
|
|
|
- "Next, we'll determine if we can maximize the amount of comments an `Ask HN` post receives by creating it at a certain time. First, we'll find 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 `Ask HN` posts created at each hour of the day receive."
|
|
|
+ "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."
|
|
|
]
|
|
|
},
|
|
|
{
|
|
@@ -268,19 +272,23 @@
|
|
|
}
|
|
|
],
|
|
|
"source": [
|
|
|
- "# Calculate the amount of `Ask HN` posts created during each hour of day and the number of comments received.\n",
|
|
|
+ "# Calculate the amount of ask posts created during each hour of day and the number of comments received.\n",
|
|
|
"import datetime as dt\n",
|
|
|
"\n",
|
|
|
- "ask_comments = [int(post[4]) for post in ask_posts]\n",
|
|
|
- "created_date = [post[6] for post in ask_posts]\n",
|
|
|
+ "result_list = []\n",
|
|
|
"\n",
|
|
|
- "result_list = zip(created_date, ask_comments)\n",
|
|
|
+ "for post in ask_posts:\n",
|
|
|
+ " result_list.append(\n",
|
|
|
+ " [post[6], int(post[4])]\n",
|
|
|
+ " )\n",
|
|
|
"\n",
|
|
|
"comments_by_hour = {}\n",
|
|
|
"counts_by_hour = {}\n",
|
|
|
"date_format = \"%m/%d/%Y %H:%M\"\n",
|
|
|
"\n",
|
|
|
- "for date, comment in result_list:\n",
|
|
|
+ "for each_row in result_list:\n",
|
|
|
+ " date = each_row[0]\n",
|
|
|
+ " comment = each_row[1]\n",
|
|
|
" time = dt.datetime.strptime(date, date_format).strftime(\"%H\")\n",
|
|
|
" if time in counts_by_hour:\n",
|
|
|
" comments_by_hour[time] += comment\n",
|
|
@@ -340,7 +348,11 @@
|
|
|
],
|
|
|
"source": [
|
|
|
"# Calculate the average amount of comments `Ask HN` posts created at each hour of the day receive.\n",
|
|
|
- "avg_by_hour = [[hr, comments_by_hour[hr] / counts_by_hour[hr]] for hr in comments_by_hour]\n",
|
|
|
+ "avg_by_hour = []\n",
|
|
|
+ "\n",
|
|
|
+ "for hr in comments_by_hour:\n",
|
|
|
+ " avg_by_hour.append([hr, comments_by_hour[hr] / counts_by_hour[hr]])\n",
|
|
|
+ "\n",
|
|
|
"avg_by_hour"
|
|
|
]
|
|
|
},
|
|
@@ -355,6 +367,65 @@
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 8,
|
|
|
"metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "[[5.5777777777777775, '09'], [14.741176470588234, '13'], [13.440677966101696, '10'], [13.233644859813085, '14'], [16.796296296296298, '16'], [7.985294117647059, '23'], [9.41095890410959, '12'], [11.46, '17'], [38.5948275862069, '15'], [16.009174311926607, '21'], [21.525, '20'], [23.810344827586206, '02'], [13.20183486238532, '18'], [7.796296296296297, '03'], [10.08695652173913, '05'], [10.8, '19'], [11.383333333333333, '01'], [6.746478873239437, '22'], [10.25, '08'], [7.170212765957447, '04'], [8.127272727272727, '00'], [9.022727272727273, '06'], [7.852941176470588, '07'], [11.051724137931034, '11']]\n"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "data": {
|
|
|
+ "text/plain": [
|
|
|
+ "[[38.5948275862069, '15'],\n",
|
|
|
+ " [23.810344827586206, '02'],\n",
|
|
|
+ " [21.525, '20'],\n",
|
|
|
+ " [16.796296296296298, '16'],\n",
|
|
|
+ " [16.009174311926607, '21'],\n",
|
|
|
+ " [14.741176470588234, '13'],\n",
|
|
|
+ " [13.440677966101696, '10'],\n",
|
|
|
+ " [13.233644859813085, '14'],\n",
|
|
|
+ " [13.20183486238532, '18'],\n",
|
|
|
+ " [11.46, '17'],\n",
|
|
|
+ " [11.383333333333333, '01'],\n",
|
|
|
+ " [11.051724137931034, '11'],\n",
|
|
|
+ " [10.8, '19'],\n",
|
|
|
+ " [10.25, '08'],\n",
|
|
|
+ " [10.08695652173913, '05'],\n",
|
|
|
+ " [9.41095890410959, '12'],\n",
|
|
|
+ " [9.022727272727273, '06'],\n",
|
|
|
+ " [8.127272727272727, '00'],\n",
|
|
|
+ " [7.985294117647059, '23'],\n",
|
|
|
+ " [7.852941176470588, '07'],\n",
|
|
|
+ " [7.796296296296297, '03'],\n",
|
|
|
+ " [7.170212765957447, '04'],\n",
|
|
|
+ " [6.746478873239437, '22'],\n",
|
|
|
+ " [5.5777777777777775, '09']]"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ "execution_count": 8,
|
|
|
+ "metadata": {},
|
|
|
+ "output_type": "execute_result"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "swap_avg_by_hour = []\n",
|
|
|
+ "\n",
|
|
|
+ "for row in avg_by_hour:\n",
|
|
|
+ " swap_avg_by_hour.append([row[1], row[0]])\n",
|
|
|
+ " \n",
|
|
|
+ "print(swap_avg_by_hour)\n",
|
|
|
+ "\n",
|
|
|
+ "sorted_swap = sorted(swap_avg_by_hour, reverse=True)\n",
|
|
|
+ "\n",
|
|
|
+ "sorted_swap"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 9,
|
|
|
+ "metadata": {},
|
|
|
"outputs": [
|
|
|
{
|
|
|
"name": "stdout",
|
|
@@ -371,11 +442,14 @@
|
|
|
],
|
|
|
"source": [
|
|
|
"# Sort the values and print the the 5 hours with the highest average comments.\n",
|
|
|
- "avg_by_hour = sorted(avg_by_hour, key=lambda result: result[1], reverse=True)\n",
|
|
|
"\n",
|
|
|
"print(\"Top 5 Hours for 'Ask HN' Comments\")\n",
|
|
|
- "for hr, avg in avg_by_hour[:5]:\n",
|
|
|
- " print(\"{}: {:.2f} average comments per post\".format(dt.datetime.strptime(hr, \"%H\").strftime(\"%H:%M\"),avg))"
|
|
|
+ "for avg, hr in sorted_swap[:5]:\n",
|
|
|
+ " print(\n",
|
|
|
+ " \"{}: {:.2f} average comments per post\".format(\n",
|
|
|
+ " dt.datetime.strptime(hr, \"%H\").strftime(\"%H:%M\"),avg\n",
|
|
|
+ " )\n",
|
|
|
+ " )"
|
|
|
]
|
|
|
},
|
|
|
{
|
|
@@ -394,17 +468,10 @@
|
|
|
"source": [
|
|
|
"# Conclusion\n",
|
|
|
"\n",
|
|
|
- "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 15:00 and 16:00 (3:00 pm est - 4:00 pm est). \n",
|
|
|
+ "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). \n",
|
|
|
"\n",
|
|
|
- "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 15:00 and 16:00 (3:00 pm est - 4:00 pm est) received the most comments on average. "
|
|
|
+ "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. "
|
|
|
]
|
|
|
- },
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
- "metadata": {},
|
|
|
- "outputs": [],
|
|
|
- "source": []
|
|
|
}
|
|
|
],
|
|
|
"metadata": {
|
|
@@ -423,7 +490,7 @@
|
|
|
"name": "python",
|
|
|
"nbconvert_exporter": "python",
|
|
|
"pygments_lexer": "ipython3",
|
|
|
- "version": "3.6.6"
|
|
|
+ "version": "3.7.0"
|
|
|
}
|
|
|
},
|
|
|
"nbformat": 4,
|