2014-10-12 5 views
2

현재 베이지안 분류에 대한이 알고리즘을 배우고 있으며 책의 예를 따라 가려고했을 때 책의 예와 간결하지 않은 이상한 결과를 얻었습니다 .베이지안 분류, 기계 학습을위한 Clojure의 예

나는 (내가 기본적으로 손으로 복사하기 때문에)하지만 난 여전히 같은 불가능 이는 REPL에서 결과를 얻을 내 코드가 잘못 생각하지 않는다 :

> (+ (evidence-of-sea-bass) (evidence-of-salmon)) 
==> 2.8139728009700775 

그것은 1.000를 반환해야합니다 ... 작은 부동 소수점 정밀도 오차. 이 결과는 1.0로 예상되는 경우

(defn make-sea-bass [] 
    #{:sea-bass 
    (if (< (rand) 0.2) :fat :thin) 
    (if (< (rand) 0.7) :long :short) 
    (if (< (rand) 0.8) :light :dark)}) 

(defn make-salmon [] 
    #{:salmon 
    (if (< (rand) 0.8) :fat :thin) 
    (if (< (rand) 0.5) :long :short) 
    (if (< (rand) 0.3) :light :dark)}) 

(defn make-sample-fish [] 
    (if (< (rand) 0.3) (make-sea-bass) (make-salmon))) 

(def fish-training-data 
    (for [i (range 10000)] (make-sample-fish))) 

(defn probability 
    [attribute & {:keys 
       [category prior-positive prior-negative data] 
       :or {category nil 
        data fish-training-data}}] 
    (let [by-category (if category 
        (filter category data) 
        data) 
     positive (count (filter attribute by-category)) 
     negative (- (count by-category) positive) 
     total (+ positive negative)] 
    (/ positive negative))) 

(defn evidence-of-salmon [& attrs] 
    (let [attr-prob (map #(probability % :category :salmon) attrs) 
     class-and-attr-prob (conj attr-prob (probability :salmon))] 
    (float (apply * class-and-attr-prob)))) 

(defn evidence-of-sea-bass [& attrs] 
    (let [attr-prob (map #(probability % :category :sea-bass) attrs) 
     class-and-attr-prob (conj attr-prob (probability :sea-bass))] 
    (float (apply * class-and-attr-prob)))) 

답변

1

다음 확률 FN 결과가 (/ positive total)

+0

니스 감사해야합니다 :) 내가 맹목적으로 올바른을 제시하는 저자를 받아 같아요 여기

코드입니다 및 관련 코드를 예제에 추가하십시오. –

관련 문제