2013-03-14 3 views
2

을 얻고 왜이 NaiveBayes 파이썬 라이브러리 (파이썬 2.7)이 코드는 나에게 ZeroDivisionError을주고 실행 이유가 궁금파이썬 NaiveBayes : 나는 ZeroDivisionError

을 시도하고있다. 그래서 즉, 내 입력 (에 문제가, 내가 베이지안 분류기에 새로운 오전

Traceback (most recent call last): 
    File "/tmp/py4127eDT", line 24, in <module> 
    result = model.predict({'attributes': {'Height': 212, 'Weight': 200}}) 
    File "/usr/local/lib/python2.7/dist-packages/NaiveBayes.py", line 152, in predict 
    scores[label] /= sumPx 
ZeroDivisionError: float division by zero 

: 숫자의 분포, 또는 충분한 샘플이없는 여기에

#!/usr/bin/env python 
import NaiveBayes 

model = NaiveBayes.NaiveBayes() 

model.set_real(['Height']) 
model.set_real(['Weight']) 
model.add_instances({'attributes': 
         {'Height': 239, 
          'Weight': 231, 
          }, 
        'cases': 32, 
        'label': 'Sex=M'}) 

model.add_instances({'attributes': 
         {'Height': 190, 
          'Weight': 152 
          }, 
        'cases': 58, 
        'label': 'Sex=F' 
        }) 

model.train() 
result = model.predict({'attributes': {'Height': 212, 'Weight': 200}}) 

print("The result is %s" % (result)) 

그리고

는 출력 ?)

+0

(어두운로 샷)이 수레로 높이와 무게를 강제하는 경우 문제가 지속됩니다? 예 : '239' 대신'239.' 등? –

+0

나는 이것을 시도했지만 일부 입력에 대해서는 작동하지만 전부는 아닙니다. 나는 이유를 모른다. – dg123

답변

3

이 두 가지 문제가 있습니다

먼저, 파이썬 2.7을 사용하고 NaiveBayes 파이썬 2 개 사단 파이썬 3. 필요는에 차례 사용 티거 (teger) 구분과 0을 반환합니다.

둘째, 레이블 당 각 속성의 인스턴스가 하나뿐이므로 sigmas는 0입니다.

import NaiveBayes 

model = NaiveBayes.NaiveBayes() 

model.set_real(['Height']) 
model.set_real(['Weight']) 
model.add_instances({'attributes': 
         {'Height': 239, 
          'Weight': 231, 
          }, 
        'cases': 32, 
        'label': 'Sex=M'}) 

model.add_instances({'attributes': 
         {'Height': 233, 
          'Weight': 234, 
          }, 
        'cases': 32, 
        'label': 'Sex=M'}) 
model.add_instances({'attributes': 
         {'Height': 190, 
          'Weight': 152 
          }, 
        'cases': 58, 
        'label': 'Sex=F' 
        }) 
model.add_instances({'attributes': 
         {'Height': 191, 
          'Weight': 153 
          }, 
        'cases': 58, 
        'label': 'Sex=F' 
        }) 

model.train() 
result = model.predict({'attributes': {'Height': 212, 'Weight': 200}}) 

print ("The result is %s" % (result)) 

을 그리고 python3를 사용하십시오 :

실제 속성에 더 많은 변화를 추가

$ python3 bayes.py 
The result is {'Sex=M': 1.0, 'Sex=F': 0.0} 
관련 문제