This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
#GENERAL PLOT SETTINGS | |
#style sheet | |
plt.style.use('seaborn-paper') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#select the relevant variables | |
variables = np.array( [ 'HOMICIDES', 'CAR_THEFT', | |
'EXTORTION', 'STREET_TRANSPORT_THEFT' ] ) | |
#Here we standardize a dataset along any axis. | |
#Center to the mean and component wise scale to unit variance. | |
from sklearn import preprocessing | |
df_standardized = preprocessing.scale( df[variables] ) | |
df_standardized = pd.DataFrame( df_standardized ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.metrics import silhouette_samples, silhouette_score | |
#create a list of K-Means models. Each element is a KMeans model with a specific number of clusters | |
kmeans2_sil= [KMeans(n_clusters=i, random_state=11, init='k-means++').fit(df_standardized) for i in range(1,15)] | |
#calculamos el silhouette score a cada modelo (de diferente número de clusters) | |
sil_values = [ silhouette_score(df_standardized, model.labels_, random_state=11) | |
for model in kmeans2_sil[1:] | |
] | |
#save the silhouette coefficient in an array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.cluster import KMeans | |
#create a list for the wcss parameter | |
wcss = [] | |
#test with 14 clusters | |
for i in range(1, 15): | |
kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 11) | |
kmeans.fit(df_standardized) | |
wcss.append(kmeans.inertia_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('Hola a todos') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def degres2decimal(a_degree, a_minute, a_seconds): | |
""" | |
This functions has as input numpy-arrays of degrees, minutes and seconds and | |
returns a decimal degree array | |
""" | |
degree = a_degree.astype(int) | |
minute = a_minute.astype(int) | |
seconds = a_seconds.astype(int) | |
return degree + minute/60 + seconds/3600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#assign colours to duration days | |
def colour_duration(string): | |
if string == '1 Día': | |
return '#003049' | |
elif string == '2 a 3 Días': | |
return '#FCBF49' | |
elif string == '4 a 7 Días': | |
return '#F77F00' | |
else: | |
return '#D62828' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
style_function = lambda feature: { | |
'fill_color': COLOUR_FUNCTION(feature), | |
'fillOpacity': 0.95, | |
'color': 'black' #contour colour, | |
'weight': 1 #tickness of the colour | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_color(feature): | |
value = map_dict.get(feature['id']) | |
if value is None: | |
return '#8c8c8c' # MISSING -> gray | |
elif value ==1 : | |
return '#DAF7A6' | |
elif value ==2 : | |
return '#FFC300' | |
elif value ==3 : | |
return '#FF5733' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import folium | |
from folium.plugins import HeatMap | |
loc = df[ ['Latitude', 'Longitude'] ].to_numpy() | |
map_heatmap = folium.Map( location= (df['Latitude'].mean(), df['Longitude'].mean()), zoom_start= 5 ) | |
HeatMap( | |
data = loc, |
OlderNewer