Skip to content

Instantly share code, notes, and snippets.

View ShayanRiyaz's full-sized avatar
🎯
Focusing

Shayan Riyaz ShayanRiyaz

🎯
Focusing
View GitHub Profile
{
"query_input": {
"text": {
"text": "how to get tested",
"language_code": "en-US"
}
}
}
// See the detectIntent page for details about the JSON format.
import numpy as np # library to handle data in a vectorized manner
import pandas as pd # library for data analsysis
import json # library to handle JSON files
from geopy.geocoders import Nominatim # convert an address into latitude and longitude values
import requests # library to handle requests
from pandas.io.json import json_normalize # tranform JSON file into a pandas data frame
# Matplotlib and associated plotting modules
import matplotlib.cm as cm
import matplotlib.colors as colors
# import k-means from clustering stage
url = requests.get('https://www.rentcafe.com/average-rent-market-trends/us/ca/los-angeles/').text
soup = BeautifulSoup(url,"html.parser")
table = soup.find('table',id="MarketTrendsAverageRentTable")
pr = table.find_all('td')
nh = table.find_all('th')
price = []
neighbourhood = []
filter2_nhoods = filter2_nhoods.reindex( columns = filter2_nhoods.columns.tolist() + ['Distance from LA center (in km)']) #this way to avoid warnings
from math import radians, sin, cos, acos
slat = radians(34.0536909) #LA center Latitude obtained earlier
slon = radians(-118.2427666) #LA center Longitude obtained earlier
for n in range(0,len(filter2_nhoods)):
elat = radians(filter2_nhoods.iloc[n,1])
def get_neighbourhood_Greek_Restaurant(url1):
results = requests.get(url1).json()
# assign relevant part of JSON to venues
venues = results['response']['venues']
# tranform venues into a data frame
dataframe = json_normalize(venues)
clus1neigh=la_merged.loc[la_merged['Cluster Label'] == 1, la_merged.columns[0]].values.tolist()
filtered_nhoods=nhoods.copy()
for i in range(0,len(filtered_nhoods)):
if filtered_nhoods.iloc[i,0] not in clus1neigh:
filtered_nhoods.iloc[i,0]='TO DROP'
def generate_plot(clus,i):
plt.style.use('default')
tags=['Restaurant','Coffee','Food','Pizza','Sandwich']
colors = []
for value in clus.index:
if any(t in value for t in tags):
colors.append('#0000FF')
else:
import matplotlib.colors as colors
from matplotlib.colors import rgb2hex
# create map
map_clusters = folium.Map(location=[latitude, longitude], zoom_start=10)
# 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]
la_merged.loc[la_merged['Cluster Label'] == 0, la_merged.columns[[0] + list(range(4, la_merged.shape[1]))]]
la_merged.loc[la_merged['Cluster Label'] == 1, la_merged.columns[[0] + list(range(4, la_merged.shape[1]))]]
la_merged.loc[la_merged['Cluster Label'] == 2, la_merged.columns[[0] + list(range(4, la_merged.shape[1]))]]
la_merged.loc[la_merged['Cluster Label'] == 3, la_merged.columns[[0] + list(range(4, la_merged.shape[1]))]]
la_results = pd.DataFrame(kmeans.cluster_centers_)
la_results.columns = la_grouped_clustering.columns
la_results.index = ['Cluster 0','Cluster 1','Cluster 2','Cluster 3']
la_results['Total Sum'] = la_results.sum(axis = 1)
from sklearn.metrics import silhouette_score
la_grouped_clustering = la_grouped.drop('Neighbourhood', 1)
for n_cluster in range(2, 12):
kmeans = KMeans(n_clusters=n_cluster).fit(la_grouped_clustering)
label = kmeans.labels_
sil_coeff = silhouette_score(la_grouped_clustering, label, metric='euclidean')
print("For n_clusters={}, The Silhouette Coefficient is {}".format(n_cluster, sil_coeff))