In [9]:
%matplotlib inline
import matplotlib.pyplot as plt
import time
import datetime as dt
import pickle
import numpy as np
from geopy.distance import vincenty
from itertools import chain, combinations
import random
import scipy as sp
from __future__ import division
from algoritmo_3.auxiliar_functions import *
import algoritmo_3.tfe
import algoritmo_3.borrador
from algoritmo_1.tpm_identification import get_similarity

In [5]:
with open('/home/cata/Proyectos/Notebooks & beyond/Notebooks/Normalizasound_2/users_profiles.pickle') as f:
    profiles_abril = pickle.load(f)

In [6]:
with open('/home/cata/Proyectos/Notebooks & beyond/Notebooks/Normalizasound_2/profiles.pickle') as f:
    sequences_septiembre = pickle.load(f)

In [8]:
with open('algoritmo_3/data/rois_meters_data.pickle') as f:
    abril_the_rois = pickle.load(f)
    septiembre_the_rois = pickle.load(f)

In [18]:
with open('algoritmo_3/data/shared_rois.pickle') as f:
    shared_rois = pickle.load(f)

In [14]:
len(sequences_septiembre)


Out[14]:
5343

In [15]:
len(profiles_abril)


Out[15]:
5168

In [66]:
#obtiene los vecinos del usuario "user",
#considerando como vecino a quien comparte dos ubicaciones
def get_neighbours_index(rois_a,shared_rois,user,min_shared):
    #min_shared_x = min(len(rois_a),min_shared)
    neighbours = np.where(shared_rois[user] >= min_shared)
    return neighbours[0]

In [50]:
# Funcion que construye la matriz de identificacion en que cada indice corresponde
# a la similitud entre la i-esima tpm y la j-esima secuencia, obtenidas a partir de un
# perfil de usuario y un periodo de identificacion.
# len(users_profiles) == len(users_sequences)
# asume que los usuarios de users_profiles y users_sequences son los mismos
# get_identification_matrix; get_profiles(...) get_sequences(...) -> [[int]]
def get_identification_matrix_2_rois(rois_a,shared_rois,min_shared,users_profiles,users_sequences):
    i = 0
    limit = np.min((len(users_profiles),len(users_sequences)))
    identification_matrix = np.ones((limit,limit)) * -pow(10,6)
    for profile in users_profiles:
        tpm = profile['tpm']
        id_user = profile['user_id']
        mls = profile['mls']
        neighbours = get_neighbours_index(rois_a,shared_rois,i,min_shared)
        for neighbour in neighbours:
            if neighbour < limit:
                data_sequence = users_sequences[neighbour]
                identification_matrix[i,neighbour] = get_similarity(tpm,mls,data_sequence['sequence'])
        i += 1
        if(i >= limit):
            break
    return identification_matrix

In [51]:
a_matrix = get_identification_matrix_2_rois(abril_the_rois,shared_rois,2,profiles_abril,sequences_septiembre)

In [52]:
iden_matrix = np.matrix(a_matrix)
df_ident = pd.DataFrame(iden_matrix)
df_ident.head(100)


Out[52]:
0 1 2 3 4 5 6 7 8 9 ... 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167
0 -118.703875 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
1 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
2 -1000000.000000 -1000000 -476.982271 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
3 -1000000.000000 -1000000 -1000000.000000 -307.20412 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
4 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -387.40824 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
5 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -783 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
6 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -405.78558 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
7 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -783 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
8 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -252.704365 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
9 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -344.72006 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
10 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
11 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
12 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
13 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
14 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
15 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
16 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
17 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
18 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
19 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
20 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
21 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
22 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
23 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
24 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
25 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
26 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
27 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
28 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
29 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
70 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
71 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
72 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
73 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
74 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
75 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
76 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
77 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
78 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
79 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
80 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
81 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
82 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
83 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
84 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
85 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
86 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
87 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
88 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
89 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
90 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
91 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
92 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
93 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
94 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
95 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
96 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
97 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
98 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000
99 -1000000.000000 -1000000 -1000000.000000 -1000000.00000 -1000000.00000 -1000000 -1000000.00000 -1000000 -1000000.000000 -1000000.00000 ... -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000 -1000000

100 rows × 5168 columns


In [54]:
i = 0
identified_indexs = []
wrong_indexs = []
correct_indexs = []
selected_indexs = []
n_identified = 0
limit = 5168
while (i<limit):
    the_index = np.argmax(iden_matrix[i,:])
    selected_indexs.append(np.max(iden_matrix[i,:]))
    identified_indexs.append(the_index)
    if(the_index!=i):
        wrong_indexs.append(the_index)
    else:
        correct_indexs.append(the_index)
        n_identified += 1
    i += 1
# In[11]:
porcentaje_correcto = n_identified*100/limit
print str(round(porcentaje_correcto,2))+ "%"


49.01%

In [60]:
get_neighbours_index(abril_the_rois,shared_rois,5167,2)


Out[60]:
array([5167])

In [67]:
shared = 0
for i in range(len(profiles_abril)):
    neighbours = get_neighbours_index(abril_the_rois,shared_rois,i,2)
    if len(neighbours) > 0:
        shared+=1
print shared*100/len(profiles_abril)


79.6826625387

In [65]:
same = 0
for i in range(len(profiles_abril)):
    if profiles_abril[i]['user_id'] == sequences_septiembre[i]['user_id']:
        same +=1
print same*100/len(profiles_abril)


100.0

In [ ]: