<jupyter_start><jupyter_text>Battle of Neighbourhood - Bangalore  Importing Packages<jupyter_code>import numpy as np # library to handle data in a vectorized manner

import pandas as pd # library for data analsysis
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

import json # library to handle JSON files

#!conda install -c conda-forge geopy --yes # uncomment this line if you haven't completed the Foursquare API lab
from geopy.geocoders import Nominatim # convert an address into latitude and longitude values

# Matplotlib and associated plotting modules
import matplotlib.cm as cm
import matplotlib.colors as colors

# import k-means from clustering stage
from sklearn.cluster import KMeans

!conda install -c conda-forge folium=0.5.0 --yes # uncomment this line if you haven't completed the Foursquare API lab
import folium # map rendering library

import requests # library to handle requests
from pandas.io.json import json_normalize # tranform JSON file into a pandas dataframe

print('Libraries imported.')<jupyter_output>Solving environment: done

# All requested packages already installed.

Libraries imported.<jupyter_text>Loading the Geolocation Data<jupyter_code># The code was removed by Watson Studio for sharing.<jupyter_output><empty_output><jupyter_text>Converting JSON to DF<jupyter_code>data = json.load(body)
dfjs=[]
for area in data["features"]:
    dfjs.append(area['properties'])
df = pd.DataFrame(dfjs)
df = df[['WARD_NAME','LAT','LON']]
print(df.shape)
df.head()<jupyter_output>(198, 3)<jupyter_text>Finding different location details using Foursquare<jupyter_code>neighborhood_latitude =12.972442
neighborhood_longitude = 77.580643


CLIENT_ID = 'QGEESMKPCO1WIAXXQ0NM15QFWO2YATAROUPQ1RTKA4HDNAJB' # your Foursquare ID
CLIENT_SECRET = '<KEY>' # your Foursquare Secret
VERSION = '20190605'
LIMIT = 100

radius = 2000

cat = ['school','restaurant','park','cafe','hospital','temple','store','shop','theater','hotel','taxi','bus','pharmacy','café']
catlst=[]

for lat, lon, area in list(zip(df['LAT'], df['LON'], df['WARD_NAME'])):
    url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
        CLIENT_ID, 
        CLIENT_SECRET, 
        VERSION, 
        lat, 
        lon, 
        radius, 
        LIMIT)
    
    results = requests.get(url).json()
    print(results)
    dfa=''
    
    try:
        for i in results['response']['groups'][0]['items']:
            dfa = dfa+i['venue']['categories'][0]['name']+','
    except:
        dfa=''
    lst=''
    for categ in cat:
        if categ in dfa.lower() and categ not in lst:
            lst=lst+categ+'|'
    catlst.append(lst)
df['Area']=catlst
df.head(20)<jupyter_output><empty_output><jupyter_text>Calculating Score of different areas<jupyter_code>score=[]
for ele in df['Area']:
    score.append((len(ele.split('|'))-1)/len(cat)*100)
df["Score"]=score
df.head(20)<jupyter_output><empty_output><jupyter_text>Data Visualisation - Box Plot<jupyter_code>y = df[['WARD_NAME','Score']]
# use the inline backend to generate the plots within the browser
%matplotlib inline 

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.style.use('ggplot') # optional: for ggplot-like style
plt.boxplot(df["Score"], 0, 'gD')

plt.show()<jupyter_output><empty_output><jupyter_text>Data Visualisation - Map<jupyter_code># create map of New York using latitude and longitude values
map_newyork = folium.Map(location=[12.9716, 77.5946], zoom_start=12)
threshold_scale = np.linspace(df['Score'].min(),
                              df['Score'].max(),
                              4, dtype=int)
#print(threshold_scale)
# add markers to map
for lat, lon, col,score in zip(df['LAT'], df['LON'], df['WARD_NAME'], df['Score']):
    if score>47:
        folium.CircleMarker(
            [lat, lon],
            radius=5,
            color='red',
            fill=True,
            fill_color='#3186cc',
            fill_opacity=0.0,
            parse_html=False).add_to(map_newyork)
    elif score>23:
        folium.CircleMarker(
            [lat, lon],
            popup=col+' ==> Net Score :'+str(score)+'%',
            radius=5,
            color='blue',
            fill=True,
            fill_color='#3186cc',
            fill_opacity=0.0,
            parse_html=False).add_to(map_newyork)
    else:
        folium.CircleMarker(
            [lat, lon],
            radius=5,
            color='green',
            fill=True,
            fill_color='#3186cc',
            fill_opacity=0.0,
            parse_html=False).add_to(map_newyork)

    #folium.Marker([lat,lon], popup=col+df['Area']).add_to(map_newyork) 
        
map_newyork<jupyter_output><empty_output><jupyter_text>Listing Top 10 Areas<jupyter_code>df = df.sort_values(['Score'], ascending=False)
print("Top Ten Area with Recquirements for living within 3KM in Bengaluru :\n",df["WARD_NAME"].head())<jupyter_output>Top Ten Area with Recquirements for living within 3KM in Bengaluru :
 6                   Devasandra
8                <NAME>
10           <NAME>
13                 Thanisandra
2     Yelahanka Satellite Town
Name: WARD_NAME, dtype: object
{'fraction_non_alphanumeric': 0.12766775620073062, 'fraction_numerical': 0.020765237454335703, 'mean_word_length': 3.547202797202797, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '649649', 'license': 'MIT', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1709, 'n_words': 403, 'path': 'Battle_of_Neighbourhood.ipynb', 'repo_name': 'akshaygj/Applied-Data-Science-Capstone', 'size': '5262'}