Skip to content

Instantly share code, notes, and snippets.

@Ken-Watson
Last active October 23, 2018 17:29
Show Gist options
  • Save Ken-Watson/18df345fae4405ffaeb124e457a83270 to your computer and use it in GitHub Desktop.
Save Ken-Watson/18df345fae4405ffaeb124e457a83270 to your computer and use it in GitHub Desktop.
Analyzing Gun Deaths in the US
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Analyzing Gun Deaths in the US"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## This dataset from FiveThirtyEight contains information on gun deaths in the US from 2012 to 2014."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Findings\n",
"\n",
"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",
"\n",
"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",
"\n",
"I also checked to see if the total deaths by race coincides with the proportion of each race. Census data to see what percentage the US population falls into each race.\n",
"\n",
"The data shows that gun-related deaths in the US disproportionately affect the Black and Hispanic racial categories.\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introducing US Gun Death Data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import csv\n",
"f = open(\"guns.csv\", 'r')\n",
"data = list(csv.reader(f))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Eliminate header from data set"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['', 'year', 'month', 'intent', 'police', 'sex', 'age', 'race', 'hispanic', 'place', 'education']]\n",
"[['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"
]
}
],
"source": [
"headers = data[:1]\n",
"data = data[1:]\n",
"print(headers)\n",
"print(data[:5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Counting gun deaths by year"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'2012': 33562, '2013': 33635, '2014': 33598}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"years = [row[1] for row in data]\n",
"year_counts = {}\n",
"for year in years:\n",
" if year not in year_counts:\n",
" year_counts[year] = 0\n",
" else:\n",
" year_counts[year] += 1\n",
"year_counts\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Looks like there is not much change between years. We can check if gun deaths change in the US by year and month.\n",
"\n",
"### Counting Gun Deaths by Year and Month"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{datetime.datetime(2012, 1, 1, 0, 0): 2758,\n",
" datetime.datetime(2012, 2, 1, 0, 0): 2357,\n",
" datetime.datetime(2012, 3, 1, 0, 0): 2743,\n",
" datetime.datetime(2012, 4, 1, 0, 0): 2795,\n",
" datetime.datetime(2012, 5, 1, 0, 0): 2999,\n",
" datetime.datetime(2012, 6, 1, 0, 0): 2826,\n",
" datetime.datetime(2012, 7, 1, 0, 0): 3026,\n",
" datetime.datetime(2012, 8, 1, 0, 0): 2954,\n",
" datetime.datetime(2012, 9, 1, 0, 0): 2852,\n",
" datetime.datetime(2012, 10, 1, 0, 0): 2733,\n",
" datetime.datetime(2012, 11, 1, 0, 0): 2729,\n",
" datetime.datetime(2012, 12, 1, 0, 0): 2791,\n",
" datetime.datetime(2013, 1, 1, 0, 0): 2864,\n",
" datetime.datetime(2013, 2, 1, 0, 0): 2375,\n",
" datetime.datetime(2013, 3, 1, 0, 0): 2862,\n",
" datetime.datetime(2013, 4, 1, 0, 0): 2798,\n",
" datetime.datetime(2013, 5, 1, 0, 0): 2806,\n",
" datetime.datetime(2013, 6, 1, 0, 0): 2920,\n",
" datetime.datetime(2013, 7, 1, 0, 0): 3079,\n",
" datetime.datetime(2013, 8, 1, 0, 0): 2859,\n",
" datetime.datetime(2013, 9, 1, 0, 0): 2742,\n",
" datetime.datetime(2013, 10, 1, 0, 0): 2808,\n",
" datetime.datetime(2013, 11, 1, 0, 0): 2758,\n",
" datetime.datetime(2013, 12, 1, 0, 0): 2765,\n",
" datetime.datetime(2014, 1, 1, 0, 0): 2651,\n",
" datetime.datetime(2014, 2, 1, 0, 0): 2361,\n",
" datetime.datetime(2014, 3, 1, 0, 0): 2684,\n",
" datetime.datetime(2014, 4, 1, 0, 0): 2862,\n",
" datetime.datetime(2014, 5, 1, 0, 0): 2864,\n",
" datetime.datetime(2014, 6, 1, 0, 0): 2931,\n",
" datetime.datetime(2014, 7, 1, 0, 0): 2884,\n",
" datetime.datetime(2014, 8, 1, 0, 0): 2970,\n",
" datetime.datetime(2014, 9, 1, 0, 0): 2914,\n",
" datetime.datetime(2014, 10, 1, 0, 0): 2865,\n",
" datetime.datetime(2014, 11, 1, 0, 0): 2756,\n",
" datetime.datetime(2014, 12, 1, 0, 0): 2857}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"dates = [datetime.datetime(year=int(row[1]), month=int(row[2]), day=1) for row in data]\n",
"date_counts = {}\n",
"for date in dates:\n",
" if date not in date_counts:\n",
" date_counts[date] = 0\n",
" date_counts[date] += 1\n",
"date_counts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exploring How Gun Deaths Vary by Sex and Race"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'F': 14449, 'M': 86349}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sex = [row[5] for row in data]\n",
"sex_counts = {}\n",
"for gender in sex:\n",
" if gender not in sex_counts:\n",
" sex_counts[gender] = 0\n",
" sex_counts[gender] += 1\n",
"sex_counts"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Asian/Pacific Islander': 1326,\n",
" 'Black': 23296,\n",
" 'Hispanic': 9022,\n",
" 'Native American/Native Alaskan': 917,\n",
" 'White': 66237}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"races = [row[7] for row in data]\n",
"race_counts = {}\n",
"for race_classification in races:\n",
" if race_classification not in race_counts:\n",
" race_counts[race_classification] = 0\n",
" race_counts[race_classification] += 1\n",
"race_counts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### How Do Total Deaths by Race Compare to the US Population?"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['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"
]
}
],
"source": [
"import csv\n",
"\n",
"f = open(\"census.csv\", 'r')\n",
"cen_data = list(csv.reader(f))\n",
"print(cen_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Computing the Rate of Gun Deaths per 100,000 people"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Asian/Pacific Islander': 8.374309664161762,\n",
" 'Black': 57.8773477735196,\n",
" 'Hispanic': 20.220491210910907,\n",
" 'Native American/Native Alaskan': 24.521955573811088,\n",
" 'White': 33.56849303419181}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mapping = {\n",
" 'Asian/Pacific Islander': 15834141,\n",
" 'Native American/Native Alaskan': 3739506,\n",
" 'Black': 40250635,\n",
" 'Hispanic': 44618105,\n",
" 'White': 197318956\n",
"}\n",
"\n",
"race_per_hundredk = {}\n",
"for k,v in race_counts.items():\n",
" race_per_hundredk[k] = (v / mapping[k]) * 100000\n",
"\n",
"race_per_hundredk"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Computing Gun-Related Homicides by Race per 100,000 people"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Asian/Pacific Islander': 3.530346230970155,\n",
" 'Black': 48.471284987180944,\n",
" 'Hispanic': 12.627161104219914,\n",
" 'Native American/Native Alaskan': 8.717729026240365,\n",
" 'White': 4.6356417981453335}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"intents = [item[3] for item in data]\n",
"homicide_race_counts = {}\n",
"for i, race in enumerate(races):\n",
" if race not in homicide_race_counts:\n",
" homicide_race_counts[race] = 0\n",
" if intents[i] == \"Homicide\":\n",
" homicide_race_counts[race] += 1\n",
"\n",
"race_per_hundredk = {}\n",
"for k,v in homicide_race_counts.items():\n",
" race_per_hundredk[k] = (v / mapping[k]) * 100000\n",
"\n",
"race_per_hundredk"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment