def kullback_leibler(vec1, vec2, num_features=None):
 if scipy.sparse.issparse(vec1):
  vec1 = vec1.toarray()
 if scipy.sparse.issparse(vec2):
  vec2 = vec2.toarray()
 if (isbow(vec1) and isbow(vec2)):
  if (num_features != None):
   dense1 = sparse2full(vec1, num_features)
   dense2 = sparse2full(vec2, num_features)
   return entropy(dense1, dense2)
  else:
   max_len = max(len(vec1), len(vec2))
   dense1 = sparse2full(vec1, max_len)
   dense2 = sparse2full(vec2, max_len)
   return entropy(dense1, dense2)
 else:
  if (len(vec1) == 1):
   vec1 = vec1[0]
  if (len(vec2) == 1):
   vec2 = vec2[0]
  return scipy.stats.entropy(vec1, vec2)

def kullback_leibler ( vec1 , vec2 , num_features = None ) : if scipy . sparse . issparse ( vec1 ) : vec1 = vec1 . toarray ( ) if scipy . sparse . issparse ( vec2 ) : vec2 = vec2 . toarray ( ) if ( isbow ( vec1 ) and isbow ( vec2 ) ) : if ( num_features != None ) : dense1 = sparse2full ( vec1 , num_features ) dense2 = sparse2full ( vec2 , num_features ) return entropy ( dense1 , dense2 ) else : max_len = max ( len ( vec1 ) , len ( vec2 ) ) dense1 = sparse2full ( vec1 , max_len ) dense2 = sparse2full ( vec2 , max_len ) return entropy ( dense1 , dense2 ) else : if ( len ( vec1 ) == 1 ) : vec1 = vec1 [ 0 ] if ( len ( vec2 ) == 1 ) : vec2 = vec2 [ 0 ] return scipy . stats . entropy ( vec1 , vec2 )
a distance metric between two probability distributions .