Skip to content

Instantly share code, notes, and snippets.

Avatar

Isaac Arroyo isaacarroyov

View GitHub Profile
@isaacarroyov
isaacarroyov / relevant_libraries.py
Created Sep 18, 2020
relevant code for the crime_analysis_mx2017 repository
View relevant_libraries.py
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')
@isaacarroyov
isaacarroyov / data_preprocessing.py
Last active Nov 20, 2020
relevant code for the preprocessing
View data_preprocessing.py
#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 )
@isaacarroyov
isaacarroyov / elbow_method.py
Last active Nov 20, 2020
application of elbow method
View elbow_method.py
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_)
@isaacarroyov
isaacarroyov / sil_method.py
Created Nov 20, 2020
application of the silhouette method
View sil_method.py
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
@isaacarroyov
isaacarroyov / hello.py
Created Mar 5, 2021
inicios de python
View hello.py
print('Hola a todos')
View conversion_function.py
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
View HeatMap_folium.py
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,
View colour_duration_translation.py
#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'
View style_function_map.py
style_function = lambda feature: {
'fill_color': COLOUR_FUNCTION(feature),
'fillOpacity': 0.95,
'color': 'black' #contour colour,
'weight': 1 #tickness of the colour
}
View color_function_map.py
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'