Guest User

Untitled

a guest
Oct 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.36 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Analyzing Gun Deaths in the US"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "## This dataset from FiveThirtyEight contains information on gun deaths in the US from 2012 to 2014."
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {},
  20. "source": [
  21. "### Introducing US Gun Death Data"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 25,
  27. "metadata": {
  28. "collapsed": false
  29. },
  30. "outputs": [],
  31. "source": [
  32. "import csv\n",
  33. "f = open(\"guns.csv\", 'r')\n",
  34. "data = list(csv.reader(f))"
  35. ]
  36. },
  37. {
  38. "cell_type": "markdown",
  39. "metadata": {},
  40. "source": [
  41. "### Eliminate header from data set"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": 26,
  47. "metadata": {
  48. "collapsed": false
  49. },
  50. "outputs": [
  51. {
  52. "name": "stdout",
  53. "output_type": "stream",
  54. "text": [
  55. "[['', 'year', 'month', 'intent', 'police', 'sex', 'age', 'race', 'hispanic', 'place', 'education']]\n",
  56. "[['1', '2012', '01', 'Suicide', '0', 'M', '34', 'Asian/Pacific Islander', '100', 'Home', '4'], ['2', '2012', '01', 'Suicide', '0', 'F', '21', 'White', '100', 'Street', '3'], ['3', '2012', '01', 'Suicide', '0', 'M', '60', 'White', '100', 'Other specified', '4'], ['4', '2012', '02', 'Suicide', '0', 'M', '64', 'White', '100', 'Home', '4'], ['5', '2012', '02', 'Suicide', '0', 'M', '31', 'White', '100', 'Other specified', '2']]\n"
  57. ]
  58. }
  59. ],
  60. "source": [
  61. "headers = data[:1]\n",
  62. "data = data[1:]\n",
  63. "print(headers)\n",
  64. "print(data[:5])"
  65. ]
  66. },
  67. {
  68. "cell_type": "markdown",
  69. "metadata": {},
  70. "source": [
  71. "### Counting gun deaths by year"
  72. ]
  73. },
  74. {
  75. "cell_type": "code",
  76. "execution_count": 27,
  77. "metadata": {
  78. "collapsed": false,
  79. "scrolled": true
  80. },
  81. "outputs": [
  82. {
  83. "data": {
  84. "text/plain": [
  85. "{'2012': 33562, '2013': 33635, '2014': 33598}"
  86. ]
  87. },
  88. "execution_count": 27,
  89. "metadata": {},
  90. "output_type": "execute_result"
  91. }
  92. ],
  93. "source": [
  94. "years = [row[1] for row in data]\n",
  95. "year_counts = {}\n",
  96. "for year in years:\n",
  97. " if year not in year_counts:\n",
  98. " year_counts[year] = 0\n",
  99. " else:\n",
  100. " year_counts[year] += 1\n",
  101. "year_counts\n"
  102. ]
  103. },
  104. {
  105. "cell_type": "markdown",
  106. "metadata": {},
  107. "source": [
  108. "Looks like there is not much change between years. We can check if gun deaths change in the US by year and month.\n",
  109. "\n",
  110. "### Counting Gun Deaths by Year and Month"
  111. ]
  112. },
  113. {
  114. "cell_type": "code",
  115. "execution_count": 28,
  116. "metadata": {
  117. "collapsed": false,
  118. "scrolled": true
  119. },
  120. "outputs": [
  121. {
  122. "data": {
  123. "text/plain": [
  124. "{datetime.datetime(2012, 1, 1, 0, 0): 2758,\n",
  125. " datetime.datetime(2012, 2, 1, 0, 0): 2357,\n",
  126. " datetime.datetime(2012, 3, 1, 0, 0): 2743,\n",
  127. " datetime.datetime(2012, 4, 1, 0, 0): 2795,\n",
  128. " datetime.datetime(2012, 5, 1, 0, 0): 2999,\n",
  129. " datetime.datetime(2012, 6, 1, 0, 0): 2826,\n",
  130. " datetime.datetime(2012, 7, 1, 0, 0): 3026,\n",
  131. " datetime.datetime(2012, 8, 1, 0, 0): 2954,\n",
  132. " datetime.datetime(2012, 9, 1, 0, 0): 2852,\n",
  133. " datetime.datetime(2012, 10, 1, 0, 0): 2733,\n",
  134. " datetime.datetime(2012, 11, 1, 0, 0): 2729,\n",
  135. " datetime.datetime(2012, 12, 1, 0, 0): 2791,\n",
  136. " datetime.datetime(2013, 1, 1, 0, 0): 2864,\n",
  137. " datetime.datetime(2013, 2, 1, 0, 0): 2375,\n",
  138. " datetime.datetime(2013, 3, 1, 0, 0): 2862,\n",
  139. " datetime.datetime(2013, 4, 1, 0, 0): 2798,\n",
  140. " datetime.datetime(2013, 5, 1, 0, 0): 2806,\n",
  141. " datetime.datetime(2013, 6, 1, 0, 0): 2920,\n",
  142. " datetime.datetime(2013, 7, 1, 0, 0): 3079,\n",
  143. " datetime.datetime(2013, 8, 1, 0, 0): 2859,\n",
  144. " datetime.datetime(2013, 9, 1, 0, 0): 2742,\n",
  145. " datetime.datetime(2013, 10, 1, 0, 0): 2808,\n",
  146. " datetime.datetime(2013, 11, 1, 0, 0): 2758,\n",
  147. " datetime.datetime(2013, 12, 1, 0, 0): 2765,\n",
  148. " datetime.datetime(2014, 1, 1, 0, 0): 2651,\n",
  149. " datetime.datetime(2014, 2, 1, 0, 0): 2361,\n",
  150. " datetime.datetime(2014, 3, 1, 0, 0): 2684,\n",
  151. " datetime.datetime(2014, 4, 1, 0, 0): 2862,\n",
  152. " datetime.datetime(2014, 5, 1, 0, 0): 2864,\n",
  153. " datetime.datetime(2014, 6, 1, 0, 0): 2931,\n",
  154. " datetime.datetime(2014, 7, 1, 0, 0): 2884,\n",
  155. " datetime.datetime(2014, 8, 1, 0, 0): 2970,\n",
  156. " datetime.datetime(2014, 9, 1, 0, 0): 2914,\n",
  157. " datetime.datetime(2014, 10, 1, 0, 0): 2865,\n",
  158. " datetime.datetime(2014, 11, 1, 0, 0): 2756,\n",
  159. " datetime.datetime(2014, 12, 1, 0, 0): 2857}"
  160. ]
  161. },
  162. "execution_count": 28,
  163. "metadata": {},
  164. "output_type": "execute_result"
  165. }
  166. ],
  167. "source": [
  168. "import datetime\n",
  169. "dates = [datetime.datetime(year=int(row[1]), month=int(row[2]), day=1) for row in data]\n",
  170. "date_counts = {}\n",
  171. "for date in dates:\n",
  172. " if date not in date_counts:\n",
  173. " date_counts[date] = 0\n",
  174. " date_counts[date] += 1\n",
  175. "date_counts"
  176. ]
  177. },
  178. {
  179. "cell_type": "markdown",
  180. "metadata": {},
  181. "source": [
  182. "### Exploring How Gun Deaths Vary by Sex and Race"
  183. ]
  184. },
  185. {
  186. "cell_type": "code",
  187. "execution_count": 29,
  188. "metadata": {
  189. "collapsed": false
  190. },
  191. "outputs": [
  192. {
  193. "data": {
  194. "text/plain": [
  195. "{'F': 14449, 'M': 86349}"
  196. ]
  197. },
  198. "execution_count": 29,
  199. "metadata": {},
  200. "output_type": "execute_result"
  201. }
  202. ],
  203. "source": [
  204. "sex = [row[5] for row in data]\n",
  205. "sex_counts = {}\n",
  206. "for gender in sex:\n",
  207. " if gender not in sex_counts:\n",
  208. " sex_counts[gender] = 0\n",
  209. " sex_counts[gender] += 1\n",
  210. "sex_counts"
  211. ]
  212. },
  213. {
  214. "cell_type": "code",
  215. "execution_count": 30,
  216. "metadata": {
  217. "collapsed": false,
  218. "scrolled": true
  219. },
  220. "outputs": [
  221. {
  222. "data": {
  223. "text/plain": [
  224. "{'Asian/Pacific Islander': 1326,\n",
  225. " 'Black': 23296,\n",
  226. " 'Hispanic': 9022,\n",
  227. " 'Native American/Native Alaskan': 917,\n",
  228. " 'White': 66237}"
  229. ]
  230. },
  231. "execution_count": 30,
  232. "metadata": {},
  233. "output_type": "execute_result"
  234. }
  235. ],
  236. "source": [
  237. "races = [row[7] for row in data]\n",
  238. "race_counts = {}\n",
  239. "for race_classification in races:\n",
  240. " if race_classification not in race_counts:\n",
  241. " race_counts[race_classification] = 0\n",
  242. " race_counts[race_classification] += 1\n",
  243. "race_counts"
  244. ]
  245. },
  246. {
  247. "cell_type": "markdown",
  248. "metadata": {},
  249. "source": [
  250. "## Findings\n",
  251. "\n",
  252. "The data shows that there were more gun deaths for males than females but it is surprising to see the vast difference in numbers. \n",
  253. "\n",
  254. "Looking at gun deaths by year and month shows that there is some seasonal correlation with deaths increasing in the summer and decreasing in the winter.\n",
  255. "\n",
  256. "Also, we can check to see if the total deaths by race coincides with the proportion of each race. We can get census data to see what percentage the US population falls into each race.\n",
  257. "\n",
  258. "### How Do Total Deaths by Race Compare to the US Population?"
  259. ]
  260. },
  261. {
  262. "cell_type": "code",
  263. "execution_count": 31,
  264. "metadata": {
  265. "collapsed": false,
  266. "scrolled": false
  267. },
  268. "outputs": [
  269. {
  270. "name": "stdout",
  271. "output_type": "stream",
  272. "text": [
  273. "[['Id', 'Year', 'Id', 'Sex', 'Id', 'Hispanic Origin', 'Id', 'Id2', 'Geography', 'Total', 'Race Alone - White', 'Race Alone - Hispanic', 'Race Alone - Black or African American', 'Race Alone - American Indian and Alaska Native', 'Race Alone - Asian', 'Race Alone - Native Hawaiian and Other Pacific Islander', 'Two or More Races'], ['cen42010', 'April 1, 2010 Census', 'totsex', 'Both Sexes', 'tothisp', 'Total', '0100000US', '', 'United States', '308745538', '197318956', '44618105', '40250635', '3739506', '15159516', '674625', '6984195']]\n"
  274. ]
  275. }
  276. ],
  277. "source": [
  278. "import csv\n",
  279. "\n",
  280. "f = open(\"census.csv\", 'r')\n",
  281. "cen_data = list(csv.reader(f))\n",
  282. "print(cen_data)"
  283. ]
  284. },
  285. {
  286. "cell_type": "markdown",
  287. "metadata": {},
  288. "source": [
  289. "### Computing the Rate of Gun Deaths per 100,000 people"
  290. ]
  291. },
  292. {
  293. "cell_type": "code",
  294. "execution_count": 32,
  295. "metadata": {
  296. "collapsed": false,
  297. "scrolled": true
  298. },
  299. "outputs": [
  300. {
  301. "data": {
  302. "text/plain": [
  303. "{'Asian/Pacific Islander': 8.374309664161762,\n",
  304. " 'Black': 57.8773477735196,\n",
  305. " 'Hispanic': 20.220491210910907,\n",
  306. " 'Native American/Native Alaskan': 24.521955573811088,\n",
  307. " 'White': 33.56849303419181}"
  308. ]
  309. },
  310. "execution_count": 32,
  311. "metadata": {},
  312. "output_type": "execute_result"
  313. }
  314. ],
  315. "source": [
  316. "mapping = {\n",
  317. " 'Asian/Pacific Islander': 15834141,\n",
  318. " 'Native American/Native Alaskan': 3739506,\n",
  319. " 'Black': 40250635,\n",
  320. " 'Hispanic': 44618105,\n",
  321. " 'White': 197318956\n",
  322. "}\n",
  323. "\n",
  324. "race_per_hundredk = {}\n",
  325. "for k,v in race_counts.items():\n",
  326. " race_per_hundredk[k] = (v / mapping[k]) * 100000\n",
  327. "\n",
  328. "race_per_hundredk"
  329. ]
  330. },
  331. {
  332. "cell_type": "markdown",
  333. "metadata": {},
  334. "source": [
  335. "### Computing Gun-Related Homicides by Race per 100,000 people"
  336. ]
  337. },
  338. {
  339. "cell_type": "code",
  340. "execution_count": 33,
  341. "metadata": {
  342. "collapsed": false,
  343. "scrolled": true
  344. },
  345. "outputs": [
  346. {
  347. "data": {
  348. "text/plain": [
  349. "{'Asian/Pacific Islander': 3.530346230970155,\n",
  350. " 'Black': 48.471284987180944,\n",
  351. " 'Hispanic': 12.627161104219914,\n",
  352. " 'Native American/Native Alaskan': 8.717729026240365,\n",
  353. " 'White': 4.6356417981453335}"
  354. ]
  355. },
  356. "execution_count": 33,
  357. "metadata": {},
  358. "output_type": "execute_result"
  359. }
  360. ],
  361. "source": [
  362. "intents = [item[3] for item in data]\n",
  363. "homicide_race_counts = {}\n",
  364. "for i, race in enumerate(races):\n",
  365. " if race not in homicide_race_counts:\n",
  366. " homicide_race_counts[race] = 0\n",
  367. " if intents[i] == \"Homicide\":\n",
  368. " homicide_race_counts[race] += 1\n",
  369. "\n",
  370. "race_per_hundredk = {}\n",
  371. "for k,v in homicide_race_counts.items():\n",
  372. " race_per_hundredk[k] = (v / mapping[k]) * 100000\n",
  373. "\n",
  374. "race_per_hundredk"
  375. ]
  376. },
  377. {
  378. "cell_type": "markdown",
  379. "metadata": {
  380. "collapsed": true
  381. },
  382. "source": [
  383. "## Findings\n",
  384. "\n",
  385. "The data shows that gun-related deaths in the US disproportionately affect the Black and Hispanic racial categories.\n",
  386. "\n",
  387. "Some potential next steps could be to figure out more links between different variables, such as how gun death rates correlate to location and education."
  388. ]
  389. }
  390. ],
  391. "metadata": {
  392. "kernelspec": {
  393. "display_name": "Python 3",
  394. "language": "python",
  395. "name": "python3"
  396. },
  397. "language_info": {
  398. "codemirror_mode": {
  399. "name": "ipython",
  400. "version": 3
  401. },
  402. "file_extension": ".py",
  403. "mimetype": "text/x-python",
  404. "name": "python",
  405. "nbconvert_exporter": "python",
  406. "pygments_lexer": "ipython3",
  407. "version": "3.4.3"
  408. }
  409. },
  410. "nbformat": 4,
  411. "nbformat_minor": 0
  412. }
Add Comment
Please, Sign In to add comment