London Crimes
Published: 2020-05-20 09:32:52

<h1 id="c1d1" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">1. Introduction</h1><h2 id="fbaa" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">Background</h2><p id="aa96" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">London is the capital and largest city of England and the United Kingdom. London exerts a considerable impact upon the arts, commerce, education, entertainment, fashion, finance, health-care, media, professional services, research and development, tourism and transportation. It is the most-visited city as measured by international arrivals and has the busiest city airport system as measured by passenger traffic.</p><h2 id="4a74" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">Problem</h2><p id="7c49" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">As London is one of the most important cities in the world so the security of the city is most important. There are always some parts of the city that have more crime rates than any other part. So the problem is with people who migrate from different parts of the world for work and business. They are pretty much conscious about the selection of the area for residence from a safety point of view. This analysis will also help local police to focus and control the crimes in those areas which become the major centers for criminal activities.</p><h2 id="a14d" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">Solution</h2><p id="226f" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">This project aims to select the safest borough in London based on the total crimes, explore the neighborhoods of that borough to find the 10 most common venues in each neighborhood and finally cluster the neighborhoods using k-mean clustering.</p><p id="8090" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">This report will be targeted to people who are looking to relocate to London. In order to finalize a neighborhood to hunt for an apartment, safety is considered as a top concern when moving to a new place. If you don’t feel safe in your own home, you’re not going to be able to enjoy living there. The crime statistics will provide an insight into this issue.</p><h1 id="6f8a" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">2. Data</h1><p id="ea76" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">London crime dataset is used for the analysis. Description of the same is given below:</p><p id="0ffd" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">1. lsoa_code: code for Lower Super Output Area in Greater London.<br/>2. borough: Common name for London borough.<br/>3. major_category: High-level categorization of crime.<br/>4. minor_category: Low-level categorization of crime within a major category.<br/>5. value: monthly reported count of categorical crime in a given borough.<br/>6. year: Year of reported counts, 2008–2016<br/>7. month: Month of reported counts, 1–12.</p><p id="5882" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Apart from this data set Web Scraping is done to gather additional information about the different Boroughs in London from a Wikipedia page.</p><h2 id="dd3a" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">Web Scraping</h2><p id="72d2" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">So, to scrap the longitude and latitude of Boroughs from the Wikipedia page we’ll use Beautiful Soup.</p><pre class="hl hm hn ho hp jx jy dj"><span id="a59a" class="ir ig ce bj jz b gq ka kb r kc"><strong class="jz kd">from</strong> <strong class="jz kd">bs4</strong> <strong class="jz kd">import</strong> BeautifulSoup <em class="ke"># library for web scrapping</em><br/>wikipedia_link=&#x27;https://en.wikipedia.org/wiki/List_of_London_boroughs&#x27;<br/><em class="ke">#using beautiful soup to parse the XML code.</em><br/>wikipedia_page=requests.get(wikipedia_link).text<br/>soup = BeautifulSoup(wikipedia_page,&#x27;xml&#x27;)<br/>print(soup.prettify())</span></pre><p id="1d93" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">The table which contains additional details of neighborhoods is also scrapped and converted to the data frame.</p><pre class="hl hm hn ho hp jx jy dj"><span id="9d62" class="ir ig ce bj jz b gq ka kb r kc"><em class="ke">#extracting the table from webpage</em><br/>table = soup.find_all(&#x27;table&#x27;,{&#x27;class&#x27;:&#x27;wikitable sortable&#x27;})<br/>print(table)<br/><em class="ke">#converting table to dataframe</em><br/>London_table = pd.read_html(str(table[0]), index_col=<strong class="jz kd">None</strong>, header=0)[0]<br/>London_table.tail()</span></pre><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek kf"><div class="hw r hx hy"><div class="kj ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*RSBwzpYc344nkHHfytGt_A.png?q=20" width="1021" height="644" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1021" height="644" srcSet="https://miro.medium.com/max/552/1*RSBwzpYc344nkHHfytGt_A.png 276w, https://miro.medium.com/max/1104/1*RSBwzpYc344nkHHfytGt_A.png 552w, https://miro.medium.com/max/1280/1*RSBwzpYc344nkHHfytGt_A.png 640w, https://miro.medium.com/max/1400/1*RSBwzpYc344nkHHfytGt_A.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2042/1*RSBwzpYc344nkHHfytGt_A.png" width="1021" height="644" srcSet="https://miro.medium.com/max/552/1*RSBwzpYc344nkHHfytGt_A.png 276w, https://miro.medium.com/max/1104/1*RSBwzpYc344nkHHfytGt_A.png 552w, https://miro.medium.com/max/1280/1*RSBwzpYc344nkHHfytGt_A.png 640w, https://miro.medium.com/max/1400/1*RSBwzpYc344nkHHfytGt_A.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">Scrapped Table</strong></figcaption></figure><h2 id="7eb7" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">London Crime Data Set</h2><p id="6a1c" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">Loading the data set.</p><pre class="hl hm hn ho hp jx jy dj"><span id="2d9b" class="ir ig ce bj jz b gq ka kb r kc">df = pd.read_csv(&quot;london_crime_by_lsoa.csv&quot;)<br/><em class="ke">#taking recent year - 2016</em><br/>df.drop(df.index[df[&#x27;year&#x27;]!=2016],inplace=<strong class="jz kd">True</strong>)<br/><em class="ke">#Removing all entries where crime value is null</em><br/>df=df[df.value!=0]<br/><em class="ke">#reset index &amp; dropping previous one</em><br/>df=df.reset_index(drop=<strong class="jz kd">True</strong>)</span></pre><p id="9dfd" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Let’s do some preprocessing stuff before proceeding further.</p><pre class="hl hm hn ho hp jx jy dj"><span id="2694" class="ir ig ce bj jz b gq ka kb r kc"><em class="ke">#change the column names</em><br/>df.columns =[&#x27;LSOA_Code&#x27;,&#x27;Borough&#x27;,&#x27;Major_Category&#x27;,&#x27;Minor_Category&#x27;,&#x27;No_of_Crimes&#x27;,&#x27;Year&#x27;,&#x27;Month&#x27;]<br/>#pivoting the table to view the no. of crimes for each major category in each Borough<br/>London_crime = pd.pivot_table(df,values=[&#x27;No_of_Crimes&#x27;],<br/>                             index=[&#x27;Borough&#x27;],<br/>                             columns=[&#x27;Major_Category&#x27;],<br/>                             aggfunc=np.sum,fill_value=0)<br/><em class="ke">#total crimes per borough</em><br/>London_crime[&#x27;Total&#x27;]=London_crime.sum(axis=1)<br/>London_crime.columns = [&#x27;Borough&#x27;,&#x27;Burglary&#x27;, &#x27;Criminal Damage&#x27;,&#x27;Drugs&#x27;,&#x27;Other Notifiable Offences&#x27;,<br/>                        &#x27;Robbery&#x27;,&#x27;Theft and Handling&#x27;,&#x27;Violence Against the Person&#x27;,&#x27;Total&#x27;]<br/>London_crime.head()</span></pre><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek ko"><div class="hw r hx hy"><div class="kp ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*x9LfK-g0NdRAvdkwKe307g.png?q=20" width="1020" height="271" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1020" height="271" srcSet="https://miro.medium.com/max/552/1*x9LfK-g0NdRAvdkwKe307g.png 276w, https://miro.medium.com/max/1104/1*x9LfK-g0NdRAvdkwKe307g.png 552w, https://miro.medium.com/max/1280/1*x9LfK-g0NdRAvdkwKe307g.png 640w, https://miro.medium.com/max/1400/1*x9LfK-g0NdRAvdkwKe307g.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2040/1*x9LfK-g0NdRAvdkwKe307g.png" width="1020" height="271" srcSet="https://miro.medium.com/max/552/1*x9LfK-g0NdRAvdkwKe307g.png 276w, https://miro.medium.com/max/1104/1*x9LfK-g0NdRAvdkwKe307g.png 552w, https://miro.medium.com/max/1280/1*x9LfK-g0NdRAvdkwKe307g.png 640w, https://miro.medium.com/max/1400/1*x9LfK-g0NdRAvdkwKe307g.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">London Crime Table after preprocessing</strong></figcaption></figure><p id="9524" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Now combine both the tables.</p><pre class="hl hm hn ho hp jx jy dj"><span id="8045" class="ir ig ce bj jz b gq ka kb r kc"><em class="ke">#combining both dataframes</em><br/>final_crime = pd.merge(London_crime, London_table, on=&#x27;Borough&#x27;)<br/>final_crime.head()</span></pre><h1 id="8a80" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">3. Exploratory Data Analysis (EDA)</h1><p id="ec1a" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">The describe function in python is used to get statistics of the London crime data, this returns the mean, standard deviation, minimum, maximum, 1st quartile (25%), 2nd quartile (50%), and the 3rd quartile (75%) for each of the major categories of crime. The count for each of the major categories of crime returns the value 33 which is the number of London boroughs. ‘Theft and Handling’ is the highest reported crime during the year 2016 followed by ‘Violence against the person’, ‘Criminal damage’. The lowest recorded crimes are ’Drugs’, ‘Robbery’ and ‘Other Notifiable offenses’.</p><pre class="hl hm hn ho hp jx jy dj"><span id="5beb" class="ir ig ce bj jz b gq ka kb r kc">London_crime.describe()</span></pre><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek kq"><div class="hw r hx hy"><div class="kr ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*jcg4b5v40cubxe8BBo8OpA.png?q=20" width="1034" height="371" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1034" height="371" srcSet="https://miro.medium.com/max/552/1*jcg4b5v40cubxe8BBo8OpA.png 276w, https://miro.medium.com/max/1104/1*jcg4b5v40cubxe8BBo8OpA.png 552w, https://miro.medium.com/max/1280/1*jcg4b5v40cubxe8BBo8OpA.png 640w, https://miro.medium.com/max/1400/1*jcg4b5v40cubxe8BBo8OpA.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2068/1*jcg4b5v40cubxe8BBo8OpA.png" width="1034" height="371" srcSet="https://miro.medium.com/max/552/1*jcg4b5v40cubxe8BBo8OpA.png 276w, https://miro.medium.com/max/1104/1*jcg4b5v40cubxe8BBo8OpA.png 552w, https://miro.medium.com/max/1280/1*jcg4b5v40cubxe8BBo8OpA.png 640w, https://miro.medium.com/max/1400/1*jcg4b5v40cubxe8BBo8OpA.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">Statistical Summary of Crimes</strong></figcaption></figure><h2 id="8a76" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">Visualizing the 5 boroughs with the MOST number of crimes.</h2><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek ks"><div class="hw r hx hy"><div class="kt ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*ywFZFttwjn76V1dhZ3lLuA.png?q=20" width="830" height="458" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="830" height="458" srcSet="https://miro.medium.com/max/552/1*ywFZFttwjn76V1dhZ3lLuA.png 276w, https://miro.medium.com/max/1104/1*ywFZFttwjn76V1dhZ3lLuA.png 552w, https://miro.medium.com/max/1280/1*ywFZFttwjn76V1dhZ3lLuA.png 640w, https://miro.medium.com/max/1400/1*ywFZFttwjn76V1dhZ3lLuA.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/1660/1*ywFZFttwjn76V1dhZ3lLuA.png" width="830" height="458" srcSet="https://miro.medium.com/max/552/1*ywFZFttwjn76V1dhZ3lLuA.png 276w, https://miro.medium.com/max/1104/1*ywFZFttwjn76V1dhZ3lLuA.png 552w, https://miro.medium.com/max/1280/1*ywFZFttwjn76V1dhZ3lLuA.png 640w, https://miro.medium.com/max/1400/1*ywFZFttwjn76V1dhZ3lLuA.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">London Boroughs with Highest Crimes</strong></figcaption></figure><p id="2a24" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: Westminster has the highest number of crimes.</p><p id="6f4c" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq"><strong class="jf kd">Visualizing different types of crimes in the borough ‘Westminster’.</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek ku"><div class="hw r hx hy"><div class="kv ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*fitD_u0g-c7We_FEQe9Jwg.png?q=20" width="892" height="469" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="892" height="469" srcSet="https://miro.medium.com/max/552/1*fitD_u0g-c7We_FEQe9Jwg.png 276w, https://miro.medium.com/max/1104/1*fitD_u0g-c7We_FEQe9Jwg.png 552w, https://miro.medium.com/max/1280/1*fitD_u0g-c7We_FEQe9Jwg.png 640w, https://miro.medium.com/max/1400/1*fitD_u0g-c7We_FEQe9Jwg.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/1784/1*fitD_u0g-c7We_FEQe9Jwg.png" width="892" height="469" srcSet="https://miro.medium.com/max/552/1*fitD_u0g-c7We_FEQe9Jwg.png 276w, https://miro.medium.com/max/1104/1*fitD_u0g-c7We_FEQe9Jwg.png 552w, https://miro.medium.com/max/1280/1*fitD_u0g-c7We_FEQe9Jwg.png 640w, https://miro.medium.com/max/1400/1*fitD_u0g-c7We_FEQe9Jwg.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">Different Crimes in Westminster</strong></figcaption></figure><p id="9f12" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Conclusion: It can be found that Westminster has the highest number of crimes when compared to the other boroughs in London.</p><h2 id="5b48" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">Visualize the 5 boroughs with the LEAST number of crimes.</h2><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek kw"><div class="hw r hx hy"><div class="kx ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*ZcEFNf6DCmgrKGvB7oQY-A.png?q=20" width="963" height="518" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="963" height="518" srcSet="https://miro.medium.com/max/552/1*ZcEFNf6DCmgrKGvB7oQY-A.png 276w, https://miro.medium.com/max/1104/1*ZcEFNf6DCmgrKGvB7oQY-A.png 552w, https://miro.medium.com/max/1280/1*ZcEFNf6DCmgrKGvB7oQY-A.png 640w, https://miro.medium.com/max/1400/1*ZcEFNf6DCmgrKGvB7oQY-A.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/1926/1*ZcEFNf6DCmgrKGvB7oQY-A.png" width="963" height="518" srcSet="https://miro.medium.com/max/552/1*ZcEFNf6DCmgrKGvB7oQY-A.png 276w, https://miro.medium.com/max/1104/1*ZcEFNf6DCmgrKGvB7oQY-A.png 552w, https://miro.medium.com/max/1280/1*ZcEFNf6DCmgrKGvB7oQY-A.png 640w, https://miro.medium.com/max/1400/1*ZcEFNf6DCmgrKGvB7oQY-A.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">London Boroughs with Least Crimes</strong></figcaption></figure><p id="eff0" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: The borough City of London has the lowest no. of crimes recorded for the year 2016.</p><p id="42ca" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">As per the Wikipedia page, The City of London is the 33rd principal division of Greater London but it is not a London borough.<br/>Hence we will focus on the next borough with the least crime i.e. Kingston upon Thames.</p><p id="6f1d" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq"><strong class="jf kd">Visualizing different types of crimes in the borough ‘Kingston upon Thames’.</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek ky"><div class="hw r hx hy"><div class="kz ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*cokvJkFOxEqvIE2l0I6vaQ.png?q=20" width="1067" height="582" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1067" height="582" srcSet="https://miro.medium.com/max/552/1*cokvJkFOxEqvIE2l0I6vaQ.png 276w, https://miro.medium.com/max/1104/1*cokvJkFOxEqvIE2l0I6vaQ.png 552w, https://miro.medium.com/max/1280/1*cokvJkFOxEqvIE2l0I6vaQ.png 640w, https://miro.medium.com/max/1400/1*cokvJkFOxEqvIE2l0I6vaQ.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2134/1*cokvJkFOxEqvIE2l0I6vaQ.png" width="1067" height="582" srcSet="https://miro.medium.com/max/552/1*cokvJkFOxEqvIE2l0I6vaQ.png 276w, https://miro.medium.com/max/1104/1*cokvJkFOxEqvIE2l0I6vaQ.png 552w, https://miro.medium.com/max/1280/1*cokvJkFOxEqvIE2l0I6vaQ.png 640w, https://miro.medium.com/max/1400/1*cokvJkFOxEqvIE2l0I6vaQ.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div><figcaption class="kk kl el ej ek km kn bi du gq bk bn"><strong class="bi ih">Different Crimes in Kingston upon Thames</strong></figcaption></figure><p id="7812" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Conclusion: It can be found that Kingston Upon Thames has the least number of crimes when compared to the other boroughs in London.</p><h1 id="5b6e" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">4. FourSquare API Integration.</h1><p id="b5cb" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">The Foursquare API allows application developers to interact with the Foursquare platform. The API itself is a RESTful set of addresses to which you can send requests, so there’s really nothing to download onto your server. So we will use this API to extract the details of important locations such as Bar, Garage, Restaurant, etc for each Neighborhood in Kingston Upon Thames.</p><p id="ba8e" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">But first, we have to find co-ordinates of neighborhoods.</p><pre class="hl hm hn ho hp jx jy dj"><span id="c6ac" class="ir ig ce bj jz b gq ka kb r kc"><strong class="jz kd">import</strong> <strong class="jz kd">geocoder</strong><em class="ke"> </em><br/><strong class="jz kd">from</strong> <strong class="jz kd">geopy.geocoders</strong> <strong class="jz kd">import</strong> Nominatim <em class="ke">#to convert an address into latitude and longitude values<br/></em>Latitude = []<br/>Longitude = []<br/><strong class="jz kd">for</strong> i <strong class="jz kd">in</strong> range(len(Neighborhood)):<br/>    address = &#x27;<strong class="jz kd">{}</strong>,London,United Kingdom&#x27;.format(Neighborhood[i])<br/>    geolocator = Nominatim(user_agent=&quot;London_agent&quot;)<br/>    location = geolocator.geocode(address)<br/>    Latitude.append(location.latitude)<br/>    Longitude.append(location.longitude)<br/>print(Latitude, Longitude)</span></pre><p id="b333" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Visualizing the Neighborhood of Kingston upon Thames Borough using the folium library.</p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek la"><div class="hw r hx hy"><div class="lb ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*7f1cIGjVsa82hegfgamyEw.png?q=20" width="1072" height="730" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1072" height="730" srcSet="https://miro.medium.com/max/552/1*7f1cIGjVsa82hegfgamyEw.png 276w, https://miro.medium.com/max/1104/1*7f1cIGjVsa82hegfgamyEw.png 552w, https://miro.medium.com/max/1280/1*7f1cIGjVsa82hegfgamyEw.png 640w, https://miro.medium.com/max/1400/1*7f1cIGjVsa82hegfgamyEw.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2144/1*7f1cIGjVsa82hegfgamyEw.png" width="1072" height="730" srcSet="https://miro.medium.com/max/552/1*7f1cIGjVsa82hegfgamyEw.png 276w, https://miro.medium.com/max/1104/1*7f1cIGjVsa82hegfgamyEw.png 552w, https://miro.medium.com/max/1280/1*7f1cIGjVsa82hegfgamyEw.png 640w, https://miro.medium.com/max/1400/1*7f1cIGjVsa82hegfgamyEw.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="a25f" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Now creating the API request URL and making the GET request.</p><pre class="hl hm hn ho hp jx jy dj"><span id="73b8" class="ir ig ce bj jz b gq ka kb r kc"><em class="ke"># create the API request URL</em><br/>        url = &#x27;https://api.foursquare.com/v2/venues/explore?&amp;client_id=<strong class="jz kd">{}</strong>&amp;client_secret=<strong class="jz kd">{}</strong>&amp;v=<strong class="jz kd">{}</strong>&amp;ll=<strong class="jz kd">{}</strong>,<strong class="jz kd">{}</strong>&amp;radius=<strong class="jz kd">{}</strong>&amp;limit=<strong class="jz kd">{}</strong>&#x27;.format(<br/>            CLIENT_ID, <br/>            CLIENT_SECRET, <br/>            VERSION, <br/>            lat, <br/>            lng, <br/>            radius, <br/>            LIMIT)<br/>            <br/>        <em class="ke"># make the GET request</em><br/>        results = requests.get(url).json()[&quot;response&quot;][&#x27;groups&#x27;][0][&#x27;items&#x27;]</span></pre><h2 id="d8da" class="ir ig ce bj bi ih is it iu iv iw ix iy iz ja jb jc">One Hot Encoding</h2><p id="0277" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">For categorical variables where no such ordinal relationship exists, the integer encoding is not enough. In this case, a one-hot encoding can be applied to the integer representation. This is where the integer encoded variable is removed and a new binary variable is added for each unique integer value.</p><p id="aed3" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">So now we will apply this encoding.</p><pre class="hl hm hn ho hp jx jy dj"><span id="545f" class="ir ig ce bj jz b gq ka kb r kc"><em class="ke"># one hot encoding</em><br/>safe_onehot = pd.get_dummies(safe_venues[[&#x27;Venue Category&#x27;]], prefix=&quot;&quot;, prefix_sep=&quot;&quot;)</span><span id="eb94" class="ir ig ce bj jz b gq lc ld le lf lg kb r kc"><em class="ke"># add neighborhood column back to dataframe</em><br/>safe_onehot[&#x27;Neighborhood&#x27;] = safe_venues[&#x27;Neighborhood&#x27;] </span><span id="4dd8" class="ir ig ce bj jz b gq lc ld le lf lg kb r kc"><em class="ke"># move neighborhood column to the first column</em><br/>fixed_columns = [safe_onehot.columns[-1]] + list(safe_onehot.columns[:-1])<br/>safe_onehot = safe_onehot[fixed_columns]</span></pre><h1 id="0bb7" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">5. Clustering</h1><p id="b33c" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">Clustering is a way of grouping data. It means finding clusters in a dataset. It’s an example of unsupervised learning. There are many algorithms to implement clustering but we will use the K-Means algorithm. K-Means divides the data into non-overlapping clusters without any cluster internal structure or labels.</p><p id="cf36" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Clustering similar neighborhoods together using k-means clustering.</p><pre class="hl hm hn ho hp jx jy dj"><span id="aaea" class="ir ig ce bj jz b gq ka kb r kc"><em class="ke"># import k-means from clustering stage</em><br/><strong class="jz kd">from</strong> <strong class="jz kd">sklearn.cluster</strong> <strong class="jz kd">import</strong> KMeans</span><span id="3f5d" class="ir ig ce bj jz b gq lc ld le lf lg kb r kc"><em class="ke"># set number of clusters</em><br/>kclusters = 5</span><span id="89f4" class="ir ig ce bj jz b gq lc ld le lf lg kb r kc">safe_grouped_clustering = safe_grouped.drop(&#x27;Neighborhood&#x27;, 1)</span><span id="202a" class="ir ig ce bj jz b gq lc ld le lf lg kb r kc"><em class="ke"># run k-means clustering</em><br/>kmeans = KMeans(n_clusters=kclusters, random_state=0).fit(safe_grouped_clustering)</span></pre><p id="f73b" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Now, visualizing the clusters on the map using folium.</p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek lh"><div class="hw r hx hy"><div class="li ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*6VK0o3jo_ZRk6mE7lKQgnQ.png?q=20" width="1142" height="676" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1142" height="676" srcSet="https://miro.medium.com/max/552/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 276w, https://miro.medium.com/max/1104/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 552w, https://miro.medium.com/max/1280/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 640w, https://miro.medium.com/max/1400/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2284/1*6VK0o3jo_ZRk6mE7lKQgnQ.png" width="1142" height="676" srcSet="https://miro.medium.com/max/552/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 276w, https://miro.medium.com/max/1104/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 552w, https://miro.medium.com/max/1280/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 640w, https://miro.medium.com/max/1400/1*6VK0o3jo_ZRk6mE7lKQgnQ.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="d642" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: Each cluster is color-coded for the ease of presentation, we can see that the majority of the neighborhood falls in the red cluster which is the first cluster. Blue, Orange, and Green each consist of 1 neighborhood. The purple cluster consists of 3 neighborhoods.</p><h1 id="2ccd" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">6. Results</h1><p id="a997" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq"><strong class="jf kd">1st Cluster :</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek lj"><div class="hw r hx hy"><div class="lk ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*oSt_M_v-PSifDEq-W2eMlw.png?q=20" width="1289" height="524" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1289" height="524" srcSet="https://miro.medium.com/max/552/1*oSt_M_v-PSifDEq-W2eMlw.png 276w, https://miro.medium.com/max/1104/1*oSt_M_v-PSifDEq-W2eMlw.png 552w, https://miro.medium.com/max/1280/1*oSt_M_v-PSifDEq-W2eMlw.png 640w, https://miro.medium.com/max/1400/1*oSt_M_v-PSifDEq-W2eMlw.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2578/1*oSt_M_v-PSifDEq-W2eMlw.png" width="1289" height="524" srcSet="https://miro.medium.com/max/552/1*oSt_M_v-PSifDEq-W2eMlw.png 276w, https://miro.medium.com/max/1104/1*oSt_M_v-PSifDEq-W2eMlw.png 552w, https://miro.medium.com/max/1280/1*oSt_M_v-PSifDEq-W2eMlw.png 640w, https://miro.medium.com/max/1400/1*oSt_M_v-PSifDEq-W2eMlw.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="1605" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: This is the biggest cluster with 10 of the 16 neighborhoods in the borough of Kingston upon Thames. Upon closely examining these neighborhoods we can see that the most common venues in these neighborhoods are Restaurants, Pubs, Cafes, Supermarkets, and stores.</p><p id="1934" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq"><strong class="jf kd">2nd Cluster :</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek ll"><div class="hw r hx hy"><div class="lm ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*vRCY1prqgsJa_g0-C-8QCQ.png?q=20" width="1253" height="274" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1253" height="274" srcSet="https://miro.medium.com/max/552/1*vRCY1prqgsJa_g0-C-8QCQ.png 276w, https://miro.medium.com/max/1104/1*vRCY1prqgsJa_g0-C-8QCQ.png 552w, https://miro.medium.com/max/1280/1*vRCY1prqgsJa_g0-C-8QCQ.png 640w, https://miro.medium.com/max/1400/1*vRCY1prqgsJa_g0-C-8QCQ.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2506/1*vRCY1prqgsJa_g0-C-8QCQ.png" width="1253" height="274" srcSet="https://miro.medium.com/max/552/1*vRCY1prqgsJa_g0-C-8QCQ.png 276w, https://miro.medium.com/max/1104/1*vRCY1prqgsJa_g0-C-8QCQ.png 552w, https://miro.medium.com/max/1280/1*vRCY1prqgsJa_g0-C-8QCQ.png 640w, https://miro.medium.com/max/1400/1*vRCY1prqgsJa_g0-C-8QCQ.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="8a4c" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: The second cluster has 3 neighborhoods in it, these neighborhoods have common venues such as Construction &amp; Landscaping, Sandwich Place, Grocery store, Gym/Fitness centers, Soccer fields, Department Store, etc.</p><p id="3545" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq"><strong class="jf kd">3rd Cluster :</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek ln"><div class="hw r hx hy"><div class="lo ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*X1zTGHmlZx3X9OOHH9MufQ.png?q=20" width="1215" height="185" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1215" height="185" srcSet="https://miro.medium.com/max/552/1*X1zTGHmlZx3X9OOHH9MufQ.png 276w, https://miro.medium.com/max/1104/1*X1zTGHmlZx3X9OOHH9MufQ.png 552w, https://miro.medium.com/max/1280/1*X1zTGHmlZx3X9OOHH9MufQ.png 640w, https://miro.medium.com/max/1400/1*X1zTGHmlZx3X9OOHH9MufQ.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2430/1*X1zTGHmlZx3X9OOHH9MufQ.png" width="1215" height="185" srcSet="https://miro.medium.com/max/552/1*X1zTGHmlZx3X9OOHH9MufQ.png 276w, https://miro.medium.com/max/1104/1*X1zTGHmlZx3X9OOHH9MufQ.png 552w, https://miro.medium.com/max/1280/1*X1zTGHmlZx3X9OOHH9MufQ.png 640w, https://miro.medium.com/max/1400/1*X1zTGHmlZx3X9OOHH9MufQ.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="6071" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: The third cluster has one neighborhood which consists of Venues such as Parks, Gyms, Bus Stop, Wine Shop, Farmer Market and Department stores.</p><p id="f9a7" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq"><strong class="jf kd">4th Cluster :</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek lp"><div class="hw r hx hy"><div class="lq ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*DK6uRMxTBsiYPbhhV11zzw.png?q=20" width="1212" height="185" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1212" height="185" srcSet="https://miro.medium.com/max/552/1*DK6uRMxTBsiYPbhhV11zzw.png 276w, https://miro.medium.com/max/1104/1*DK6uRMxTBsiYPbhhV11zzw.png 552w, https://miro.medium.com/max/1280/1*DK6uRMxTBsiYPbhhV11zzw.png 640w, https://miro.medium.com/max/1400/1*DK6uRMxTBsiYPbhhV11zzw.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2424/1*DK6uRMxTBsiYPbhhV11zzw.png" width="1212" height="185" srcSet="https://miro.medium.com/max/552/1*DK6uRMxTBsiYPbhhV11zzw.png 276w, https://miro.medium.com/max/1104/1*DK6uRMxTBsiYPbhhV11zzw.png 552w, https://miro.medium.com/max/1280/1*DK6uRMxTBsiYPbhhV11zzw.png 640w, https://miro.medium.com/max/1400/1*DK6uRMxTBsiYPbhhV11zzw.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="91eb" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: The fourth cluster has one neighborhood which consists of Venues such as Train Station, Pub, Food, Deli/Bodega, Dry Cleaner, etc.</p><p id="89f5" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq"><strong class="jf kd">5th Cluster :</strong></p><figure class="hl hm hn ho hp hq ej ek paragraph-image"><div class="kg kh hx ki ai"><div class="ej ek lr"><div class="hw r hx hy"><div class="ls ia r"><div class="hr hs s t u ht ai da hu hv"><img class="s t u ht ai ib ic id" src="https://miro.medium.com/max/60/1*rZ2Mm7NxC4RCByTbQD99iQ.png?q=20" width="1221" height="186" role="presentation"/></div><img class="hr hs s t u ht ai ie" width="1221" height="186" srcSet="https://miro.medium.com/max/552/1*rZ2Mm7NxC4RCByTbQD99iQ.png 276w, https://miro.medium.com/max/1104/1*rZ2Mm7NxC4RCByTbQD99iQ.png 552w, https://miro.medium.com/max/1280/1*rZ2Mm7NxC4RCByTbQD99iQ.png 640w, https://miro.medium.com/max/1400/1*rZ2Mm7NxC4RCByTbQD99iQ.png 700w" sizes="700px" role="presentation"/><noscript><img class="s t u ht ai" src="https://miro.medium.com/max/2442/1*rZ2Mm7NxC4RCByTbQD99iQ.png" width="1221" height="186" srcSet="https://miro.medium.com/max/552/1*rZ2Mm7NxC4RCByTbQD99iQ.png 276w, https://miro.medium.com/max/1104/1*rZ2Mm7NxC4RCByTbQD99iQ.png 552w, https://miro.medium.com/max/1280/1*rZ2Mm7NxC4RCByTbQD99iQ.png 640w, https://miro.medium.com/max/1400/1*rZ2Mm7NxC4RCByTbQD99iQ.png 700w" sizes="700px" role="presentation"/></noscript></div></div></div></div></figure><p id="8e36" class="jd jp ce bj jf b fq js jq fs jt jr ji ju gd jk jv ge jm jw gf jo eq">Observation: The fifth cluster has one neighborhood which consists of Venues such as Fish &amp; Chips Shop, Bakery, Indian Restaurant, Electronics Store, Department stores.</p><h1 id="0a46" class="if ig ce bj bi ih ez ii fb ij ik il im in io ip iq">7. Conclusion</h1><p id="5092" class="jd jp ce bj jf b fq jg jq fs jh jr ji jj gd jk jl ge jm jn gf jo eq">This project helps a person get a better understanding of the neighborhoods with respect to the most common venues in that neighborhood. Another result which we get from the analysis is that Westminster has the most number of crimes so police should focus more on this area. It is always helpful to make use of technology to stay one step ahead i.e. finding out more about places before moving into a neighborhood. We have just taken safety as a primary concern to shortlist the borough of London.</p></div></div></section></div></article>