Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Estimativa de efetivo para PMMT nas eleições de 2018

3171 views
Kernel: Python 3 (Ubuntu Linux)
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster from scipy.spatial.distance import pdist, squareform from matplotlib import pyplot as plt from geopy.distance import vincenty import geopy import numpy as np import pandas as pd max_d = 2 # Distancia maxima de 2km]
def calcula_distancias(locs_1, locs_2): n_linhas_1 = locs_1.shape[0] n_linhas_2 = locs_2.shape[0] dists = np.empty((n_linhas_1, n_linhas_2)) for i in range(n_linhas_1): for j in range(n_linhas_2): dists[i, j] = geopy.distance.vincenty(locs_1[i], locs_2[j]).km return dists
locais = pd.read_excel('Planilha para distribuicao do efetivo.xlsx', sheet_name='dados') locais[:5]
coords = locais[['longitude','latitude']].values
coords
array([[-61.50422603, -10.87078709], [-60.941759 , -10.475339 ], [-60.180703 , -14.107886 ], ..., [-50.511945 , -10.47179925], [-50.51073158, -10.46768262], [-50.296635 , -9.994842 ]])
matriz_distancias = calcula_distancias(coords, coords)
matriz_distancias
array([[0.00000000e+00, 6.61801777e+01, 2.29587452e+02, ..., 1.22413550e+03, 1.22427559e+03, 1.24901215e+03], [6.61801777e+01, 0.00000000e+00, 2.16509550e+02, ..., 1.16121113e+03, 1.16134618e+03, 1.18553983e+03], [2.29587452e+02, 2.16509550e+02, 0.00000000e+00, ..., 1.10042873e+03, 1.10061520e+03, 1.13048953e+03], ..., [1.22413550e+03, 1.16121113e+03, 1.10042873e+03, ..., 0.00000000e+00, 3.21690889e-01, 4.15135648e+01], [1.22427559e+03, 1.16134618e+03, 1.10061520e+03, ..., 3.21690889e-01, 0.00000000e+00, 4.11970411e+01], [1.24901215e+03, 1.18553983e+03, 1.13048953e+03, ..., 4.15135648e+01, 4.11970411e+01, 0.00000000e+00]])
d = matriz_distancias.shape[0] v = d*(d - 1)/2 C = np.empty([int(round(v,0))]) ic = 0 for i in range(d): for j in range(i+1,d): # print(matriz_distancias[i,j]) C[ic] = matriz_distancias[i,j] ic = ic + 1
C
array([ 66.18017766, 229.587452 , 283.50213756, ..., 0.32169089, 41.5135648 , 41.19704105])
Z = linkage(C, 'complete') dn = dendrogram(Z)
Image in a Jupyter notebook
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html
grupos = fcluster(Z, max_d, criterion='distance')
locais['GRUPOS'] = grupos
locais[:5]
res1 = pd.ExcelWriter('res1 VMD.xlsx') locais.to_excel(res1,'dados',index=False) res1.save()