Assignment 4 - Geography Solution

We will first make the function to compute the great circle distance

In [1]:
from math import sin,cos,atan2,sqrt,radians
def getDistance(phi_1,lambda_1,phi_2=33.6547,lambda_2=73.2500):    
    phi_1,phi_2=radians(phi_1),radians(phi_2)
    lambda_1,lambda_2=radians(lambda_1),radians(lambda_2)    
    d_phi=phi_2-phi_1
    d_lambda=lambda_2-lambda_1    
    a=sin(d_phi/2.0)**2+cos(phi_1)*cos(phi_2)*sin(d_lambda/2.0)**2
    b=2*atan2(sqrt(a),sqrt(1-a))
    R=6371
    d=R*b    
    return d

We will now import our data

In [2]:
import csv
fname = 'countries.csv'
with open(fname, 'rb') as f:
    reader = csv.reader(f)
    data=[]
    for row in reader:
        data.append(row[:2]+[float(v) for v in row[2:]])

Create our matrix of pairwise distances and a list of distances for computing the histogram.

In [3]:
import numpy as np
Ncities = len(data)
distances = np.zeros((Ncities,Ncities))
flat_distances = []
for i in range(Ncities):
    distances[i,i] = np.nan
    for j in range(i+1,Ncities):
        distances[i,j] = getDistance(data[i][2],data[i][3],data[j][2],data[j][3])
        distances[j,i] = distances[i,j]
        flat_distances.append(distances[i,j])
In [4]:
print distances
[[           nan  4334.69682243  5857.7213588  ...,  3281.06909049
   7034.73151314  7080.56114668]
 [ 4334.69682243            nan  1533.06426381 ...,  3719.98643493
   6369.17407835  6679.37778062]
 [ 5857.7213588   1533.06426381            nan ...,  4699.85774014
   6375.89890102  6743.41730804]
 ..., 
 [ 3281.06909049  3719.98643493  4699.85774014 ...,            nan
   3841.45777743  3961.31895005]
 [ 7034.73151314  6369.17407835  6375.89890102 ...,  3841.45777743
             nan   398.58972843]
 [ 7080.56114668  6679.37778062  6743.41730804 ...,  3961.31895005
    398.58972843            nan]]

In [5]:
import matplotlib.pyplot as plt
plt.imshow(distances)
Out[5]:
<matplotlib.image.AxesImage at 0x5ffa6b0>

Histogram of pairwise distances between all capital cities

The histogram is calculated below. Can you tell anything from the shape of this histogram about your data?

In [6]:
plt.hist(flat_distances,bins = 20); plt.xlabel('Distances'); plt.ylabel('Count'); plt.grid()

Nearest and Farthest Capital from each Capital

We will now compute the indices of the closest and farthest capital from each given capital.

In [7]:
ci = np.nanargmin(distances,axis=0)
In [8]:
fi = np.nanargmax(distances,axis=0)

We will now create a list of lists in which each row is: (country, capital city, closest capital, farthest capital), print it and save it to a csv file.

In [9]:
cf = [data[i][:2]+['-'.join(data[c][:2]),'-'.join(data[f][:2])] for i,f,c in zip(range(Ncities),fi,ci)] #There are easier ways
In [10]:
for v in cf:
    print ' : '.join(v)
Afghanistan : Kabul : Pakistan-Islamabad : Pitcairn-Adamstown
Albania : Tirana : Montenegro-Podgorica : New Zealand-Wellington
Algeria : Algiers : Tunisia-Tunis : New Zealand-Wellington
American Samoa : Pago Pago : Samoa-Apia : Nigeria-Abuja
Andorra : Andorra la Vella : Spain-Madrid : New Zealand-Wellington
Angola : Luanda : Democratic Republic of the Congo-Kinshasa : Nauru-Yaren
Anguilla : The Valley : Saint Martin-Marigot : East Timor-Dili
Antigua and Barbuda : St. John's : Montserrat-Plymouth : East Timor-Dili
Argentina : Buenos Aires : Uruguay-Montevideo : South Korea-Seoul
Armenia : Yerevan : Georgia-Tbilisi : Pitcairn-Adamstown
Aruba : Oranjestad : Venezuela-Caracas : Christmas Island-The Settlement
Australia : Canberra : Norfolk Island-Kingston : Western Sahara-El Aai�n
Austria : Vienna : Slovakia-Bratislava : New Zealand-Wellington
Azerbaijan : Baku : Georgia-Tbilisi : Pitcairn-Adamstown
Bahamas : Nassau : Cuba-Havana : Cocos Islands-West Island
Bahrain : Manama : Qatar-Doha : Pitcairn-Adamstown
Bangladesh : Dhaka : Bhutan-Thimphu : Peru-Lima
Barbados : Bridgetown : Saint Vincent and the Grenadines-Kingstown : East Timor-Dili
Belarus : Minsk : Lithuania-Vilnius : New Zealand-Wellington
Belgium : Brussels : Netherlands-Amsterdam : New Zealand-Wellington
Belize : Belmopan : Guatemala-New Guatemala : Cocos Islands-West Island
Benin : Porto-Novo : Togo-Lom� : Tuvalu-Funafuti
Bermuda : Hamilton : United States-Washington : Christmas Island-The Settlement
Bhutan : Thimphu : Bangladesh-Dhaka : Chile-Santiago
Bolivia : Sucre : Paraguay-Asunci�n : Hong Kong-Hong Kong
Bosnia and Herzegovina : Sarajevo : Montenegro-Podgorica : New Zealand-Wellington
Botswana : Gaborone : South Africa-Pretoria : Nauru-Yaren
Brazil : Bras�lia : Paraguay-Asunci�n : Palau-Melekeok
British Indian Ocean Territory : Diego Garcia : Svalbard and Jan Mayen-Longyearbyen : New Zealand-Wellington
British Virgin Islands : Road Town : U.S. Virgin Islands-Charlotte Amalie : Christmas Island-The Settlement
Brunei : Bandar Seri Begawan : Philippines-Manila : Colombia-Bogot�
Bulgaria : Sofia : Macedonia-Skopje : New Zealand-Wellington
Burkina Faso : Ouagadougou : Niger-Niamey : Tuvalu-Funafuti
Burundi : Bujumbura : Rwanda-Kigali : French Polynesia-Papeete
Cambodia : Phnom Penh : Thailand-Bangkok : Peru-Lima
Cameroon : Yaound� : Equatorial Guinea-Malabo : Samoa-Apia
Canada : Ottawa : United States-Washington : Cocos Islands-West Island
Cape Verde : Praia : Senegal-Dakar : Solomon Islands-Honiara
Cayman Islands : George Town : Cuba-Havana : Cocos Islands-West Island
Central African Republic : Bangui : Cameroon-Yaound� : American Samoa-Pago Pago
Chad : N'Djamena : Nigeria-Abuja : American Samoa-Pago Pago
Chile : Santiago : Argentina-Buenos Aires : China-Beijing
China : Beijing : North Korea-Pyongyang : Argentina-Buenos Aires
Christmas Island : The Settlement : Indonesia-Jakarta : Aruba-Oranjestad
Cocos Islands : West Island : Christmas Island-The Settlement : Costa Rica-San Jos�
Colombia : Bogot� : Ecuador-Quito : Indonesia-Jakarta
Comoros : Moroni : Mayotte-Mamoutzou : French Polynesia-Papeete
Cook Islands : Avarua : Niue-Alofi : Chad-N'Djamena
Costa Rica : San Jos� : Nicaragua-Managua : Cocos Islands-West Island
Croatia : Zagreb : Slovenia-Ljubljana : New Zealand-Wellington
Cuba : Havana : Cayman Islands-George Town : Cocos Islands-West Island
Cyprus : Nicosia : Israel-Jerusalem : Pitcairn-Adamstown
Czech Republic : Prague : Austria-Vienna : New Zealand-Wellington
Democratic Republic of the Congo : Kinshasa : Republic of the Congo-Brazzaville : Nauru-Yaren
Denmark : Copenhagen : Germany-Berlin : New Zealand-Wellington
Djibouti : Djibouti : Yemen-Sanaa : French Polynesia-Papeete
Dominica : Roseau : Martinique-Fort-de-France : East Timor-Dili
Dominican Republic : Santo Domingo : Haiti-Port-au-Prince : Christmas Island-The Settlement
East Timor : Dili : Brunei-Bandar Seri Begawan : Suriname-Paramaribo
Ecuador : Quito : Colombia-Bogot� : Singapore-Singapore
Egypt : Cairo : Jordan-Amman : French Polynesia-Papeete
El Salvador : San Salvador : Guatemala-New Guatemala : Cocos Islands-West Island
Equatorial Guinea : Malabo : Cameroon-Yaound� : Samoa-Apia
Eritrea : Asmara : Yemen-Sanaa : French Polynesia-Papeete
Estonia : Tallinn : Finland-Helsinki : New Zealand-Wellington
Ethiopia : Addis Ababa : Djibouti-Djibouti : French Polynesia-Papeete
Falkland Islands : Stanley : South Georgia and the South Sandwich Islands-Grytviken : Mongolia-Ulan Bator
Faroe Islands : T�rshavn : Iceland-Reykjav�k : New Zealand-Wellington
Fiji : Suva : Tonga-Nuku'alofa : Burkina Faso-Ouagadougou
Finland : Helsinki : Estonia-Tallinn : New Zealand-Wellington
France : Paris : Belgium-Brussels : New Zealand-Wellington
French Guiana : Cayenne : Suriname-Paramaribo : East Timor-Dili
French Polynesia : Papeete : Cook Islands-Avarua : Sudan-Khartoum
Gabon : Libreville : Sao Tome and Principe-S�o Tom� : Tuvalu-Funafuti
Gambia : Banjul : Senegal-Dakar : Solomon Islands-Honiara
Georgia : Tbilisi : Armenia-Yerevan : Pitcairn-Adamstown
Germany : Berlin : Czech Republic-Prague : New Zealand-Wellington
Ghana : Accra : Togo-Lom� : Tuvalu-Funafuti
Gibraltar : Gibraltar : Morocco-Rabat : New Zealand-Wellington
Greece : Athens : Macedonia-Skopje : Cook Islands-Avarua
Greenland : Nuuk (Godth�b) : Iceland-Reykjav�k : Australia-Canberra
Grenada : St. George's : Saint Vincent and the Grenadines-Kingstown : East Timor-Dili
Guadeloupe : Basse-Terre : Dominica-Roseau : East Timor-Dili
Guam : Hag�t�a : Northern Mariana Islands-Saipan : Brazil-Bras�lia
Guatemala : New Guatemala : El Salvador-San Salvador : Cocos Islands-West Island
Guernsey : St Peter Port : Jersey-Saint Helier : New Zealand-Wellington
Guinea : Conakry : Sierra Leone-Freetown : Tokelau-Nukunonu
Guinea-Bissau : Bissau : Gambia-Banjul : Solomon Islands-Honiara
Guyana : Georgetown : Suriname-Paramaribo : East Timor-Dili
Haiti : Port-au-Prince : Dominican Republic-Santo Domingo : Christmas Island-The Settlement
Honduras : Tegucigalpa : El Salvador-San Salvador : Cocos Islands-West Island
Hong Kong : Hong Kong : Macao-Macao : Bolivia-Sucre
Hungary : Budapest : Slovakia-Bratislava : New Zealand-Wellington
Iceland : Reykjav�k : Faroe Islands-T�rshavn : New Zealand-Wellington
India : New Delhi : Pakistan-Islamabad : Pitcairn-Adamstown
Indonesia : Jakarta : Christmas Island-The Settlement : Colombia-Bogot�
Iran : Tehran : Azerbaijan-Baku : Pitcairn-Adamstown
Iraq : Baghdad : Kuwait-Kuwait City : Pitcairn-Adamstown
Ireland : Dublin : Isle of Man-Douglas : New Zealand-Wellington
Isle of Man : Douglas : Ireland-Dublin : New Zealand-Wellington
Israel : Jerusalem : Palestinian Territory-East Jerusalem : Cook Islands-Avarua
Italy : Rome : Vatican-Vatican : New Zealand-Wellington
Ivory Coast : Yamoussoukro : Ghana-Accra : Tokelau-Nukunonu
Jamaica : Kingston : Haiti-Port-au-Prince : Christmas Island-The Settlement
Japan : Edo : South Korea-Seoul : Uruguay-Montevideo
Jersey : Saint Helier : Guernsey-St Peter Port : New Zealand-Wellington
Jordan : Amman : Syria-Damascus : Pitcairn-Adamstown
Kazakhstan : Astana : Kyrgyzstan-Bishkek : Pitcairn-Adamstown
Kenya : Nairobi : Uganda-Kampala : French Polynesia-Papeete
Kiribati : South Tarawa : Marshall Islands-Majuro : Ivory Coast-Yamoussoukro
Kosovo : Pristina : Macedonia-Skopje : New Zealand-Wellington
Kuwait : Kuwait City : Bahrain-Manama : Pitcairn-Adamstown
Kyrgyzstan : Bishkek : Uzbekistan-Tashkent : Pitcairn-Adamstown
Laos : Vientiane : Vietnam-Hanoi : Peru-Lima
Latvia : Riga : Lithuania-Vilnius : New Zealand-Wellington
Lebanon : Beirut : Syria-Damascus : Pitcairn-Adamstown
Lesotho : Maseru : South Africa-Pretoria : Nauru-Yaren
Liberia : Monrovia : Sierra Leone-Freetown : Tokelau-Nukunonu
Libya : Tripoli : Malta-Valletta : Cook Islands-Avarua
Liechtenstein : Vaduz : Switzerland-Berne : New Zealand-Wellington
Lithuania : Vilnius : Belarus-Minsk : New Zealand-Wellington
Luxembourg : Luxembourg : Belgium-Brussels : New Zealand-Wellington
Macao : Macao : Hong Kong-Hong Kong : Bolivia-Sucre
Macedonia : Skopje : Kosovo-Pristina : New Zealand-Wellington
Madagascar : Antananarivo : Mayotte-Mamoutzou : Mexico-Mexico City
Malawi : Lilongwe : Zimbabwe-Harare : French Polynesia-Papeete
Malaysia : Kuala Lumpur : Singapore-Singapore : Ecuador-Quito
Maldives : Mal� : Sri Lanka-Colombo : Guatemala-New Guatemala
Mali : Bamako : Burkina Faso-Ouagadougou : Tokelau-Nukunonu
Malta : Valletta : Libya-Tripoli : Cook Islands-Avarua
Marshall Islands : Majuro : Kiribati-South Tarawa : Saint Helena-Jamestown
Martinique : Fort-de-France : Saint Lucia-Castries : East Timor-Dili
Mauritania : Nouakchott : Senegal-Dakar : Vanuatu-Port Vila
Mauritius : Port Louis : Reunion-Saint-Denis : Mexico-Mexico City
Mayotte : Mamoutzou : Comoros-Moroni : French Polynesia-Papeete
Mexico : Mexico City : Guatemala-New Guatemala : Cocos Islands-West Island
Micronesia : Palikir : Marshall Islands-Majuro : Liberia-Monrovia
Moldova : Chi?in?u : Romania-Bucharest : New Zealand-Wellington
Monaco : Monaco : Switzerland-Berne : New Zealand-Wellington
Mongolia : Ulan Bator : China-Beijing : Falkland Islands-Stanley
Montenegro : Podgorica : Albania-Tirana : New Zealand-Wellington
Montserrat : Plymouth : Antigua and Barbuda-St. John's : East Timor-Dili
Morocco : Rabat : Gibraltar-Gibraltar : Norfolk Island-Kingston
Mozambique : Maputo : Swaziland-Mbabane : Nauru-Yaren
Myanmar : Nay Pyi Taw : Laos-Vientiane : Peru-Lima
Namibia : Windhoek : Botswana-Gaborone : Nauru-Yaren
Nauru : Yaren : Marshall Islands-Majuro : Saint Helena-Jamestown
Nepal : Kathmandu : Bhutan-Thimphu : Chile-Santiago
Netherlands : Amsterdam : Belgium-Brussels : New Zealand-Wellington
Netherlands Antilles : Willemstad : South Georgia and the South Sandwich Islands-Grytviken : Svalbard and Jan Mayen-Longyearbyen
New Caledonia : Noumea : Vanuatu-Port Vila : Mauritania-Nouakchott
New Zealand : Wellington : Norfolk Island-Kingston : Spain-Madrid
Nicaragua : Managua : Honduras-Tegucigalpa : Cocos Islands-West Island
Niger : Niamey : Burkina Faso-Ouagadougou : Wallis and Futuna-Mata-Utu
Nigeria : Abuja : Equatorial Guinea-Malabo : Samoa-Apia
Niue : Alofi : American Samoa-Pago Pago : Chad-N'Djamena
Norfolk Island : Kingston : New Caledonia-Noumea : Western Sahara-El Aai�n
North Korea : Pyongyang : South Korea-Seoul : Uruguay-Montevideo
Northern Mariana Islands : Saipan : Guam-Hag�t�a : Brazil-Bras�lia
Norway : Oslo : Sweden-Stockholm : New Zealand-Wellington
Oman : Muscat : United Arab Emirates-Abu Dhabi : Pitcairn-Adamstown
Pakistan : Islamabad : Afghanistan-Kabul : Pitcairn-Adamstown
Palau : Melekeok : Guam-Hag�t�a : Brazil-Bras�lia
Palestinian Territory : East Jerusalem : Israel-Jerusalem : Cook Islands-Avarua
Panama : Panama City : Costa Rica-San Jos� : Cocos Islands-West Island
Papua New Guinea : Port Moresby : Solomon Islands-Honiara : Cape Verde-Praia
Paraguay : Asunci�n : Argentina-Buenos Aires : Taiwan-Taipei
Peru : Lima : Ecuador-Quito : Cambodia-Phnom Penh
Philippines : Manila : Hong Kong-Hong Kong : Bolivia-Sucre
Pitcairn : Adamstown : French Polynesia-Papeete : Bahrain-Manama
Poland : Warsaw : Lithuania-Vilnius : New Zealand-Wellington
Portugal : Lisbon : Gibraltar-Gibraltar : New Zealand-Wellington
Puerto Rico : San Juan : U.S. Virgin Islands-Charlotte Amalie : Christmas Island-The Settlement
Qatar : Doha : Bahrain-Manama : Pitcairn-Adamstown
Republic of the Congo : Brazzaville : Democratic Republic of the Congo-Kinshasa : Nauru-Yaren
Reunion : Saint-Denis : Mauritius-Port Louis : Mexico-Mexico City
Romania : Bucharest : Bulgaria-Sofia : New Zealand-Wellington
Russia : Moscow : Belarus-Minsk : New Zealand-Wellington
Rwanda : Kigali : Burundi-Bujumbura : French Polynesia-Papeete
Saint Barth�lemy : Gustavia : Saint Martin-Marigot : East Timor-Dili
Saint Helena : Jamestown : Angola-Luanda : Nauru-Yaren
Saint Kitts and Nevis : Basseterre : Saint Barth�lemy-Gustavia : East Timor-Dili
Saint Lucia : Castries : Martinique-Fort-de-France : East Timor-Dili
Saint Martin : Marigot : Anguilla-The Valley : East Timor-Dili
Saint Pierre and Miquelon : Saint-Pierre : Canada-Ottawa : Australia-Canberra
Saint Vincent and the Grenadines : Kingstown : Saint Lucia-Castries : East Timor-Dili
Samoa : Apia : American Samoa-Pago Pago : Nigeria-Abuja
San Marino : San Marino : Vatican-Vatican : New Zealand-Wellington
Sao Tome and Principe : S�o Tom� : Gabon-Libreville : Tuvalu-Funafuti
Saudi Arabia : Riyadh : Bahrain-Manama : Pitcairn-Adamstown
Senegal : Dakar : Gambia-Banjul : Solomon Islands-Honiara
Serbia : Belgrade : Bosnia and Herzegovina-Sarajevo : New Zealand-Wellington
Seychelles : Victoria : Somalia-Mogadishu : Mexico-Mexico City
Sierra Leone : Freetown : Guinea-Conakry : Tokelau-Nukunonu
Singapore : Singapore : Malaysia-Kuala Lumpur : Ecuador-Quito
Slovakia : Bratislava : Austria-Vienna : New Zealand-Wellington
Slovenia : Ljubljana : Croatia-Zagreb : New Zealand-Wellington
Solomon Islands : Honiara : Tokelau-Nukunonu : Guinea-Bissau-Bissau
Somalia : Mogadishu : Kenya-Nairobi : French Polynesia-Papeete
South Africa : Pretoria : Botswana-Gaborone : Nauru-Yaren
South Georgia and the South Sandwich Islands : Grytviken : Falkland Islands-Stanley : Japan-Edo
South Korea : Seoul : North Korea-Pyongyang : Uruguay-Montevideo
Spain : Madrid : Andorra-Andorra la Vella : New Zealand-Wellington
Sri Lanka : Colombo : Maldives-Mal� : Ecuador-Quito
Sudan : Khartoum : Eritrea-Asmara : French Polynesia-Papeete
Suriname : Paramaribo : French Guiana-Cayenne : East Timor-Dili
Svalbard and Jan Mayen : Longyearbyen : British Indian Ocean Territory-Diego Garcia : Netherlands Antilles-Willemstad
Swaziland : Mbabane : Mozambique-Maputo : Nauru-Yaren
Sweden : Stockholm : Estonia-Tallinn : New Zealand-Wellington
Switzerland : Berne : Liechtenstein-Vaduz : New Zealand-Wellington
Syria : Damascus : Lebanon-Beirut : Pitcairn-Adamstown
Taiwan : Taipei : Hong Kong-Hong Kong : Paraguay-Asunci�n
Tajikistan : Dushanbe : Uzbekistan-Tashkent : Pitcairn-Adamstown
Tanzania : Dodoma : Kenya-Nairobi : French Polynesia-Papeete
Thailand : Bangkok : Laos-Vientiane : Peru-Lima
Togo : Lom� : Benin-Porto-Novo : Tuvalu-Funafuti
Tokelau : Nukunonu : Tuvalu-Funafuti : Liberia-Monrovia
Tonga : Nuku'alofa : Niue-Alofi : Niger-Niamey
Trinidad and Tobago : Port of Spain : Grenada-St. George's : East Timor-Dili
Tunisia : Tunis : Malta-Valletta : New Zealand-Wellington
Turkey : Ankara : Israel-Jerusalem : Pitcairn-Adamstown
Turkmenistan : Ashkhabad : Iran-Tehran : Pitcairn-Adamstown
Turks and Caicos Islands : Cockburn Town : Haiti-Port-au-Prince : Christmas Island-The Settlement
Tuvalu : Funafuti : Wallis and Futuna-Mata-Utu : Ghana-Accra
U.S. Virgin Islands : Charlotte Amalie : British Virgin Islands-Road Town : Christmas Island-The Settlement
Uganda : Kampala : Rwanda-Kigali : French Polynesia-Papeete
Ukraine : Kiev : Moldova-Chi?in?u : New Zealand-Wellington
United Arab Emirates : Abu Dhabi : Qatar-Doha : Pitcairn-Adamstown
United Kingdom : London : Guernsey-St Peter Port : New Zealand-Wellington
United States : Washington : Canada-Ottawa : Cocos Islands-West Island
Uruguay : Montevideo : Argentina-Buenos Aires : South Korea-Seoul
Uzbekistan : Tashkent : Tajikistan-Dushanbe : Pitcairn-Adamstown
Vanuatu : Port Vila : New Caledonia-Noumea : Mauritania-Nouakchott
Vatican : Vatican : Italy-Rome : New Zealand-Wellington
Venezuela : Caracas : Aruba-Oranjestad : Christmas Island-The Settlement
Vietnam : Hanoi : Laos-Vientiane : Bolivia-Sucre
Wallis and Futuna : Mata-Utu : Samoa-Apia : Niger-Niamey
Western Sahara : El Aai�n : Morocco-Rabat : Norfolk Island-Kingston
Yemen : Sanaa : Djibouti-Djibouti : Pitcairn-Adamstown
Zambia : Lusaka : Zimbabwe-Harare : Nauru-Yaren
Zimbabwe : Harare : Zambia-Lusaka : Nauru-Yaren

In [11]:
ofname = 'myoutput.csv'
with open(ofname, 'wb') as f:
    writer = csv.writer(f)
    for row in cf:
        writer.writerow(row)

For Pakistan

Let's see what capitals are the closest and farthest from Islamabad.

In [12]:
countries = [d[0].lower() for d in data]
In [13]:
pak_idx = countries.index('pakistan')
In [14]:
cf[pak_idx]
Out[14]:
['Pakistan', 'Islamabad', 'Afghanistan-Kabul', 'Pitcairn-Adamstown']

Which two capitals are the farthest apart from each other?

In [15]:
(f1,f2) = np.unravel_index(np.nanargmax(distances),distances.shape)
In [16]:
'-'.join(data[f1][:2]),'-'.join(data[f2][:2])
Out[16]:
('Paraguay-Asunci\xf3n', 'Taiwan-Taipei')

Which two capitals are the closest to each other?

In [17]:
(c1,c2) = np.unravel_index(np.nanargmin(distances),distances.shape)
In [18]:
'-'.join(data[c1][:2]),'-'.join(data[c2][:2])
Out[18]:
('Israel-Jerusalem', 'Palestinian Territory-East Jerusalem')

Which capital is the most isolated?

In [19]:
'-'.join(data[np.argmax(np.nansum(distances,axis=0))][:2]) # Capital with the maximum (avg/total) distance
Out[19]:
'New Zealand-Wellington'
In [20]:
'-'.join(data[np.argmax(np.nanmin(distances,axis=0))][:2]) # Alternate interpretation -- the capital closest city from which is farthest
Out[20]:
'Netherlands Antilles-Willemstad'
In [21]:
from collections import Counter
count_fi = Counter(fi)
idx = count_fi.keys()[np.argmax(count_fi.values())]
'-'.join(data[idx][:2]) # Alternate interpretation
Out[21]:
'New Zealand-Wellington'

Which capital is the most closest to all other capitals?

In [22]:
'-'.join(data[np.argmin(np.nansum(distances,axis=0))][:2]) # Capital with the minimum (avg/total) distance
Out[22]:
'Bosnia and Herzegovina-Sarajevo'
In [23]:
'-'.join(data[np.argmin(np.nanmax(distances,axis=0))][:2]) # Alternate interpretation -- the capital farthest city from which is closest
Out[23]:
'Canada-Ottawa'
In [24]:
count_ci = Counter(ci) # Alternate interpretation - most frequent closest
idx = count_ci.keys()[np.argmax(count_ci.values())]
'-'.join(data[idx][:2]) 
Out[24]:
'Argentina-Buenos Aires'

Which capital is the most equally distant from all other countries?

In [25]:
'-'.join(data[np.argmin(np.nanstd(distances,axis=0))][:2])
Out[25]:
'Netherlands Antilles-Willemstad'

Hope you enjoyed the assignment!!

(C) Python-PIEAS (2015) by Dr. Fayyaz Minhas, DCIS, PIEAS, Islamabad, Pakistan.