Permalink
Browse files

similar options refactorings in classifiers that was done in filters

  • Loading branch information...
1 parent 6dec810 commit 6ec5f1f2a079d094463838d3bfa5477f9dd4d74b @bmabey bmabey committed Nov 1, 2010
Showing with 48 additions and 88 deletions.
  1. +39 −50 src/clj_ml/classifiers.clj
  2. +9 −38 test/clj_ml/classifiers_test.clj
View
@@ -74,67 +74,56 @@
(defmulti #^{:skip-wiki true}
make-classifier-options
- "Creates the right parameters for a classifier"
+ "Creates the right parameters for a classifier. Returns the parameters as a Clojure vector."
(fn [kind algorithm map] [kind algorithm]))
(defmethod make-classifier-options [:decission-tree :c45]
([kind algorithm m]
- (let [cols-val (check-options m
- {:unpruned "-U"
- :reduced-error-pruning "-R"
- :only-binary-splits "-B"
- :no-raising "-S"
- :no-cleanup "-L"
- :laplace-smoothing "-A"}
- [""])
- cols-val-a (check-option-values m
- {:pruning-confidence "-C"
- :minimum-instances "-M"
- :pruning-number-folds "-N"
- :random-seed "-Q"}
- cols-val)]
- (into-array cols-val-a))))
+ (->> (check-options m
+ {:unpruned "-U"
+ :reduced-error-pruning "-R"
+ :only-binary-splits "-B"
+ :no-raising "-S"
+ :no-cleanup "-L"
+ :laplace-smoothing "-A"})
+ (check-option-values m
+ {:pruning-confidence "-C"
+ :minimum-instances "-M"
+ :pruning-number-folds "-N"
+ :random-seed "-Q"}))))
(defmethod make-classifier-options [:bayes :naive]
([kind algorithm m]
- (let [cols-val (check-options m
- {:kernel-estimator "-K"
- :supervised-discretization "-D"
- :old-format "-O"}
- [""])]
- (into-array cols-val))))
+ (check-options m
+ {:kernel-estimator "-K"
+ :supervised-discretization "-D"
+ :old-format "-O"})))
(defmethod make-classifier-options [:neural-network :multilayer-perceptron]
([kind algorithm m]
- (let [cols-val (check-options m
- {:no-nominal-to-binary "-B"
- :no-numeric-normalization "-C"
- :no-normalization "-I"
- :no-reset "-R"
- :learning-rate-decay "-D"}
- [""])
- cols-val-a (check-option-values m
- {:learning-rate "-L"
- :momentum "-M"
- :epochs "-N"
- :percentage-validation-set "-V"
- :random-seed "-S"
- :threshold-number-errors "-E"}
- cols-val)]
- (into-array cols-val-a))))
+ (->> (check-options m
+ {:no-nominal-to-binary "-B"
+ :no-numeric-normalization "-C"
+ :no-normalization "-I"
+ :no-reset "-R"
+ :learning-rate-decay "-D"})
+ (check-option-values m
+ {:learning-rate "-L"
+ :momentum "-M"
+ :epochs "-N"
+ :percentage-validation-set "-V"
+ :random-seed "-S"
+ :threshold-number-errors "-E"}))))
(defmethod make-classifier-options [:support-vector-machine :smo]
([kind algorithm m]
- (let [cols-val (check-options m {:fit-logistic-models "-M"}
- [""])
- cols-val-a (check-option-values m
- {:complexity-constant "-C"
- :tolerance "-L"
- :epsilon-roundoff "-P"
- :folds-for-cross-validation "-V"
- :random-seed "-W"}
- cols-val)]
- (into-array cols-val-a))))
+ (->> (check-options m {:fit-logistic-models "-M"})
+ (check-option-values m
+ {:complexity-constant "-C"
+ :tolerance "-L"
+ :epsilon-roundoff "-P"
+ :folds-for-cross-validation "-V"
+ :random-seed "-W"}))))
;; Building classifiers
@@ -145,7 +134,7 @@
([kind algorithm classifier-class options]
`(let [options-read# (if (empty? ~options) {} (first ~options))
classifier# (new ~classifier-class)
- opts# (make-classifier-options ~kind ~algorithm options-read#)]
+ opts# (into-array String (make-classifier-options ~kind ~algorithm options-read#))]
(.setOptions classifier# opts#)
classifier#)))
@@ -289,7 +278,7 @@
([kind algorithm & options]
(let [options-read (if (empty? options) {} (first options))
classifier (new SMO)
- opts (make-classifier-options :support-vector-machine :smo options-read)]
+ opts (into-array String (make-classifier-options :support-vector-machine :smo options-read))]
(.setOptions classifier opts)
(when (not (empty? (get options-read :kernel-function)))
;; We have to setup a different kernel function
@@ -7,36 +7,7 @@
(let [options (make-classifier-options :decission-tree :c45 {:unpruned true :reduced-error-pruning true :only-binary-splits true :no-raising true
:no-cleanup true :laplace-smoothing true :pruning-confidence 0.12 :minimum-instances 10
:pruning-number-folds 5 :random-seed 1})]
- (is (= (aget options 0)
- ""))
- (is (= (aget options 1)
- "-U"))
- (is (= (aget options 2)
- "-R"))
- (is (= (aget options 3)
- "-B"))
- (is (= (aget options 4)
- "-S"))
- (is (= (aget options 5)
- "-L"))
- (is (= (aget options 6)
- "-A"))
- (is (= (aget options 7)
- "-C"))
- (is (= (aget options 8)
- "0.12"))
- (is (= (aget options 9)
- "-M"))
- (is (= (aget options 10)
- "10"))
- (is (= (aget options 11)
- "-N"))
- (is (= (aget options 12)
- "5"))
- (is (= (aget options 13)
- "-Q"))
- (is (= (aget options 14)
- "1"))))
+ (is (= options ["-U" "-R" "-B" "-S" "-L" "-A" "-C" "0.12" "-M" "10" "-N" "5" "-Q" "1"]))))
(deftest make-classifier-c45
@@ -71,12 +42,12 @@
(deftest classifier-evaluate-dataset
- (let [c (make-classifier :decission-tree :c45)
- ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
+ (let [c (make-classifier :decission-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]])
- foo1(clj-ml.data/dataset-set-class ds 2)
- foo2(clj-ml.data/dataset-set-class tds 2)
- foo2 (classifier-train c ds)
+ _ (clj-ml.data/dataset-set-class ds 2)
+ _ (clj-ml.data/dataset-set-class tds 2)
+ _ (classifier-train c ds)
res (classifier-evaluate c :dataset ds tds)]
(is (= 26 (count (keys res))))))
@@ -89,15 +60,15 @@
(deftest classifier-evaluate-cross-validation
(let [c (make-classifier :decission-tree :c45)
ds (clj-ml.data/make-dataset "test" [:a :b {:c [:m :n]}] [[1 2 :m] [4 5 :m]])
- foo1(clj-ml.data/dataset-set-class ds 2)
- foo2 (classifier-train c ds)
+ _ (clj-ml.data/dataset-set-class ds 2)
+ _ (classifier-train c ds)
res (classifier-evaluate c :cross-validation ds 2)]
(is (= 26 (count (keys res))))))
(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]])
- _foo1 (dataset-set-class ds 2)
+ _ (dataset-set-class ds 2)
inst (make-instance ds {:a 56 :b 45 :c :m})]
(classifier-train c ds)
(classifier-update c ds)

0 comments on commit 6ec5f1f

Please sign in to comment.