Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
from pandas import DataFrame
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
#%%
# Figures are not time-consistent: they each represent a point between 2010 - 2016
# source: https://en.wikipedia.org/wiki/List_of_countries_by_intentional_homicide_rate
murders = [["El Salvador", 108.64],
["Honduras", 63.75],
["Venezuela", 57.15],
["United States Virgin Islands", 52.64],
["Jamaica", 43.21],
["Lesotho", 38.00],
["Belize", 34.40],
["South Africa", 34.27],
["Saint Kitts and Nevis", 33.55],
["Guatemala", 31.21],
["Trinidad and Tobago", 30.88],
["Bahamas", 29.81],
["Brazil", 26.74],
["Colombia", 26.50],
["Tuvalu", 20.28],
["Guyana", 19.42],
["Dominican Republic", 17.39],
["Namibia", 16.93],
["Mexico", 16.35],
["Puerto Rico", 15.86 ],
["Cayman Islands", 14.74 ],
["South Sudan", 13.70],
["Democratic Republic of the Congo", 13.36],
["French Guiana", 13.17],
["Central African Republic", 13.09],
["Greenland", 12.46],
["Bolivia", 12.40],
["Uganda", 11.84],
["Ivory Coast", 11.84],
["Costa Rica", 11.77],
["Nicaragua", 11.49],
["Panama", 11.38],
["Russia", 11.31],
["Antigua and Barbuda", 11.23],
["Barbados", 10.91],
["Republic of the Congo", 10.88],
["Mali", 10.82],
["Suriname", 10.68],
["Botswana", 10.53],
["Papua New Guinea", 10.42],
["Mauritania", 10.22],
["Haiti", 10.04],
["Philippines", 9.84],
["Nigeria", 9.79],
["Angola", 9.65],
["Paraguay", 9.29],
["Guinea-Bissau", 9.17],
["Togo", 9.14],
["Gambia", 9.07],
["Chad", 9.02],
["Gabon", 9.00],
["Cape Verde", 8.84],
["Guinea", 8.46],
["Uruguay", 8.42],
["Dominica", 8.40],
["Ecuador", 8.23],
["Swaziland", 8.16],
["Qatar", 8.10],
["Iraq", 8.0],
["Guadeloupe", 7.90 ],
["Pakistan", 7.81],
["Comoros", 7.60],
["Ethiopia", 7.60],
["Grenada", 7.52],
["Kiribati", 7.50],
["Eritrea", 7.46],
["Senegal", 7.30],
["Mongolia", 7.23],
["Peru", 7.16],
["Tanzania", 7.01],
["Laos", 6.87],
["Djibouti", 6.77],
["Zimbabwe", 6.74],
["Yemen", 6.67],
["Afghanistan", 6.55],
["Argentina", 6.53],
["Sudan", 6.45],
["Bermuda", 6.45 ],
["Benin", 6.01],
["Lithuania", 5.98],
["Cameroon", 5.93],
["Zambia", 5.85],
["Kenya", 5.75],
["Somalia", 5.56],
["Kyrgyzstan", 5.12],
["United States", 4.88],
["Kazakhstan", 4.84],
["Marshall Islands", 4.68],
["Micronesia", 4.67],
["Rwanda", 4.51],
["Niger", 4.47],
["North Korea", 4.41],
["Ukraine", 4.36],
["Turkey", 4.30],
["Turkmenistan", 4.20],
["Cuba", 4.19],
["Iran", 4.12],
["Latvia", 4.11],
["Burundi", 4.03],
["Lebanon", 3.95],
["Solomon Islands", 3.77],
["East Timor", 3.69],
["Chile", 3.59],
["Belarus", 3.58],
["Thailand", 3.51],
["Sao Tome and Principe", 3.44],
["Mozambique", 3.39],
["Egypt", 3.23],
["Liberia", 3.22],
["Equatorial Guinea", 3.21],
["India", 3.21],
["Estonia", 3.20],
["Moldova", 3.19],
["Samoa", 3.15],
["Palau", 3.11],
["Niue", 3.07],
["Cook Islands", 3.06],
["Tunisia", 3.05],
["Uzbekistan", 2.99],
["Fiji", 2.97],
["Sri Lanka", 2.91],
["Bhutan", 2.75],
["Montenegro", 2.72],
["Mauritius", 2.71],
["Georgia", 2.68],
["Bangladesh", 2.51],
["Libya", 2.49],
["Guam", 2.49 ],
["Armenia", 2.45],
["Azerbaijan", 2.45],
["Myanmar", 2.42],
["Albania", 2.28],
["Nepal", 2.27],
["Syria", 2.23],
["Seychelles", 2.15],
["Vanuatu", 2.13],
["Jordan", 2.00],
["Belgium", 1.95],
["Sierra Leone", 1.92],
["Malaysia", 1.92],
["Cambodia", 1.84],
["Bulgaria", 1.80],
["Malawi", 1.78],
["Kuwait", 1.78],
["Ghana", 1.70],
["Canada", 1.68],
["Finland", 1.60],
["Kosovo", 1.60],
["Macedonia", 1.59],
["France", 1.58],
["Vietnam", 1.52],
["Saudi Arabia", 1.50],
["Bosnia and Herzegovina", 1.50],
["Romania", 1.49],
["Hungary", 1.48],
["Tajikistan", 1.44],
["Algeria", 1.37],
["Israel", 1.36],
["Nauru", 1.31],
["Cyprus", 1.29],
["Slovenia", 1.21],
["Sweden", 1.15],
["Serbia", 1.13],
["Oman", 1.06],
["Morocco", 1.05],
["Denmark", 0.99],
["Australia", 0.98],
["Portugal", 0.97],
["Malta", 0.96],
["Tonga", 0.95],
["United Kingdom", 0.92],
["Iceland", 0.91],
["New Zealand", 0.91],
["Slovakia", 0.88],
["Croatia", 0.87],
["Maldives", 0.85],
["Greece", 0.85],
["Germany", 0.85],
["Taiwan", 0.82],
["Italy", 0.78],
["Czech Republic", 0.75],
["China", 0.74],
["South Korea", 0.74],
["Poland", 0.74],
["Luxembourg", 0.72],
["Burkina Faso", 0.71],
["Switzerland", 0.69],
["United Arab Emirates", 0.66],
["Spain", 0.66],
["Ireland", 0.64],
["Madagascar", 0.62],
["Netherlands", 0.61],
["Palestine", 0.60],
["Norway", 0.56],
["Bahrain", 0.54],
["Austria", 0.51],
["Indonesia", 0.50],
["Brunei", 0.49],
["French Polynesia", 0.38 ],
["Japan", 0.31],
["Hong Kong", 0.30],
["Singapore", 0.25] ]
murders = DataFrame(murders)
murders.columns = ["Country","Murders"]
# https://en.wikipedia.org/wiki/List_of_countries_by_population_density
densityPerKm =[
["Macau", 21339],
["Monaco", 18589],
["Singapore", 7796],
["Hong Kong", 6698],
["Gibraltar", 4874],
["Bahrain", 1917],
["Vatican City", 1818],
["Malta", 1461],
["Bermuda", 1227],
["Sint Maarten", 1159],
["Maldives", 1154],
["Bangladesh", 1142],
["Jersey", 898],
["Guernsey", 804],
["Palestine", 800],
["Mayotte", 686],
["Barbados", 664],
["Saint Martin", 660],
["Taiwan", 651],
["Mauritius", 620],
["Lebanon", 596],
["Aruba", 575],
["San Marino", 545],
["South Korea", 515],
["Nauru", 514],
["Rwanda", 456],
["Saint Barthélemy", 449],
["Tuvalu", 436],
["Comoros", 433],
["Netherlands", 415],
["Haiti", 411],
["India", 405],
["Israel", 401],
["Burundi", 384],
["Puerto Rico", 376],
["Belgium", 374],
["Curaçao", 361],
["Philippines", 353],
["Guam", 340],
["Réunion", 340],
["Japan", 335],
["Martinique", 334],
["Sri Lanka", 327],
["El Salvador", 313],
["Marshall Islands", 310],
["United States Virgin Islands", 302],
["Grenada", 300],
["American Samoa", 290],
["Saint Lucia", 290],
["Vietnam", 283],
["United Kingdom", 271],
["Trinidad and Tobago", 263],
["Pakistan", 263],
["Jamaica", 248],
["Guadeloupe", 243],
["Liechtenstein", 237],
["Qatar", 233],
["Germany", 231],
["Luxembourg", 228],
["Kuwait", 225],
["Cayman Islands", 214],
["Dominican Republic", 214],
["North Korea", 212],
["Nigeria", 212],
["Seychelles", 211],
["Switzerland", 205],
["Italy", 201],
["Gambia", 197],
["Nepal", 196],
["British Virgin Islands", 196],
["Antigua and Barbuda", 195],
["Saint Kitts and Nevis", 192],
["São Tomé and Príncipe", 187],
["Andorra", 173],
["Kosovo", 163],
["Kiribati", 159],
["Malawi", 157],
["Uganda", 156],
["Federated States of Micronesia", 149],
["Isle of Man", 146],
["Cyprus", 145],
["Guatemala", 145],
["China", 144],
["Tonga", 140],
["Anguilla", 140],
["Indonesia", 138],
["Czech Republic", 135],
["Denmark", 134],
["Thailand", 133],
["Cape Verde", 133],
["Northern Ireland", 133],
["Togo", 127],
["France", 124],
["Poland", 123],
["Ghana", 121],
["Northern Mariana Islands", 118],
["United Arab Emirates", 118],
["Azerbaijan", 114],
["Jordan", 113],
["Portugal", 112],
["Slovakia", 111],
["Austria", 105],
["Hungary", 105],
["Moldova", 105],
["Turkey", 103],
["Cuba", 102],
["Slovenia", 102],
["Armenia", 100],
["Albania", 100],
["Syria", 99],
["Sierra Leone", 99],
["Benin", 98],
["Malaysia", 98],
["Costa Rica", 97],
["Egypt", 97],
["Dominica", 96],
["Ethiopia", 96],
["Northern Cyprus", 93],
["Spain", 92],
["Serbia", 91],
["Iraq", 88],
["Cambodia", 88],
["Kenya", 88],
["Greece", 82],
["Romania", 82],
["Macedonia", 81],
["Myanmar", 80],
["Honduras", 79],
["Senegal", 78],
["Morocco", 78],
["Timor-Leste", 78],
["French Polynesia", 76],
["Turks and Caicos Islands", 76],
["Ivory Coast", 75],
["Lesotho", 74],
["Croatia", 73],
["Burkina Faso", 73],
["Brunei", 73],
["Uzbekistan", 72],
["Tunisia", 70],
["Ukraine", 70],
["Samoa", 70],
["Bosnia and Herzegovina", 69],
["Ireland", 68],
["Swaziland", 66],
["Caribbean Netherlands", 66],
["Norfolk Island", 66],
["Bulgaria", 64],
["Cook Islands", 63],
["Mexico", 63],
["Yemen", 62],
["Tajikistan", 62],
["Ecuador", 61],
["Tanzania", 61],
["Panama", 56],
["Georgia", 53],
["Guinea", 52],
["Nicaragua", 52],
["Cameroon", 51],
["Iran", 49],
["Montserrat", 48],
["Wallis and Futuna", 48],
["Fiji", 47],
["South Africa", 46],
["Belarus", 46],
["Afghanistan", 46],
["Montenegro", 45],
["Eritrea", 45],
["Liberia", 44],
["Madagascar", 44],
["Equatorial Guinea", 44],
["Colombia", 44],
["Palau", 43],
["Guinea-Bissau", 43],
["Lithuania", 43],
["Cocos", 39],
["Djibouti", 38],
["Zimbabwe", 37],
["Saint Helena", 37],
["Democratic Republic of the Congo", 36],
["Faroe Islands", 34],
["Venezuela", 34],
["Mozambique", 34],
["United States", 33],
["Kyrgyzstan", 31],
["Latvia", 30],
["Estonia", 29],
["Bahamas", 28],
["Laos", 27],
["Saint Pierre and Miquelon", 25],
["Peru", 25],
["Brazil", 25],
["Angola", 23],
["Chile", 23],
["Somalia", 23],
["Solomon Islands", 23],
["Sweden", 23],
["Zambia", 22],
["Sudan", 22],
["Vanuatu", 22],
["Bhutan", 21],
["South Sudan", 20],
["Uruguay", 20],
["Åland Islands", 18],
["Algeria", 18],
["Niger", 18],
["New Zealand", 18],
["Papua New Guinea", 18],
["Paraguay", 17],
["Belize", 17],
["Norway", 16],
["Finland", 16],
["Argentina", 16],
["Saudi Arabia", 16],
["Republic of the Congo", 15],
["Mali", 15],
["Christmas Island", 15],
["New Caledonia", 14],
["Oman", 14],
["Turkmenistan", 12],
["Chad", 12],
["Ascension Island", 10],
["Bolivia", 10],
["Russia", 9],
["Gabon", 8],
["Central African Republic", 7],
["Kazakhstan", 7],
["Niue", 6],
["Guyana", 4],
["Libya", 4],
["Canada", 4],
["Botswana", 3.5],
["Mauritania", 3.4],
["Iceland", 3.4],
["Suriname", 3.3],
["Australia", 3],
["Namibia", 2.9],
["French Guiana", 2.8],
["Mongolia", 1.9],
["Tristan da Cunha", 1.3],
["Pitcairn Islands", 1.2],
["Falkland Islands", 0.21],
["Greenland", 0.03]
]
densityPerKm = DataFrame(densityPerKm)
densityPerKm.columns = ["Country","Density"]
#%%
data = densityPerKm.merge(murders, how="inner", suffixes=('_x', '_y'))
# Pearson's
corr = data[["Murders","Density"]].corr()["Density"].loc["Murders"]
#>>> -0.074528
#%%
p = sns.regplot(x="Density", y="Murders", data=data, line_kws={"color": "red"})
plt.ylabel("murders per 100,000")
plt.xlabel("pop density per km^2")
p.set(ylim=(0, None))
p.set(xlim=(0, None))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment