0
  1. So we can do this in with numpy arrays:

    IN:
     a = np.array([1,2,3])
     b = np.array([[1,2,3], [1,2,4], [1,2,5]])
     a+b
    OUT:
      array([[2, 4, 6],
             [2, 4, 7],
             [2, 4, 8]])
    
  2. I have this function, where di and dj are lists or arrays of the same lenght and KL is scipy.stats.entropy

    def dis(di,dj):
        di = np.array(di)
        dj = np.array(dj)
        m =  0.5 * (di+dj)
        kl1 = KL(di,m)
        kl2 = KL(dj,m)
        return 0.5*(kl1+kl2)
    
  3. I was wondering if there was a way to apply the function as with a+b:

    dis(a,b) 
    

So instead of doing:

 for x in b:
     dis(a,x)

I am getting this error when trying to run:

    dis(a,b)

Error

UPDATE After resolving the error thanks to hpaulj suggestion I get the following result: Result 2

The problem with the result is that KL should only give positive values. So I don't get quiet well what does: dis(a,b) is actually doing.

kibs
  • 26
  • 3
  • What is the error you get when you try to run `dis(a, b)`? – Kevin Feb 02 '18 at 18:46
  • Thanks for your comment Kevin, I updated my question with a link to the picture of my error. – kibs Feb 02 '18 at 18:51
  • 1
    The code in the error is not the one you show. Do you understand the error? It has to do with that `float` call, not any looping or broadcasting issues. – hpaulj Feb 02 '18 at 19:07
  • Oh, thank you! Understood. Nevertheless, the output I get doesn't make sense. I updated my question with a picture of my result – kibs Feb 02 '18 at 20:49
  • Just a note on performance, you can use `np.asanyarray` in place of `np.array`, and it will speed up your code substantially when the inputs are already arrays. – user2699 Feb 03 '18 at 04:31
  • Thanks! I was looking forward to make my code more efficient too. :) – kibs Feb 04 '18 at 00:18

0 Answers0