Changed classifier-classify to produce the class label; updated classifier-label to avoid using classifier-classify.

This commit is contained in:
Joshua Eckroth 2013-08-06 03:41:12 -04:00
parent 53d141019f
commit 8aeed64130
2 changed files with 48 additions and 51 deletions

View file

@ -704,39 +704,16 @@
;; Classifying instances
(defn classifier-classify
"Classifies an instance using the provided classifier.
The value returned is the numeric attribute of that value for
the list of valid values for the class."
"Classifies an instance using the provided classifier. Returns the
class as a keyword."
([^Classifier classifier ^Instance instance]
(.classifyInstance classifier instance)))
(let [pred (.classifyInstance classifier instance)]
(keyword (.value (.classAttribute instance) pred)))))
(defn classifier-label
"Classifies and assign a label to a dataset instance.
This function is similar to classifier-classify but
instead of just returning the numeric identifier for the
new instance, it changes the class value for that instance
to the newly assigned by the classifier.
The function returns the newly classified instance.
This call is destructive, the instance passed as an argument
is modified.
; We create the instance to classify
(def *to-classify* (make-instance *dataset* {:class :Iris-versicolor
:petalwidth 0.2
:petallength 1.4
:sepalwidth 3.5
:sepallength 5.1}))
; We use the classifier to check the value for the class
(classifier-classify *classifier* *to-classify*)
>0.0
; We change the class for the instance according to the assigned class
(classifier-label *classifier* *to-classify*)
>#<Instance 5.1,3.5,1.4,0.2,Iris-setosa>
"
([classifier instance]
(let [cls (classifier-classify classifier instance)]
(instance-set-class instance cls))))
The function returns the newly classified instance. This call is
destructive, the instance passed as an argument is modified."
([^Classifier classifier ^Instance instance]
(let [cls (.classifyInstance classifier instance)]
(doto instance (.setClassValue cls)))))

View file

@ -16,8 +16,8 @@
(deftest train-classifier-ibk
(let [c (make-classifier :lazy :ibk)
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])]
(clj-ml.data/dataset-set-class ds 2)
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])]
(dataset-set-class ds 2)
(classifier-train c ds)
(is true)))
@ -37,35 +37,35 @@
(deftest train-classifier-c45
(let [c (make-classifier :decision-tree :c45)
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])]
(clj-ml.data/dataset-set-class ds 2)
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])]
(dataset-set-class ds 2)
(classifier-train c ds)
(is true)))
(deftest make-classifier-bayes
(fact
(let [c (clj-ml.classifiers/make-classifier :bayes :naive {:kernel-estimator true :old-format true})
(let [c (make-classifier :bayes :naive {:kernel-estimator true :old-format true})
opts (vec (.getOptions c))]
opts => (contains ["-K" "-O"]))))
(deftest make-classifier-bayes-updateable
(let [c (clj-ml.classifiers/make-classifier :bayes :naive {:updateable true})]
(let [c (make-classifier :bayes :naive {:updateable true})]
(is (= (class c)
weka.classifiers.bayes.NaiveBayesUpdateable))))
(deftest train-classifier-bayes
(let [c (clj-ml.classifiers/make-classifier :bayes :naive {:kernel-estimator true :old-format true})
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])]
(clj-ml.data/dataset-set-class ds 2)
(let [c (make-classifier :bayes :naive {:kernel-estimator true :old-format true})
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])]
(dataset-set-class ds 2)
(classifier-train c ds)
(is true)))
(deftest classifier-evaluate-dataset
(let [c (make-classifier :decision-tree :c45)
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
tds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[4 1 :n] [4 5 :m]])
_ (clj-ml.data/dataset-set-class ds 2)
_ (clj-ml.data/dataset-set-class tds 2)
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
tds (make-dataset "test" [:a :b {:c [:m :n]}] [[4 1 :n] [4 5 :m]])
_ (dataset-set-class ds 2)
_ (dataset-set-class tds 2)
_ (classifier-train c ds)
res (classifier-evaluate c :dataset ds tds)]
(is (= 28 (count (keys res))))))
@ -77,22 +77,42 @@
(deftest classifier-evaluate-cross-validation
(let [c (make-classifier :decision-tree :c45)
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
_ (clj-ml.data/dataset-set-class ds 2)
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
_ (dataset-set-class ds 2)
_ (classifier-train c ds)
res (classifier-evaluate c :cross-validation ds 2)]
(is (= 28 (count (keys res))))))
(deftest classifier-evaluate-cross-validation-grid
(let [c (make-classifier :support-vector-machine :libsvm-grid)
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
_ (clj-ml.data/dataset-set-class ds 2)
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
_ (dataset-set-class ds 2)
res (classifier-evaluate c :cross-validation ds 2)]
(is (= 28 (count (keys res))))))
(deftest test-classifier-classify
(let [c (make-classifier :decision-tree :c45)
ds (-> (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
(dataset-set-class 2))
inst (-> (first (dataset-seq ds))
(instance-set-class-missing))]
(classifier-train c ds)
(is (= :m (classifier-classify c inst)))))
(deftest test-classifier-label
(let [c (make-classifier :decision-tree :c45)
ds (-> (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
(dataset-set-class 2))
inst (-> (first (dataset-seq ds))
(instance-set-class-missing))]
(is (= nil (instance-get-class inst)))
(classifier-train c ds)
(classifier-label c inst)
(is (= :m (instance-get-class inst)))))
(deftest update-updateable-classifier
(let [c (clj-ml.classifiers/make-classifier :bayes :naive {:updateable true})
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
(let [c (make-classifier :bayes :naive {:updateable true})
ds (make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
_ (dataset-set-class ds 2)
inst (make-instance ds {:a 56 :b 45 :c :m})]
(classifier-train c ds)