As Github does not support folium map, so if you want to see fully rendered notebook, click on this link https://nbviewer.jupyter.org/github/Mr-Piyush-Kumar/Data_Science_Projects/blob/master/Toronto_City_Neighborhood_Clustring/TorrontoCityNeighborhoodClustring.ipynb
This Notebook is the part of IBM Data Science Capastone Project. In This project, I am going to explore the nearby venues of Toronto City and after I will use machine learning to create clusters of these neighborhoods and will show all these clusters in the map of Toronto City.
Displaying Toronto City Neighborhoods dataset after scrapping and cleaning from wikipedia a page.
# Importing libraries.
import pandas as pd
import numpy as np
# This line of code will fetch all the tables in this 'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M'
# wikipedia page.
tabels = pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')
len(tabels)
3
As we can see there are three tabels. Lets check which table is reqiured for us.
# 1st Table,
# NOTE:- each element in tabels list is a DataFrame.
tabels[0].head()
Postcode | Borough | Neighbourhood | |
---|---|---|---|
0 | M1A | Not assigned | Not assigned |
1 | M2A | Not assigned | Not assigned |
2 | M3A | North York | Parkwoods |
3 | M4A | North York | Victoria Village |
4 | M5A | Downtown Toronto | Harbourfront |
As we can see this is the table which we reqiured, so there is no need to check other tabels.
# Storing tabel 1 in a saparate DataFrame.
Toronto_df = tabels[0]
del(tabels) # deleting tables list, as we don't required it anymore.
Toronto_df.head()
Postcode | Borough | Neighbourhood | |
---|---|---|---|
0 | M1A | Not assigned | Not assigned |
1 | M2A | Not assigned | Not assigned |
2 | M3A | North York | Parkwoods |
3 | M4A | North York | Victoria Village |
4 | M5A | Downtown Toronto | Harbourfront |
# Data Wrangling # Data Preprocessing
# 1- Changing DataFrame's columns name according to project instructions
Toronto_df.columns = ['PostalCode','Borough','Neighborhood']
# 2- Removing those rows whoose Borough is Not assigned
Toronto_df = Toronto_df[Toronto_df['Borough']!='Not assigned']
# 3- Grouping of Postal Code
temp_lst = []
for name, group in Toronto_df.groupby('PostalCode'):
temp_lst.append([name, group['Borough'].unique()[0],", ".join(set(group['Neighborhood'].values))])
Toronto_df = pd.DataFrame(temp_lst, columns = ['PostalCode','Borough','Neighborhood'])
# 4- Replacing Not assigned values in Neighborhood with corresponding Borough
index = Toronto_df['Neighborhood'][Toronto_df['Neighborhood'].apply(lambda x: 'Not assigned' in str(x)) == True].index
# index where neighborhood is Not assigned
code = Toronto_df.iloc[index]['PostalCode'].values[0] # postal code where neighborhood is Not assigned
Toronto_df['Neighborhood'][Toronto_df['PostalCode']==code] = Toronto_df['Borough'] # replacing
Toronto_df
PostalCode | Borough | Neighborhood | |
---|---|---|---|
0 | M1B | Scarborough | Rouge, Malvern |
1 | M1C | Scarborough | Rouge Hill, Highland Creek, Port Union |
2 | M1E | Scarborough | Guildwood, Morningside, West Hill |
3 | M1G | Scarborough | Woburn |
4 | M1H | Scarborough | Cedarbrae |
5 | M1J | Scarborough | Scarborough Village |
6 | M1K | Scarborough | Kennedy Park, East Birchmount Park, Ionview |
7 | M1L | Scarborough | Oakridge, Clairlea, Golden Mile |
8 | M1M | Scarborough | Scarborough Village West, Cliffcrest, Cliffside |
9 | M1N | Scarborough | Cliffside West, Birch Cliff |
10 | M1P | Scarborough | Dorset Park, Wexford Heights, Scarborough Town... |
11 | M1R | Scarborough | Wexford, Maryvale |
12 | M1S | Scarborough | Agincourt |
13 | M1T | Scarborough | Tam O'Shanter, Clarks Corners, Sullivan |
14 | M1V | Scarborough | Steeles East, L'Amoreaux East, Milliken, Aginc... |
15 | M1W | Scarborough | L'Amoreaux West |
16 | M1X | Scarborough | Upper Rouge |
17 | M2H | North York | Hillcrest Village |
18 | M2J | North York | Oriole, Fairview, Henry Farm |
19 | M2K | North York | Bayview Village |
20 | M2L | North York | Silver Hills, York Mills |
21 | M2M | North York | Willowdale, Newtonbrook |
22 | M2N | North York | Willowdale South |
23 | M2P | North York | York Mills West |
24 | M2R | North York | Willowdale West |
25 | M3A | North York | Parkwoods |
26 | M3B | North York | Don Mills North |
27 | M3C | North York | Flemingdon Park, Don Mills South |
28 | M3H | North York | Downsview North, Wilson Heights, Bathurst Manor |
29 | M3J | North York | Northwood Park, York University |
... | ... | ... | ... |
73 | M6C | York | Humewood-Cedarvale |
74 | M6E | York | Caledonia-Fairbanks |
75 | M6G | Downtown Toronto | Christie |
76 | M6H | West Toronto | Dovercourt Village, Dufferin |
77 | M6J | West Toronto | Trinity, Little Portugal |
78 | M6K | West Toronto | Parkdale Village, Brockton, Exhibition Place |
79 | M6L | North York | Downsview, North Park, Upwood Park |
80 | M6M | York | Silverthorn, Mount Dennis, Keelesdale, Del Ray |
81 | M6N | York | The Junction North, Runnymede |
82 | M6P | West Toronto | The Junction South, High Park |
83 | M6R | West Toronto | Parkdale, Roncesvalles |
84 | M6S | West Toronto | Swansea, Runnymede |
85 | M7A | Downtown Toronto | Queen's Park |
86 | M7R | Mississauga | Canada Post Gateway Processing Centre |
87 | M7Y | East Toronto | Business Reply Mail Processing Centre 969 Eastern |
88 | M8V | Etobicoke | New Toronto, Mimico South, Humber Bay Shores |
89 | M8W | Etobicoke | Alderwood, Long Branch |
90 | M8X | Etobicoke | The Kingsway, Old Mill North, Montgomery Road |
91 | M8Y | Etobicoke | Mimico NE, Royal York South East, Sunnylea, Ki... |
92 | M8Z | Etobicoke | Royal York South West, Mimico NW, The Queenswa... |
93 | M9A | Queen's Park | Queen's Park |
94 | M9B | Etobicoke | West Deane Park, Cloverdale, Martin Grove, Pri... |
95 | M9C | Etobicoke | Bloordale Gardens, Old Burnhamthorpe, Eringate... |
96 | M9L | North York | Humber Summit |
97 | M9M | North York | Humberlea, Emery |
98 | M9N | York | Weston |
99 | M9P | Etobicoke | Westmount |
100 | M9R | Etobicoke | Martin Grove Gardens, Kingsview Village, St. P... |
101 | M9V | Etobicoke | Albion Gardens, Mount Olive, Jamestown, Beaumo... |
102 | M9W | Etobicoke | Northwest |
103 rows × 3 columns
print('No. of rows in Toronto Data Frame are ',Toronto_df.shape[0],'.')
No. of rows in Toronto Data Frame are 103 .
Getting geographical co-ordinates of each neighborhood.
coordinates_data = pd.read_csv('http://cocl.us/Geospatial_data') # Downloading coordinates data.
coordinates_data.columns = ['PostalCode','Latitude','Longitude']
coordinates_data.head()
PostalCode | Latitude | Longitude | |
---|---|---|---|
0 | M1B | 43.806686 | -79.194353 |
1 | M1C | 43.784535 | -79.160497 |
2 | M1E | 43.763573 | -79.188711 |
3 | M1G | 43.770992 | -79.216917 |
4 | M1H | 43.773136 | -79.239476 |
# merging Toronto_df and coordinates_data DataFrame together
Toronto_df = Toronto_df.merge(coordinates_data, how='left',on='PostalCode')
Toronto_df
PostalCode | Borough | Neighborhood | Latitude | Longitude | |
---|---|---|---|---|---|
0 | M1B | Scarborough | Rouge, Malvern | 43.806686 | -79.194353 |
1 | M1C | Scarborough | Rouge Hill, Highland Creek, Port Union | 43.784535 | -79.160497 |
2 | M1E | Scarborough | Guildwood, Morningside, West Hill | 43.763573 | -79.188711 |
3 | M1G | Scarborough | Woburn | 43.770992 | -79.216917 |
4 | M1H | Scarborough | Cedarbrae | 43.773136 | -79.239476 |
5 | M1J | Scarborough | Scarborough Village | 43.744734 | -79.239476 |
6 | M1K | Scarborough | Kennedy Park, East Birchmount Park, Ionview | 43.727929 | -79.262029 |
7 | M1L | Scarborough | Oakridge, Clairlea, Golden Mile | 43.711112 | -79.284577 |
8 | M1M | Scarborough | Scarborough Village West, Cliffcrest, Cliffside | 43.716316 | -79.239476 |
9 | M1N | Scarborough | Cliffside West, Birch Cliff | 43.692657 | -79.264848 |
10 | M1P | Scarborough | Dorset Park, Wexford Heights, Scarborough Town... | 43.757410 | -79.273304 |
11 | M1R | Scarborough | Wexford, Maryvale | 43.750072 | -79.295849 |
12 | M1S | Scarborough | Agincourt | 43.794200 | -79.262029 |
13 | M1T | Scarborough | Tam O'Shanter, Clarks Corners, Sullivan | 43.781638 | -79.304302 |
14 | M1V | Scarborough | Steeles East, L'Amoreaux East, Milliken, Aginc... | 43.815252 | -79.284577 |
15 | M1W | Scarborough | L'Amoreaux West | 43.799525 | -79.318389 |
16 | M1X | Scarborough | Upper Rouge | 43.836125 | -79.205636 |
17 | M2H | North York | Hillcrest Village | 43.803762 | -79.363452 |
18 | M2J | North York | Oriole, Fairview, Henry Farm | 43.778517 | -79.346556 |
19 | M2K | North York | Bayview Village | 43.786947 | -79.385975 |
20 | M2L | North York | Silver Hills, York Mills | 43.757490 | -79.374714 |
21 | M2M | North York | Willowdale, Newtonbrook | 43.789053 | -79.408493 |
22 | M2N | North York | Willowdale South | 43.770120 | -79.408493 |
23 | M2P | North York | York Mills West | 43.752758 | -79.400049 |
24 | M2R | North York | Willowdale West | 43.782736 | -79.442259 |
25 | M3A | North York | Parkwoods | 43.753259 | -79.329656 |
26 | M3B | North York | Don Mills North | 43.745906 | -79.352188 |
27 | M3C | North York | Flemingdon Park, Don Mills South | 43.725900 | -79.340923 |
28 | M3H | North York | Downsview North, Wilson Heights, Bathurst Manor | 43.754328 | -79.442259 |
29 | M3J | North York | Northwood Park, York University | 43.767980 | -79.487262 |
... | ... | ... | ... | ... | ... |
73 | M6C | York | Humewood-Cedarvale | 43.693781 | -79.428191 |
74 | M6E | York | Caledonia-Fairbanks | 43.689026 | -79.453512 |
75 | M6G | Downtown Toronto | Christie | 43.669542 | -79.422564 |
76 | M6H | West Toronto | Dovercourt Village, Dufferin | 43.669005 | -79.442259 |
77 | M6J | West Toronto | Trinity, Little Portugal | 43.647927 | -79.419750 |
78 | M6K | West Toronto | Parkdale Village, Brockton, Exhibition Place | 43.636847 | -79.428191 |
79 | M6L | North York | Downsview, North Park, Upwood Park | 43.713756 | -79.490074 |
80 | M6M | York | Silverthorn, Mount Dennis, Keelesdale, Del Ray | 43.691116 | -79.476013 |
81 | M6N | York | The Junction North, Runnymede | 43.673185 | -79.487262 |
82 | M6P | West Toronto | The Junction South, High Park | 43.661608 | -79.464763 |
83 | M6R | West Toronto | Parkdale, Roncesvalles | 43.648960 | -79.456325 |
84 | M6S | West Toronto | Swansea, Runnymede | 43.651571 | -79.484450 |
85 | M7A | Downtown Toronto | Queen's Park | 43.662301 | -79.389494 |
86 | M7R | Mississauga | Canada Post Gateway Processing Centre | 43.636966 | -79.615819 |
87 | M7Y | East Toronto | Business Reply Mail Processing Centre 969 Eastern | 43.662744 | -79.321558 |
88 | M8V | Etobicoke | New Toronto, Mimico South, Humber Bay Shores | 43.605647 | -79.501321 |
89 | M8W | Etobicoke | Alderwood, Long Branch | 43.602414 | -79.543484 |
90 | M8X | Etobicoke | The Kingsway, Old Mill North, Montgomery Road | 43.653654 | -79.506944 |
91 | M8Y | Etobicoke | Mimico NE, Royal York South East, Sunnylea, Ki... | 43.636258 | -79.498509 |
92 | M8Z | Etobicoke | Royal York South West, Mimico NW, The Queenswa... | 43.628841 | -79.520999 |
93 | M9A | Queen's Park | Queen's Park | 43.667856 | -79.532242 |
94 | M9B | Etobicoke | West Deane Park, Cloverdale, Martin Grove, Pri... | 43.650943 | -79.554724 |
95 | M9C | Etobicoke | Bloordale Gardens, Old Burnhamthorpe, Eringate... | 43.643515 | -79.577201 |
96 | M9L | North York | Humber Summit | 43.756303 | -79.565963 |
97 | M9M | North York | Humberlea, Emery | 43.724766 | -79.532242 |
98 | M9N | York | Weston | 43.706876 | -79.518188 |
99 | M9P | Etobicoke | Westmount | 43.696319 | -79.532242 |
100 | M9R | Etobicoke | Martin Grove Gardens, Kingsview Village, St. P... | 43.688905 | -79.554724 |
101 | M9V | Etobicoke | Albion Gardens, Mount Olive, Jamestown, Beaumo... | 43.739416 | -79.588437 |
102 | M9W | Etobicoke | Northwest | 43.706748 | -79.594054 |
103 rows × 5 columns
Exploring and clustering the neighborhoods in Toronto for only those boroughs that contains word Toronto in its name.
# Getting Latitude and Longitude of Toronto City.
!conda install -c conda-forge geopy --yes # Installing geopy library, this library helps in getting Latitude and Longitude of a given address.
from geopy.geocoders import Nominatim # Nominatim converts an address into latitude and longitude values.
Solving environment: done ## Package Plan ## environment location: /opt/conda/envs/Python36 added / updated specs: - geopy The following packages will be downloaded: package | build ---------------------------|----------------- ca-certificates-2019.11.28 | hecc5488_0 145 KB conda-forge certifi-2019.11.28 | py36_0 149 KB conda-forge openssl-1.1.1d | h516909a_0 2.1 MB conda-forge geographiclib-1.50 | py_0 34 KB conda-forge geopy-1.21.0 | py_0 58 KB conda-forge ------------------------------------------------------------ Total: 2.5 MB The following NEW packages will be INSTALLED: geographiclib: 1.50-py_0 conda-forge geopy: 1.21.0-py_0 conda-forge The following packages will be UPDATED: ca-certificates: 2019.11.27-0 --> 2019.11.28-hecc5488_0 conda-forge certifi: 2019.11.28-py36_0 --> 2019.11.28-py36_0 conda-forge The following packages will be DOWNGRADED: openssl: 1.1.1d-h7b6447c_3 --> 1.1.1d-h516909a_0 conda-forge Downloading and Extracting Packages ca-certificates-2019 | 145 KB | ##################################### | 100% certifi-2019.11.28 | 149 KB | ##################################### | 100% openssl-1.1.1d | 2.1 MB | ##################################### | 100% geographiclib-1.50 | 34 KB | ##################################### | 100% geopy-1.21.0 | 58 KB | ##################################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done
address = 'Toronto, CA'
geolocator = Nominatim(user_agent="Toronto_explorer")
To_location = geolocator.geocode(address)
To_latitude = To_location.latitude
To_longitude = To_location.longitude
print('The geograpical coordinate of Toronto City are {}, {}.'.format(To_latitude, To_longitude))
The geograpical coordinate of Toronto City are 43.653963, -79.387207.
!conda install -c conda-forge folium=0.5.0 --yes # Installing Folium Library
import folium # map rendering library
Solving environment: done ## Package Plan ## environment location: /opt/conda/envs/Python36 added / updated specs: - folium=0.5.0 The following packages will be downloaded: package | build ---------------------------|----------------- altair-4.0.1 | py_0 575 KB conda-forge vincent-0.4.4 | py_1 28 KB conda-forge branca-0.3.1 | py_0 25 KB conda-forge folium-0.5.0 | py_0 45 KB conda-forge ------------------------------------------------------------ Total: 673 KB The following NEW packages will be INSTALLED: altair: 4.0.1-py_0 conda-forge branca: 0.3.1-py_0 conda-forge folium: 0.5.0-py_0 conda-forge vincent: 0.4.4-py_1 conda-forge Downloading and Extracting Packages altair-4.0.1 | 575 KB | ##################################### | 100% vincent-0.4.4 | 28 KB | ##################################### | 100% branca-0.3.1 | 25 KB | ##################################### | 100% folium-0.5.0 | 45 KB | ##################################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done
Toronto_map = folium.Map(location=[To_latitude,To_longitude], zoom_start = 11)
# add markers to map
for lat, lng, borough, neighborhood in zip(Toronto_df['Latitude'], Toronto_df['Longitude'], Toronto_df['Borough'], Toronto_df['Neighborhood']):
label = '{}, {}'.format(neighborhood, borough)
label = folium.Popup(label, parse_html=True)
folium.CircleMarker(
[lat, lng],
radius=5,
popup=label,
color='blue',
fill=True,
fill_color='#3186cc',
fill_opacity=0.7,
parse_html=False).add_to(Toronto_map)
Toronto_map
As per the objective I have to show only those neighborhoods whoose borough name contain Toronto word
# Filtering Data Set, Considering only those rows where column Borough contains Toronto Word.
New_Toronto_df = Toronto_df[Toronto_df['Borough'].apply(lambda x: 'Toronto' in str(x))]
New_Toronto_df.head()
PostalCode | Borough | Neighborhood | Latitude | Longitude | |
---|---|---|---|---|---|
37 | M4E | East Toronto | The Beaches | 43.676357 | -79.293031 |
41 | M4K | East Toronto | Riverdale, The Danforth West | 43.679557 | -79.352188 |
42 | M4L | East Toronto | The Beaches West, India Bazaar | 43.668999 | -79.315572 |
43 | M4M | East Toronto | Studio District | 43.659526 | -79.340923 |
44 | M4N | Central Toronto | Lawrence Park | 43.728020 | -79.388790 |
# list of unique Borough in New Data set
Borough_lst = New_Toronto_df.Borough.unique().tolist()
Borough_lst
['East Toronto', 'Central Toronto', 'Downtown Toronto', 'West Toronto']
Toronto_Map = folium.Map(location=[To_latitude,To_longitude],zoom_start=11)
for lat,lon,borough,neighborhood in zip(New_Toronto_df['Latitude'],New_Toronto_df['Longitude'],New_Toronto_df['Borough'],New_Toronto_df['Neighborhood']):
label = '{}, {}'.format(neighborhood, borough)
label = folium.Popup(label,parse_html=True)
folium.CircleMarker(
[lat,lon],
radius=5,
popup=label,
color='blue',
fill=True,
fill_color='#3186cc',
fill_opacity=0.7,
parse_html=False).add_to(Toronto_Map)
Toronto_Map
Next, we are going to start utilizing the Foursquare API to explore the neighborhoods and segment them.
CLIENT_ID = 'MUINF3SJELTWX0T2R3GWA5P5R3QYAGI2PDFGFR0HCERWTFNH' # my Foursquare ID
CLIENT_SECRET = 'TNBO5TIGKMZR0RR1ARMSUHBMPJ2V0JZBNZQ2G2220FAMS05U' # my Foursquare Secret
VERSION = '20180605' # Foursquare API version
print('My credentails:')
print('CLIENT_ID: ' + CLIENT_ID)
print('CLIENT_SECRET:' + CLIENT_SECRET)
My credentails: CLIENT_ID: MUINF3SJELTWX0T2R3GWA5P5R3QYAGI2PDFGFR0HCERWTFNH CLIENT_SECRET:TNBO5TIGKMZR0RR1ARMSUHBMPJ2V0JZBNZQ2G2220FAMS05U
Get the neighborhood's name.
New_Toronto_df = New_Toronto_df.reset_index(drop=True)
New_Toronto_df.loc[0, 'Neighborhood']
'The Beaches'
Get the latitude and longitude values of The Beaches.
Beaches_latitude = New_Toronto_df.loc[0, 'Latitude'] # neighborhood latitude value
Beaches_longitude = New_Toronto_df.loc[0, 'Longitude'] # neighborhood longitude value
print('Latitude and longitude values of The Beaches are {}, {}.'.format(
Beaches_latitude,
Beaches_longitude))
Latitude and longitude values of The Beaches are 43.67635739999999, -79.2930312.
# First, let's create the GET request URL. Name your URL url.
LIMIT = 100 # no. of venues
radius = 500
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
Beaches_latitude,
Beaches_longitude,
radius,
LIMIT)
url
'https://api.foursquare.com/v2/venues/explore?&client_id=MUINF3SJELTWX0T2R3GWA5P5R3QYAGI2PDFGFR0HCERWTFNH&client_secret=TNBO5TIGKMZR0RR1ARMSUHBMPJ2V0JZBNZQ2G2220FAMS05U&v=20180605&ll=43.67635739999999,-79.2930312&radius=500&limit=100'
Send the GET request and examine the resutls
import requests # importing request handling library
results = requests.get(url).json()
results
{'meta': {'code': 200, 'requestId': '5e38fe3e006dce001ce3e09a'}, 'response': {'headerLocation': 'The Beaches', 'headerFullLocation': 'The Beaches, Toronto', 'headerLocationGranularity': 'neighborhood', 'totalResults': 5, 'suggestedBounds': {'ne': {'lat': 43.680857404499996, 'lng': -79.28682091449052}, 'sw': {'lat': 43.67185739549999, 'lng': -79.29924148550948}}, 'groups': [{'type': 'Recommended Places', 'name': 'recommended', 'items': [{'reasons': {'count': 0, 'items': [{'summary': 'This spot is popular', 'type': 'general', 'reasonName': 'globalInteractionReason'}]}, 'venue': {'id': '4bd461bc77b29c74a07d9282', 'name': 'Glen Manor Ravine', 'location': {'address': 'Glen Manor', 'crossStreet': 'Queen St.', 'lat': 43.67682094413784, 'lng': -79.29394208780985, 'labeledLatLngs': [{'label': 'display', 'lat': 43.67682094413784, 'lng': -79.29394208780985}], 'distance': 89, 'cc': 'CA', 'city': 'Toronto', 'state': 'ON', 'country': 'Canada', 'formattedAddress': ['Glen Manor (Queen St.)', 'Toronto ON', 'Canada']}, 'categories': [{'id': '4bf58dd8d48988d159941735', 'name': 'Trail', 'pluralName': 'Trails', 'shortName': 'Trail', 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/parks_outdoors/hikingtrail_', 'suffix': '.png'}, 'primary': True}], 'photos': {'count': 0, 'groups': []}}, 'referralId': 'e-0-4bd461bc77b29c74a07d9282-0'}, {'reasons': {'count': 0, 'items': [{'summary': 'This spot is popular', 'type': 'general', 'reasonName': 'globalInteractionReason'}]}, 'venue': {'id': '4ad4c062f964a52011f820e3', 'name': 'The Big Carrot Natural Food Market', 'location': {'address': '125 Southwood Dr', 'lat': 43.678879, 'lng': -79.297734, 'labeledLatLngs': [{'label': 'display', 'lat': 43.678879, 'lng': -79.297734}], 'distance': 471, 'postalCode': 'M4E 0B8', 'cc': 'CA', 'city': 'Toronto', 'state': 'ON', 'country': 'Canada', 'formattedAddress': ['125 Southwood Dr', 'Toronto ON M4E 0B8', 'Canada']}, 'categories': [{'id': '50aa9e744b90af0d42d5de0e', 'name': 'Health Food Store', 'pluralName': 'Health Food Stores', 'shortName': 'Health Food Store', 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/food_grocery_', 'suffix': '.png'}, 'primary': True}], 'photos': {'count': 0, 'groups': []}, 'venuePage': {'id': '75150878'}}, 'referralId': 'e-0-4ad4c062f964a52011f820e3-1'}, {'reasons': {'count': 0, 'items': [{'summary': 'This spot is popular', 'type': 'general', 'reasonName': 'globalInteractionReason'}]}, 'venue': {'id': '4b8daea1f964a520480833e3', 'name': 'Grover Pub and Grub', 'location': {'address': '676 Kingston Rd.', 'crossStreet': 'at Main St.', 'lat': 43.679181434941015, 'lng': -79.29721535878515, 'labeledLatLngs': [{'label': 'display', 'lat': 43.679181434941015, 'lng': -79.29721535878515}], 'distance': 460, 'postalCode': 'M4E 1R4', 'cc': 'CA', 'city': 'Toronto', 'state': 'ON', 'country': 'Canada', 'formattedAddress': ['676 Kingston Rd. (at Main St.)', 'Toronto ON M4E 1R4', 'Canada']}, 'categories': [{'id': '4bf58dd8d48988d11b941735', 'name': 'Pub', 'pluralName': 'Pubs', 'shortName': 'Pub', 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/nightlife/pub_', 'suffix': '.png'}, 'primary': True}], 'photos': {'count': 0, 'groups': []}}, 'referralId': 'e-0-4b8daea1f964a520480833e3-2'}, {'reasons': {'count': 0, 'items': [{'summary': 'This spot is popular', 'type': 'general', 'reasonName': 'globalInteractionReason'}]}, 'venue': {'id': '4df91c4bae60f95f82229ad5', 'name': 'Upper Beaches', 'location': {'lat': 43.68056321147582, 'lng': -79.2928688743688, 'labeledLatLngs': [{'label': 'display', 'lat': 43.68056321147582, 'lng': -79.2928688743688}], 'distance': 468, 'cc': 'CA', 'city': 'Toronto', 'state': 'ON', 'country': 'Canada', 'formattedAddress': ['Toronto ON', 'Canada']}, 'categories': [{'id': '4f2a25ac4b909258e854f55f', 'name': 'Neighborhood', 'pluralName': 'Neighborhoods', 'shortName': 'Neighborhood', 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_', 'suffix': '.png'}, 'primary': True}], 'photos': {'count': 0, 'groups': []}}, 'referralId': 'e-0-4df91c4bae60f95f82229ad5-3'}, {'reasons': {'count': 0, 'items': [{'summary': 'This spot is popular', 'type': 'general', 'reasonName': 'globalInteractionReason'}]}, 'venue': {'id': '4dbc53541e72b351caafda72', 'name': 'Seaspray Restaurant', 'location': {'address': '629 Kingston Rd', 'crossStreet': 'at Main St', 'lat': 43.678887885937094, 'lng': -79.29816674246032, 'labeledLatLngs': [{'label': 'display', 'lat': 43.678887885937094, 'lng': -79.29816674246032}], 'distance': 500, 'cc': 'CA', 'city': 'Toronto', 'state': 'ON', 'country': 'Canada', 'formattedAddress': ['629 Kingston Rd (at Main St)', 'Toronto ON', 'Canada']}, 'categories': [{'id': '4bf58dd8d48988d142941735', 'name': 'Asian Restaurant', 'pluralName': 'Asian Restaurants', 'shortName': 'Asian', 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/asian_', 'suffix': '.png'}, 'primary': True}], 'photos': {'count': 0, 'groups': []}}, 'referralId': 'e-0-4dbc53541e72b351caafda72-4'}]}]}}
# function that extracts the category of the venue
def get_category_type(row):
try:
categories_list = row['categories']
except:
categories_list = row['venue.categories']
if len(categories_list) == 0:
return None
else:
return categories_list[0]['name']
Now we are ready to clean the json and structure it into a pandas dataframe.
from pandas.io.json import json_normalize # tranform JSON file into a pandas dataframe
venues = results['response']['groups'][0]['items']
nearby_venues = json_normalize(venues) # flatten JSON
# filter columns
filtered_columns = ['venue.name', 'venue.categories', 'venue.location.lat', 'venue.location.lng']
nearby_venues =nearby_venues.loc[:, filtered_columns]
# filter the category for each row
nearby_venues['venue.categories'] = nearby_venues.apply(get_category_type, axis=1)
# clean columns
nearby_venues.columns = [col.split(".")[-1] for col in nearby_venues.columns]
nearby_venues.head()
name | categories | lat | lng | |
---|---|---|---|---|
0 | Glen Manor Ravine | Trail | 43.676821 | -79.293942 |
1 | The Big Carrot Natural Food Market | Health Food Store | 43.678879 | -79.297734 |
2 | Grover Pub and Grub | Pub | 43.679181 | -79.297215 |
3 | Upper Beaches | Neighborhood | 43.680563 | -79.292869 |
4 | Seaspray Restaurant | Asian Restaurant | 43.678888 | -79.298167 |
And how many venues were returned by Foursquare?
print('{} venues were returned by Foursquare.'.format(nearby_venues.shape[0]))
5 venues were returned by Foursquare.
Let's create a function to repeat the same process to all the neighborhoods in Manhattan
def getNearbyVenues(names, latitudes, longitudes, radius=500):
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
print('Processing ',name,'.....')
# create the API request URL
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
lng,
radius,
LIMIT)
# make the GET request
results = requests.get(url).json()["response"]['groups'][0]['items']
# return only relevant information for each nearby venue
venues_list.append([(
name,
lat,
lng,
v['venue']['name'],
v['venue']['location']['lat'],
v['venue']['location']['lng'],
v['venue']['categories'][0]['name']) for v in results])
nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = ['Neighborhood',
'Neighborhood Latitude',
'Neighborhood Longitude',
'Venue',
'Venue Latitude',
'Venue Longitude',
'Venue Category']
return(nearby_venues)
Filtered_Toronto_Venues = getNearbyVenues(names=New_Toronto_df['Neighborhood'],
latitudes=New_Toronto_df['Latitude'],
longitudes=New_Toronto_df['Longitude']
)
Filtered_Toronto_Venues.head()
Processing The Beaches ..... Processing Riverdale, The Danforth West ..... Processing The Beaches West, India Bazaar ..... Processing Studio District ..... Processing Lawrence Park ..... Processing Davisville North ..... Processing North Toronto West ..... Processing Davisville ..... Processing Summerhill East, Moore Park ..... Processing South Hill, Summerhill West, Rathnelly, Forest Hill SE, Deer Park ..... Processing Rosedale ..... Processing St. James Town, Cabbagetown ..... Processing Church and Wellesley ..... Processing Harbourfront ..... Processing Ryerson, Garden District ..... Processing St. James Town ..... Processing Berczy Park ..... Processing Central Bay Street ..... Processing Richmond, Adelaide, King ..... Processing Toronto Islands, Harbourfront East, Union Station ..... Processing Design Exchange, Toronto Dominion Centre ..... Processing Victoria Hotel, Commerce Court ..... Processing Roselawn ..... Processing Forest Hill West, Forest Hill North ..... Processing The Annex, North Midtown, Yorkville ..... Processing Harbord, University of Toronto ..... Processing Chinatown, Kensington Market, Grange Park ..... Processing Harbourfront West, CN Tower, Island airport, Railway Lands, Bathurst Quay, South Niagara, King and Spadina ..... Processing Stn A PO Boxes 25 The Esplanade ..... Processing First Canadian Place, Underground city ..... Processing Christie ..... Processing Dovercourt Village, Dufferin ..... Processing Trinity, Little Portugal ..... Processing Parkdale Village, Brockton, Exhibition Place ..... Processing The Junction South, High Park ..... Processing Parkdale, Roncesvalles ..... Processing Swansea, Runnymede ..... Processing Queen's Park ..... Processing Business Reply Mail Processing Centre 969 Eastern .....
Neighborhood | Neighborhood Latitude | Neighborhood Longitude | Venue | Venue Latitude | Venue Longitude | Venue Category | |
---|---|---|---|---|---|---|---|
0 | The Beaches | 43.676357 | -79.293031 | Glen Manor Ravine | 43.676821 | -79.293942 | Trail |
1 | The Beaches | 43.676357 | -79.293031 | The Big Carrot Natural Food Market | 43.678879 | -79.297734 | Health Food Store |
2 | The Beaches | 43.676357 | -79.293031 | Grover Pub and Grub | 43.679181 | -79.297215 | Pub |
3 | The Beaches | 43.676357 | -79.293031 | Upper Beaches | 43.680563 | -79.292869 | Neighborhood |
4 | The Beaches | 43.676357 | -79.293031 | Seaspray Restaurant | 43.678888 | -79.298167 | Asian Restaurant |
print(Filtered_Toronto_Venues.shape)
(1714, 7)
Let's check how many venues were returned for each neighborhood
Filtered_Toronto_Venues.groupby('Neighborhood').count().iloc[:,0]
Neighborhood Berczy Park 56 Business Reply Mail Processing Centre 969 Eastern 16 Central Bay Street 83 Chinatown, Kensington Market, Grange Park 87 Christie 19 Church and Wellesley 82 Davisville 32 Davisville North 8 Design Exchange, Toronto Dominion Centre 100 Dovercourt Village, Dufferin 16 First Canadian Place, Underground city 100 Forest Hill West, Forest Hill North 4 Harbord, University of Toronto 37 Harbourfront 47 Harbourfront West, CN Tower, Island airport, Railway Lands, Bathurst Quay, South Niagara, King and Spadina 16 Lawrence Park 3 North Toronto West 21 Parkdale Village, Brockton, Exhibition Place 23 Parkdale, Roncesvalles 14 Queen's Park 39 Richmond, Adelaide, King 100 Riverdale, The Danforth West 42 Rosedale 4 Roselawn 3 Ryerson, Garden District 100 South Hill, Summerhill West, Rathnelly, Forest Hill SE, Deer Park 15 St. James Town 100 St. James Town, Cabbagetown 47 Stn A PO Boxes 25 The Esplanade 95 Studio District 42 Summerhill East, Moore Park 1 Swansea, Runnymede 40 The Annex, North Midtown, Yorkville 22 The Beaches 5 The Beaches West, India Bazaar 18 The Junction South, High Park 24 Toronto Islands, Harbourfront East, Union Station 100 Trinity, Little Portugal 53 Victoria Hotel, Commerce Court 100 Name: Neighborhood Latitude, dtype: int64
print('There are {} uniques categories.'.format(len(Filtered_Toronto_Venues['Venue Category'].unique())))
There are 230 uniques categories.
# one hot encoding
Toronto_onehot = pd.get_dummies(Filtered_Toronto_Venues[['Venue Category']], prefix="", prefix_sep="")
# add neighborhood column back to dataframe
Toronto_onehot['Neighborhood'] = Filtered_Toronto_Venues['Neighborhood']
# move neighborhood column to the first column
fixed_columns = [Toronto_onehot.columns[-1]] + list(Toronto_onehot.columns[:-1])
Toronto_onehot = Toronto_onehot[fixed_columns]
Toronto_onehot.head()
Yoga Studio | Afghan Restaurant | Airport | Airport Food Court | Airport Gate | Airport Lounge | Airport Service | Airport Terminal | American Restaurant | Antique Shop | ... | Theater | Theme Restaurant | Toy / Game Store | Trail | Train Station | Vegetarian / Vegan Restaurant | Video Game Store | Vietnamese Restaurant | Wine Bar | Women's Store | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 rows × 230 columns
Next, let's group rows by neighborhood and by taking the mean of the frequency of occurrence of each category
Toronto_grouped = Toronto_onehot.groupby('Neighborhood').mean().reset_index()
Toronto_grouped
Neighborhood | Yoga Studio | Afghan Restaurant | Airport | Airport Food Court | Airport Gate | Airport Lounge | Airport Service | Airport Terminal | American Restaurant | ... | Theater | Theme Restaurant | Toy / Game Store | Trail | Train Station | Vegetarian / Vegan Restaurant | Video Game Store | Vietnamese Restaurant | Wine Bar | Women's Store | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Berczy Park | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.017857 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
1 | Business Reply Mail Processing Centre 969 Eastern | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
2 | Central Bay Street | 0.012048 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.012048 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.012048 | 0.000000 | 0.000000 | 0.012048 | 0.00 |
3 | Chinatown, Kensington Market, Grange Park | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.045977 | 0.000000 | 0.068966 | 0.011494 | 0.00 |
4 | Christie | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
5 | Church and Wellesley | 0.012195 | 0.012195 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.012195 | ... | 0.012195 | 0.012195 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
6 | Davisville | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.03125 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
7 | Davisville North | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
8 | Design Exchange, Toronto Dominion Centre | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.020000 | ... | 0.010000 | 0.000000 | 0.00000 | 0.00000 | 0.01 | 0.010000 | 0.000000 | 0.000000 | 0.010000 | 0.00 |
9 | Dovercourt Village, Dufferin | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
10 | First Canadian Place, Underground city | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.020000 | ... | 0.010000 | 0.000000 | 0.00000 | 0.00000 | 0.01 | 0.010000 | 0.000000 | 0.000000 | 0.010000 | 0.00 |
11 | Forest Hill West, Forest Hill North | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.25000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
12 | Harbord, University of Toronto | 0.027027 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.027027 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.027027 | 0.000000 | 0.000000 | 0.00 |
13 | Harbourfront | 0.021277 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.021277 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
14 | Harbourfront West, CN Tower, Island airport, R... | 0.000000 | 0.000000 | 0.0625 | 0.0625 | 0.0625 | 0.125 | 0.125 | 0.125 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
15 | Lawrence Park | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
16 | North Toronto West | 0.047619 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
17 | Parkdale Village, Brockton, Exhibition Place | 0.043478 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
18 | Parkdale, Roncesvalles | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
19 | Queen's Park | 0.025641 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.025641 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
20 | Richmond, Adelaide, King | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.020000 | ... | 0.010000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.020000 | 0.000000 | 0.000000 | 0.010000 | 0.01 |
21 | Riverdale, The Danforth West | 0.023810 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.023810 | ... | 0.000000 | 0.000000 | 0.00000 | 0.02381 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
22 | Rosedale | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.25000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
23 | Roselawn | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
24 | Ryerson, Garden District | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.010000 | ... | 0.020000 | 0.000000 | 0.01000 | 0.00000 | 0.00 | 0.000000 | 0.010000 | 0.010000 | 0.010000 | 0.00 |
25 | South Hill, Summerhill West, Rathnelly, Forest... | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.066667 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.066667 | 0.000000 | 0.00 |
26 | St. James Town | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.030000 | ... | 0.010000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.010000 | 0.000000 | 0.000000 | 0.010000 | 0.00 |
27 | St. James Town, Cabbagetown | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.021277 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
28 | Stn A PO Boxes 25 The Esplanade | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.010526 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
29 | Studio District | 0.023810 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.047619 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.023810 | 0.00 |
30 | Summerhill East, Moore Park | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
31 | Swansea, Runnymede | 0.025000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.025000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
32 | The Annex, North Midtown, Yorkville | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.045455 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.045455 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
33 | The Beaches | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.20000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
34 | The Beaches West, India Bazaar | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
35 | The Junction South, High Park | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.00 |
36 | Toronto Islands, Harbourfront East, Union Station | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.010000 | 0.000000 | 0.00000 | 0.00000 | 0.01 | 0.010000 | 0.000000 | 0.000000 | 0.010000 | 0.00 |
37 | Trinity, Little Portugal | 0.018868 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.000000 | ... | 0.018868 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.018868 | 0.000000 | 0.037736 | 0.018868 | 0.00 |
38 | Victoria Hotel, Commerce Court | 0.000000 | 0.000000 | 0.0000 | 0.0000 | 0.0000 | 0.000 | 0.000 | 0.000 | 0.030000 | ... | 0.000000 | 0.000000 | 0.00000 | 0.00000 | 0.00 | 0.020000 | 0.000000 | 0.000000 | 0.010000 | 0.00 |
39 rows × 230 columns
Toronto_grouped.shape
(39, 230)
num_top_venues = 5
for hood in Toronto_grouped['Neighborhood']:
print("----"+hood+"----")
temp = Toronto_grouped[Toronto_grouped['Neighborhood'] == hood].T.reset_index()
temp.columns = ['venue','freq']
temp = temp.iloc[1:]
temp['freq'] = temp['freq'].astype(float)
temp = temp.round({'freq': 2})
print(temp.sort_values('freq', ascending=False).reset_index(drop=True).head(num_top_venues))
print('\n')
----Berczy Park---- venue freq 0 Coffee Shop 0.09 1 Cocktail Bar 0.05 2 Café 0.04 3 Steakhouse 0.04 4 Beer Bar 0.04 ----Business Reply Mail Processing Centre 969 Eastern---- venue freq 0 Pizza Place 0.06 1 Auto Workshop 0.06 2 Comic Shop 0.06 3 Restaurant 0.06 4 Farmers Market 0.06 ----Central Bay Street---- venue freq 0 Coffee Shop 0.16 1 Café 0.05 2 Ice Cream Shop 0.05 3 Italian Restaurant 0.05 4 Sandwich Place 0.04 ----Chinatown, Kensington Market, Grange Park---- venue freq 0 Café 0.07 1 Vietnamese Restaurant 0.07 2 Chinese Restaurant 0.06 3 Bar 0.06 4 Vegetarian / Vegan Restaurant 0.05 ----Christie---- venue freq 0 Grocery Store 0.21 1 Café 0.16 2 Park 0.11 3 Baby Store 0.05 4 Nightclub 0.05 ----Church and Wellesley---- venue freq 0 Coffee Shop 0.07 1 Gay Bar 0.05 2 Sushi Restaurant 0.05 3 Japanese Restaurant 0.05 4 Restaurant 0.04 ----Davisville---- venue freq 0 Pizza Place 0.09 1 Sandwich Place 0.09 2 Dessert Shop 0.09 3 Italian Restaurant 0.06 4 Sushi Restaurant 0.06 ----Davisville North---- venue freq 0 Gym 0.12 1 Hotel 0.12 2 Dance Studio 0.12 3 Department Store 0.12 4 Sandwich Place 0.12 ----Design Exchange, Toronto Dominion Centre---- venue freq 0 Coffee Shop 0.12 1 Café 0.08 2 Hotel 0.07 3 Restaurant 0.04 4 Steakhouse 0.04 ----Dovercourt Village, Dufferin---- venue freq 0 Pharmacy 0.12 1 Bakery 0.12 2 Park 0.06 3 Music Venue 0.06 4 Recording Studio 0.06 ----First Canadian Place, Underground city---- venue freq 0 Coffee Shop 0.12 1 Café 0.07 2 Restaurant 0.04 3 Steakhouse 0.04 4 Hotel 0.03 ----Forest Hill West, Forest Hill North---- venue freq 0 Jewelry Store 0.25 1 Trail 0.25 2 Mexican Restaurant 0.25 3 Sushi Restaurant 0.25 4 Yoga Studio 0.00 ----Harbord, University of Toronto---- venue freq 0 Café 0.14 1 Bar 0.05 2 Bookstore 0.05 3 Japanese Restaurant 0.05 4 Bakery 0.05 ----Harbourfront---- venue freq 0 Coffee Shop 0.17 1 Park 0.06 2 Bakery 0.06 3 Pub 0.06 4 Café 0.06 ----Harbourfront West, CN Tower, Island airport, Railway Lands, Bathurst Quay, South Niagara, King and Spadina---- venue freq 0 Airport Lounge 0.12 1 Airport Service 0.12 2 Airport Terminal 0.12 3 Harbor / Marina 0.06 4 Bar 0.06 ----Lawrence Park---- venue freq 0 Park 0.33 1 Bus Line 0.33 2 Swim School 0.33 3 Yoga Studio 0.00 4 Monument / Landmark 0.00 ----North Toronto West---- venue freq 0 Clothing Store 0.19 1 Coffee Shop 0.10 2 Yoga Studio 0.05 3 Chinese Restaurant 0.05 4 Park 0.05 ----Parkdale Village, Brockton, Exhibition Place---- venue freq 0 Coffee Shop 0.09 1 Nightclub 0.09 2 Café 0.09 3 Breakfast Spot 0.09 4 Yoga Studio 0.04 ----Parkdale, Roncesvalles---- venue freq 0 Breakfast Spot 0.14 1 Gift Shop 0.14 2 Coffee Shop 0.07 3 Eastern European Restaurant 0.07 4 Movie Theater 0.07 ----Queen's Park---- venue freq 0 Coffee Shop 0.23 1 Park 0.05 2 Gym 0.05 3 Creperie 0.03 4 Burrito Place 0.03 ----Richmond, Adelaide, King---- venue freq 0 Coffee Shop 0.07 1 Café 0.04 2 Steakhouse 0.04 3 Bakery 0.03 4 Thai Restaurant 0.03 ----Riverdale, The Danforth West---- venue freq 0 Greek Restaurant 0.21 1 Coffee Shop 0.10 2 Italian Restaurant 0.07 3 Restaurant 0.05 4 Ice Cream Shop 0.05 ----Rosedale---- venue freq 0 Park 0.50 1 Playground 0.25 2 Trail 0.25 3 Yoga Studio 0.00 4 Movie Theater 0.00 ----Roselawn---- venue freq 0 Home Service 0.33 1 Garden 0.33 2 Ice Cream Shop 0.33 3 Yoga Studio 0.00 4 Movie Theater 0.00 ----Ryerson, Garden District---- venue freq 0 Coffee Shop 0.09 1 Clothing Store 0.08 2 Cosmetics Shop 0.03 3 Japanese Restaurant 0.03 4 Café 0.03 ----South Hill, Summerhill West, Rathnelly, Forest Hill SE, Deer Park---- venue freq 0 Pub 0.13 1 Coffee Shop 0.13 2 Pizza Place 0.07 3 Fried Chicken Joint 0.07 4 Sushi Restaurant 0.07 ----St. James Town---- venue freq 0 Coffee Shop 0.08 1 Café 0.06 2 Restaurant 0.05 3 American Restaurant 0.03 4 Bakery 0.03 ----St. James Town, Cabbagetown---- venue freq 0 Coffee Shop 0.11 1 Market 0.04 2 Italian Restaurant 0.04 3 Pizza Place 0.04 4 Pub 0.04 ----Stn A PO Boxes 25 The Esplanade---- venue freq 0 Coffee Shop 0.14 1 Café 0.04 2 Hotel 0.03 3 Restaurant 0.03 4 Italian Restaurant 0.03 ----Studio District---- venue freq 0 Café 0.10 1 Coffee Shop 0.07 2 Brewery 0.05 3 Bakery 0.05 4 American Restaurant 0.05 ----Summerhill East, Moore Park---- venue freq 0 Tennis Court 1.0 1 Yoga Studio 0.0 2 Movie Theater 0.0 3 Liquor Store 0.0 4 Lounge 0.0 ----Swansea, Runnymede---- venue freq 0 Pizza Place 0.08 1 Café 0.08 2 Coffee Shop 0.08 3 Italian Restaurant 0.05 4 Sushi Restaurant 0.05 ----The Annex, North Midtown, Yorkville---- venue freq 0 Sandwich Place 0.14 1 Café 0.14 2 Coffee Shop 0.09 3 Indian Restaurant 0.05 4 BBQ Joint 0.05 ----The Beaches---- venue freq 0 Asian Restaurant 0.2 1 Health Food Store 0.2 2 Pub 0.2 3 Trail 0.2 4 Yoga Studio 0.0 ----The Beaches West, India Bazaar---- venue freq 0 Gym 0.06 1 Sushi Restaurant 0.06 2 Pet Store 0.06 3 Pizza Place 0.06 4 Pub 0.06 ----The Junction South, High Park---- venue freq 0 Mexican Restaurant 0.08 1 Café 0.08 2 Bar 0.08 3 Thai Restaurant 0.08 4 Bakery 0.04 ----Toronto Islands, Harbourfront East, Union Station---- venue freq 0 Coffee Shop 0.12 1 Aquarium 0.05 2 Hotel 0.04 3 Italian Restaurant 0.04 4 Café 0.04 ----Trinity, Little Portugal---- venue freq 0 Bar 0.13 1 Restaurant 0.06 2 Asian Restaurant 0.06 3 Coffee Shop 0.06 4 Café 0.04 ----Victoria Hotel, Commerce Court---- venue freq 0 Coffee Shop 0.12 1 Café 0.07 2 Hotel 0.05 3 Restaurant 0.05 4 Italian Restaurant 0.03
First, let's write a function to sort the venues in descending order.
def return_most_common_venues(row, num_top_venues):
row_categories = row.iloc[1:]
row_categories_sorted = row_categories.sort_values(ascending=False)
return row_categories_sorted.index.values[0:num_top_venues]
Now let's create the new dataframe and display the top 10 venues for each neighborhood.
num_top_venues = 10
indicators = ['st', 'nd', 'rd']
# create columns according to number of top venues
columns = ['Neighborhood']
for ind in np.arange(num_top_venues):
try:
columns.append('{}{} Most Common Venue'.format(ind+1, indicators[ind]))
except:
columns.append('{}th Most Common Venue'.format(ind+1))
# create a new dataframe
neighborhoods_venues_sorted = pd.DataFrame(columns=columns)
neighborhoods_venues_sorted['Neighborhood'] = Toronto_grouped['Neighborhood']
for ind in np.arange(Toronto_grouped.shape[0]):
neighborhoods_venues_sorted.iloc[ind, 1:] = return_most_common_venues(Toronto_grouped.iloc[ind, :], num_top_venues)
neighborhoods_venues_sorted.head()
Neighborhood | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | Berczy Park | Coffee Shop | Cocktail Bar | Bakery | Cheese Shop | Beer Bar | Seafood Restaurant | Farmers Market | Steakhouse | Café | Gourmet Shop |
1 | Business Reply Mail Processing Centre 969 Eastern | Park | Auto Workshop | Comic Shop | Pizza Place | Burrito Place | Restaurant | Brewery | Light Rail Station | Smoke Shop | Farmers Market |
2 | Central Bay Street | Coffee Shop | Italian Restaurant | Café | Ice Cream Shop | Juice Bar | Sandwich Place | Burger Joint | Japanese Restaurant | Salad Place | Department Store |
3 | Chinatown, Kensington Market, Grange Park | Café | Vietnamese Restaurant | Bar | Chinese Restaurant | Coffee Shop | Vegetarian / Vegan Restaurant | Dumpling Restaurant | Mexican Restaurant | Cocktail Bar | Burger Joint |
4 | Christie | Grocery Store | Café | Park | Restaurant | Candy Store | Nightclub | Baby Store | Gas Station | Coffee Shop | Bank |
Run k-means to cluster the neighborhood into 5 clusters.
from sklearn.cluster import KMeans #importing KMeans
# set number of clusters
kclusters = 5
Toronto_grouped_clustering = Toronto_grouped.drop('Neighborhood', 1)
# run k-means clustering
kmeans = KMeans(n_clusters=kclusters, random_state=0).fit(Toronto_grouped_clustering)
# check cluster labels generated for each row in the dataframe
kmeans.labels_[0:10]
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)
Let's create a new dataframe that includes the cluster as well as the top 10 venues for each neighborhood.
# add clustering labels
neighborhoods_venues_sorted.insert(0, 'Cluster Labels', kmeans.labels_)
Toronto = New_Toronto_df
Toronto = Toronto.join(neighborhoods_venues_sorted.set_index('Neighborhood'), on='Neighborhood')
Toronto.head()
PostalCode | Borough | Neighborhood | Latitude | Longitude | Cluster Labels | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | M4E | East Toronto | The Beaches | 43.676357 | -79.293031 | 0 | Asian Restaurant | Health Food Store | Pub | Trail | Dessert Shop | Dim Sum Restaurant | Diner | Discount Store | Dog Run | Doner Restaurant |
1 | M4K | East Toronto | Riverdale, The Danforth West | 43.679557 | -79.352188 | 0 | Greek Restaurant | Coffee Shop | Italian Restaurant | Ice Cream Shop | Furniture / Home Store | Restaurant | Bookstore | Grocery Store | Pub | Pizza Place |
2 | M4L | East Toronto | The Beaches West, India Bazaar | 43.668999 | -79.315572 | 0 | Sandwich Place | Italian Restaurant | Steakhouse | Fast Food Restaurant | Sushi Restaurant | Ice Cream Shop | Liquor Store | Burrito Place | Burger Joint | Fish & Chips Shop |
3 | M4M | East Toronto | Studio District | 43.659526 | -79.340923 | 0 | Café | Coffee Shop | Gastropub | Brewery | Bakery | Italian Restaurant | American Restaurant | Comfort Food Restaurant | Sandwich Place | Cheese Shop |
4 | M4N | Central Toronto | Lawrence Park | 43.728020 | -79.388790 | 4 | Park | Bus Line | Swim School | Event Space | Ethiopian Restaurant | Electronics Store | Eastern European Restaurant | Dumpling Restaurant | Donut Shop | Doner Restaurant |
# Matplotlib and associated plotting modules
import matplotlib.cm as cm
import matplotlib.colors as colors
# create map
map_clusters = folium.Map(location=[To_latitude, To_longitude], zoom_start=11)
# set color scheme for the clusters
x = np.arange(kclusters)
ys = [i + x + (i*x)**2 for i in range(kclusters)]
colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
rainbow = [colors.rgb2hex(i) for i in colors_array]
# add markers to the map
markers_colors = []
for lat, lon, poi, cluster in zip(Toronto['Latitude'], Toronto['Longitude'], Toronto['Neighborhood'], Toronto['Cluster Labels']):
label = folium.Popup(str(poi) + ' Cluster ' + str(cluster), parse_html=True)
folium.CircleMarker(
[lat, lon],
radius=5,
popup=label,
color=rainbow[cluster-1],
fill=True,
fill_color=rainbow[cluster-1],
fill_opacity=0.7).add_to(map_clusters)
map_clusters
Now, I can examine each cluster and determine the discriminating venue categories that distinguish each cluster. Based on the defining categories,I can then assign a name to each cluster
Toronto.loc[Toronto['Cluster Labels'] == 0, Toronto.columns[[1] + list(range(5, Toronto.shape[1]))]]
Borough | Cluster Labels | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | East Toronto | 0 | Asian Restaurant | Health Food Store | Pub | Trail | Dessert Shop | Dim Sum Restaurant | Diner | Discount Store | Dog Run | Doner Restaurant |
1 | East Toronto | 0 | Greek Restaurant | Coffee Shop | Italian Restaurant | Ice Cream Shop | Furniture / Home Store | Restaurant | Bookstore | Grocery Store | Pub | Pizza Place |
2 | East Toronto | 0 | Sandwich Place | Italian Restaurant | Steakhouse | Fast Food Restaurant | Sushi Restaurant | Ice Cream Shop | Liquor Store | Burrito Place | Burger Joint | Fish & Chips Shop |
3 | East Toronto | 0 | Café | Coffee Shop | Gastropub | Brewery | Bakery | Italian Restaurant | American Restaurant | Comfort Food Restaurant | Sandwich Place | Cheese Shop |
5 | Central Toronto | 0 | Hotel | Park | Gym | Breakfast Spot | Dance Studio | Sandwich Place | Department Store | Food & Drink Shop | Diner | Dessert Shop |
6 | Central Toronto | 0 | Clothing Store | Coffee Shop | Yoga Studio | Bagel Shop | Fast Food Restaurant | Diner | Dessert Shop | Mexican Restaurant | Chinese Restaurant | Café |
7 | Central Toronto | 0 | Sandwich Place | Dessert Shop | Pizza Place | Coffee Shop | Italian Restaurant | Gym | Café | Sushi Restaurant | Pharmacy | Brewery |
9 | Central Toronto | 0 | Coffee Shop | Pub | Sushi Restaurant | Pizza Place | Sports Bar | Supermarket | Fried Chicken Joint | Health & Beauty Service | American Restaurant | Restaurant |
11 | Downtown Toronto | 0 | Coffee Shop | Park | Pub | Restaurant | Italian Restaurant | Café | Market | Bakery | Pizza Place | Beer Store |
12 | Downtown Toronto | 0 | Coffee Shop | Sushi Restaurant | Gay Bar | Japanese Restaurant | Restaurant | Hotel | Fast Food Restaurant | Gastropub | Burger Joint | Gym |
13 | Downtown Toronto | 0 | Coffee Shop | Bakery | Park | Pub | Café | Breakfast Spot | Mexican Restaurant | Restaurant | Dessert Shop | Bank |
14 | Downtown Toronto | 0 | Coffee Shop | Clothing Store | Japanese Restaurant | Café | Cosmetics Shop | Diner | Fast Food Restaurant | Electronics Store | Bubble Tea Shop | Bakery |
15 | Downtown Toronto | 0 | Coffee Shop | Café | Restaurant | American Restaurant | Breakfast Spot | Cocktail Bar | Beer Bar | Cosmetics Shop | Bakery | Italian Restaurant |
16 | Downtown Toronto | 0 | Coffee Shop | Cocktail Bar | Bakery | Cheese Shop | Beer Bar | Seafood Restaurant | Farmers Market | Steakhouse | Café | Gourmet Shop |
17 | Downtown Toronto | 0 | Coffee Shop | Italian Restaurant | Café | Ice Cream Shop | Juice Bar | Sandwich Place | Burger Joint | Japanese Restaurant | Salad Place | Department Store |
18 | Downtown Toronto | 0 | Coffee Shop | Café | Steakhouse | Cosmetics Shop | Bakery | Breakfast Spot | Burger Joint | Asian Restaurant | Thai Restaurant | Bar |
19 | Downtown Toronto | 0 | Coffee Shop | Aquarium | Café | Italian Restaurant | Hotel | Scenic Lookout | Brewery | Sporting Goods Shop | Restaurant | Fried Chicken Joint |
20 | Downtown Toronto | 0 | Coffee Shop | Café | Hotel | Restaurant | Steakhouse | Deli / Bodega | Seafood Restaurant | Gastropub | Bar | Japanese Restaurant |
21 | Downtown Toronto | 0 | Coffee Shop | Café | Restaurant | Hotel | Gastropub | Steakhouse | Bakery | Italian Restaurant | Gym | American Restaurant |
24 | Central Toronto | 0 | Sandwich Place | Café | Coffee Shop | Park | History Museum | Liquor Store | Burger Joint | Indian Restaurant | Pub | Flower Shop |
25 | Downtown Toronto | 0 | Café | Bakery | Japanese Restaurant | Sandwich Place | Restaurant | Bookstore | Bar | College Arts Building | Coffee Shop | Chinese Restaurant |
26 | Downtown Toronto | 0 | Café | Vietnamese Restaurant | Bar | Chinese Restaurant | Coffee Shop | Vegetarian / Vegan Restaurant | Dumpling Restaurant | Mexican Restaurant | Cocktail Bar | Burger Joint |
27 | Downtown Toronto | 0 | Airport Terminal | Airport Lounge | Airport Service | Plane | Rental Car Location | Boat or Ferry | Harbor / Marina | Boutique | Bar | Airport Gate |
28 | Downtown Toronto | 0 | Coffee Shop | Café | Hotel | Italian Restaurant | Beer Bar | Seafood Restaurant | Restaurant | Cocktail Bar | Japanese Restaurant | Art Gallery |
29 | Downtown Toronto | 0 | Coffee Shop | Café | Steakhouse | Restaurant | Deli / Bodega | Gastropub | Bar | Asian Restaurant | Gym | Seafood Restaurant |
30 | Downtown Toronto | 0 | Grocery Store | Café | Park | Restaurant | Candy Store | Nightclub | Baby Store | Gas Station | Coffee Shop | Bank |
31 | West Toronto | 0 | Pharmacy | Bakery | Park | Supermarket | Bar | Middle Eastern Restaurant | Café | Recording Studio | Fast Food Restaurant | Gym / Fitness Center |
32 | West Toronto | 0 | Bar | Coffee Shop | Asian Restaurant | Restaurant | Vietnamese Restaurant | Pizza Place | Men's Store | Café | Greek Restaurant | Juice Bar |
33 | West Toronto | 0 | Coffee Shop | Breakfast Spot | Café | Nightclub | Pet Store | Bar | Burrito Place | Restaurant | Climbing Gym | Performing Arts Venue |
34 | West Toronto | 0 | Mexican Restaurant | Café | Thai Restaurant | Bar | Park | Fried Chicken Joint | Music Venue | Diner | Cajun / Creole Restaurant | Bookstore |
35 | West Toronto | 0 | Gift Shop | Breakfast Spot | Cuban Restaurant | Eastern European Restaurant | Italian Restaurant | Bar | Dog Run | Restaurant | Movie Theater | Dessert Shop |
36 | West Toronto | 0 | Café | Pizza Place | Coffee Shop | Italian Restaurant | Sushi Restaurant | Bookstore | Electronics Store | Burrito Place | Pub | Post Office |
37 | Downtown Toronto | 0 | Coffee Shop | Gym | Park | Diner | Beer Bar | Seafood Restaurant | Sandwich Place | Salad Place | Juice Bar | Restaurant |
38 | East Toronto | 0 | Park | Auto Workshop | Comic Shop | Pizza Place | Burrito Place | Restaurant | Brewery | Light Rail Station | Smoke Shop | Farmers Market |
In Cluster = 0 mostly all veneues related to Food and drinking services. Name:- Food Services
Toronto.loc[Toronto['Cluster Labels'] == 1, Toronto.columns[[1] + list(range(5, Toronto.shape[1]))]]
Borough | Cluster Labels | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
22 | Central Toronto | 1 | Garden | Home Service | Ice Cream Shop | Women's Store | Department Store | Ethiopian Restaurant | Electronics Store | Eastern European Restaurant | Dumpling Restaurant | Donut Shop |
Cluster = 1, Name:- Home Services
Toronto.loc[Toronto['Cluster Labels'] == 2, Toronto.columns[[1] + list(range(5, Toronto.shape[1]))]]
Borough | Cluster Labels | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
23 | Central Toronto | 2 | Jewelry Store | Trail | Mexican Restaurant | Sushi Restaurant | Women's Store | Department Store | Electronics Store | Eastern European Restaurant | Dumpling Restaurant | Donut Shop |
cluster=2 Name:- Garments Store
Toronto.loc[Toronto['Cluster Labels'] == 3, Toronto.columns[[1] + list(range(5, Toronto.shape[1]))]]
Borough | Cluster Labels | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
8 | Central Toronto | 3 | Tennis Court | Women's Store | Deli / Bodega | Ethiopian Restaurant | Electronics Store | Eastern European Restaurant | Dumpling Restaurant | Donut Shop | Doner Restaurant | Dog Run |
Cluster = 3 Name:- Play Grounds
Toronto.loc[Toronto['Cluster Labels'] == 4, Toronto.columns[[1] + list(range(5, Toronto.shape[1]))]]
Borough | Cluster Labels | 1st Most Common Venue | 2nd Most Common Venue | 3rd Most Common Venue | 4th Most Common Venue | 5th Most Common Venue | 6th Most Common Venue | 7th Most Common Venue | 8th Most Common Venue | 9th Most Common Venue | 10th Most Common Venue | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
4 | Central Toronto | 4 | Park | Bus Line | Swim School | Event Space | Ethiopian Restaurant | Electronics Store | Eastern European Restaurant | Dumpling Restaurant | Donut Shop | Doner Restaurant |
10 | Downtown Toronto | 4 | Park | Trail | Playground | Dance Studio | Electronics Store | Eastern European Restaurant | Dumpling Restaurant | Donut Shop | Doner Restaurant | Dog Run |
Cluster = 4, Name:- Transports