Changeset 21932 in project


Ignore:
Timestamp:
12/12/10 15:04:32 (9 years ago)
Author:
petercrlane
Message:

dataset-utils: information-gain exported

Location:
release/4/dataset-utils/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/4/dataset-utils/trunk/dataset-utils.scm

    r21910 r21932  
    3131          get-attribute-values filter-instances find-attribute-index
    3232          split-instances
    33           entropy
     33          entropy class-probability information-gain
    3434          dataset-utils:self-test)
    3535  (import chicken data-structures extras scheme)
     
    129129                    (* -1 prob-class (log-2 prob-class))))
    130130                (delete-duplicates (get-attribute-values relation target-class)))))
     131
     132  ;; information gain of a given attribute for relation and target-class
     133  (define (information-gain relation target-class attribute)
     134     (define (weighted-entropy split-dataset) ; compute the weighted entropy for a split dataset
     135       (/ (* (number-instances split-dataset) (entropy split-dataset target-class))
     136          (number-instances relation)))
     137      (- (entropy relation target-class) ; the initial entropy
     138         (apply + ; add up the weighted entropies
     139                (map weighted-entropy (split-instances relation attribute)))))
    131140 
    132141  ; compute the log of a number in base 2, checking for 0
  • release/4/dataset-utils/trunk/tests/run.scm

    r21884 r21932  
    5353       => 5)
    5454
     55(check (class-probability weather 'play 'yes)
     56       => 9/14)
     57
     58(check (class-probability weather 'play 'no)
     59       => 5/14)
     60
    5561(check (entropy weather 'play)
    5662       (=> =5) 0.940285958670631)
     
    5965
    6066;; uncomment for released version - replace N with number of tests
    61 (if (check-passed? 27)
     67(if (check-passed? 29)
    6268  (begin
    6369    (display "All tests passed") (newline)
Note: See TracChangeset for help on using the changeset viewer.