0

scipy의 entropy 함수를 사용하여 KL 분기를 계산하려고합니다.Scipy에서 KL 분기를 계산하는 중 오류가 발생했습니다.

p은 다음과 같습니다

array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], 
     [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], 
     [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], 
     [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], 
     [ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) 

q은 다음과 같습니다

array([[ 0.05242718, 0.04436347, 0.04130855, 0.04878344, 0.04310538, 
     0.02856853, 0.03303122, 0.02517992, 0.08525434, 0.03450324, 
     0.14580068, 0.1286993 , 0.28897473], 
     [ 0.65421444, 0.11592199, 0.0642645 , 0.02989768, 0.01385762, 
     0.01756484, 0.01024294, 0.00891479, 0.01140301, 0.00718939, 
     0.00938009, 0.01070139, 0.04644726], 
     [ 0.65984136, 0.13251236, 0.06345234, 0.02891162, 0.02429709, 
     0.02025307, 0.01073064, 0.01170066, 0.00678652, 0.00703361, 
     0.00560414, 0.00651137, 0.02236522], 
     [ 0.32315928, 0.23900077, 0.05460232, 0.03953635, 0.02901102, 
     0.01294443, 0.02372061, 0.02092882, 0.01188251, 0.01377188, 
     0.02976672, 0.05854314, 0.14313218], 
     [ 0.7717858 , 0.09692616, 0.03415596, 0.01713088, 0.01108141, 
     0.0128005 , 0.00847301, 0.01049734, 0.0052889 , 0.00514799, 
     0.00442508, 0.00485477, 0.01743218]], dtype=float32) 

내가 할 경우 :

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-201-563ea7d4decf> in <module>() 
     4 print('p0:',p[0]) 
     5 print('q0:',q[0]) 
----> 6 entropy(p[0],q[0]) 

/Users/freelancer/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/mlab.py in entropy(y, bins) 
    1570  y = np.zeros((len(x)+2,), x.dtype) 
    1571  y[1:-1] = x 
-> 1572  dif = np.diff(y) 
    1573  up = (dif == 1).nonzero()[0] 
    1574  dn = (dif == -1).nonzero()[0] 

/Users/freelancer/anaconda/envs/py35/lib/python3.5/site-packages/numpy/lib/function_base.py in histogram(a, bins, range, normed, weights, density) 
    781   if (np.diff(bins) < 0).any(): 
    782    raise ValueError(
--> 783     'bins must increase monotonically.') 
    784 
    785   # Initialize empty histogram 

ValueError: bins must increase monotonically. 
: 나는 다음과 같은 오류를 얻고있다

entropy(p[0],q[0]) 

왜 그렇습니까?

+0

스크린 샷으로 오류/코드를 게시하지 말고 텍스트를 복사하십시오. – kazemakase

+0

@ kazemakase done – VeilEclipse

+0

@ VeilEclipse [감사합니다] (http://meta.stackoverflow.com/a/285557/3005167) :) – kazemakase

답변

2

이 예제 배열에서 작동합니다

import scipy as sp 
sp.stats.entropy(p[0], q[0]) 

오류 마사지 스택 추적을 보면, 당신이 다르게 작동 scipy's entropy 기능 만 matplotlib's entropy를 호출하지 않은 것이 분명해진다. 다음은 관련 부분입니다.

/Users/freelancer/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/mlab.pyin entropy(y, bins)

+0

고마워요. matplotlib와 명시 적으로 scipy의 엔트로피를 모두 가져 왔습니다. 어쩌면 충돌이 일어 났을 것입니다. – VeilEclipse

+1

@VeilEclipse 예, 그럴 수 있습니다. 결과는 수입 주문 등의 순서에 따라 달라질 수 있습니다. 이런 이유로 처음에는 편리하다고해도 대개는 전역 네임 스페이스에 '가져 오는 것이 좋지 않습니다'(http://stackoverflow.com/q/2386714/3005167). – kazemakase

관련 문제