daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np, math, pandas as pd, itertools, functools
  2. from collections import namedtuple
  3.  
  4. def reduce_concat(x, sep=""):
  5.     return functools.reduce(lambda x, y: str(x) + sep + str(y), x)
  6.  
  7. ##
  8.  
  9. def paste(*lists, sep=" ", collapse=None):
  10.     result = map(lambda x: reduce_concat(x, sep=sep), zip(*lists))
  11.     if collapse is not None:
  12.         return reduce_concat(result, sep=collapse)
  13.     return list(result)
  14.  
  15. ##
  16.  
  17. def bandt_pompe_method(data, D = 4):
  18.   if isinstance(data, pd.DataFrame):
  19.         data = pd.DataFrame(data)
  20.   #creating vector frequency
  21.   frequency_vector = np.zeros(3211) #Maior valor possível de uma concatenação dos 4 primeiros inteiros não negativos
  22.   x = np.arange(D) #Array de D elementos que vão de 0 a D-1
  23.   permutations = list(itertools.permutations(x)) #Gera uma lista com todas a permutações possíveis de 0 a D-1
  24.  
  25.   # cria um vetor de D posicoes, de 0 a D-1
  26.   ids = np.arange(D, dtype=object)
  27.   # adiciona "ids" no comeco dele
  28.   ids = np.insert(ids, 0, 'ids')
  29.  
  30.   # cria um vetor de D posicoes, zerado
  31.   values = np.zeros(D, dtype=object)
  32.   # adiciona "values" no comeco dele
  33.   values = np.insert(values, 0, 'values')
  34.  
  35.   # junta os dois vetores em uma matriz e transpoe
  36.   dados = np.array([ids, values]).T
  37.  
  38.   # cria um frame do panda com os dados
  39.   possitions = pd.DataFrame(data=dados[1:,0:], columns=dados[0,0:])
  40.    
  41.   for i in range(1, len(data) - D + 1): # Admitindo que data seja uma string
  42.       possitions.values = 4
  43.       show = data[i:(i+D-1), : ]
  44.       possitions.values = data[i:(i+D-1), : ]
  45.       sort = possitions[sorted(possitions.values), : ]
  46.       ordinal_pattern = paste(sort.ids, collapse="")
  47.       frequency_vector[[ordinal_pattern]] = frequency_vector[[ordinal_pattern]] + 1
  48.  
  49.   frequencies = map(list, zip(*frequency_vector))
  50.   probabilities = map(list, zip(*frequency_vector))/(len(data)[1]-D+1)
  51.   output = pd.DataFrame(frequencies, probabilities) #Em que parte que frequency_vector recebe esses nomes?
  52.    
  53.   return output
  54.      
  55. ##
  56. ##
  57. ##
  58.  
  59. def shannon_entropy(probs, normalized = False):
  60.     p = np.where(probs > 1e+30) #Descarta os valores nulos do vetor de probabilidades
  61.     entropy = 0.0
  62.     for i in p:
  63.         entropy -= probs[i]*math.log(probs[i]) #i assume os valores do vetor p
  64.     if(normalized):
  65.         entropy = entropy/math.log(len(probs))
  66.     return entropy
  67.  
  68. ##
  69. ##
  70. ##
  71.    
  72. def jensen_shannon(probs_1, probs_2):
  73.     J = math.sqrt( shannon_entropy( (probs_1 + probs_2) / 2  ) - shannon_entropy(probs_1)/2 - shannon_entropy(probs_2)/2 )
  74.     return J
  75.  
  76. ##
  77. ##
  78. ##
  79.    
  80. def complexity(probs, entropy, Q_0): #Q_0 passa a ser parâmetro da função
  81.     #p = which(probs > 1e-30)
  82.     N = len(probs)
  83.     P_e = np.repeat(1/N, times = N)
  84.     J = shannon_entropy( (probs + P_e) / 2  ) - shannon_entropy(probs)/2 - shannon_entropy(P_e)/2
  85.     #aux = (((N+1)/N) * math.log(N + 1) - 2*math.log(2*N) + math.log(N))
  86.     #Q_0 = -2*(1/aux)
  87.     Q = Q_0 * J
  88.     complexity = Q*entropy
  89.     return complexity
  90.  
  91. def main():
  92.    
  93.     data = {1.000000e-04, 1.000000e-04, 1.000000e-04,
  94.  5.999999e-04, 1.000001e-04, 1.099900e-03,
  95.  1.100000e-03, 1.000047e-04,  2.099300e-03}
  96.    
  97.     result = bandt_pompe_method(data, D=4)
  98.     print(result)
  99.    
  100.     return 0
  101.  
  102. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top