allows for creation of instances (and datasets) with nil values

This commit is contained in:
Ben Mabey 2011-04-05 13:34:23 -06:00
parent 94ffacaf55
commit a83e21d72b
2 changed files with 33 additions and 20 deletions

View file

@ -116,6 +116,9 @@
;; Construction of individual data and datasets
(defn- double-or-nan [x]
(if (nil? x) Double/NaN (double x)))
(defn make-instance
"Creates a new dataset instance from a vector"
([dataset vector]
@ -140,9 +143,9 @@
(if (or (keyword? val) (string? val))
;; this is a nominal entry in keyword or string form
(.setValue inst ik ^String (name val))
(.setValue inst ik (double val))))
(.setValue inst ik (double-or-nan val))))
;; A double value for the entry
(.setValue inst (int c) (double (first vs)))))
(.setValue inst (int c) (double-or-nan (first vs)))))
(recur (rest vs)
(+ c 1)))))))))

View file

@ -24,6 +24,16 @@
(is (= 1.0 (.value inst 0)))
(is (= "b1" (.stringValue inst 1)))))
(deftest make-instance-nils
(let [dataset (make-dataset :test
[:a :b]
1)
inst (make-instance dataset [1 nil])]
(is (= (class inst)
weka.core.Instance))
(is (= 2 (.numValues inst)))
(is (= 1.0 (.value inst 0)))
(is (Double/isNaN (.value inst 1)))))
(deftest dataset-make-dataset-with-default-class
(let [ds (clj-ml.data/make-dataset :test [:a :b {:c [:d :e]}] [] {:class :c})