standardizes how attributes can be asked for (by index or name)
This commit is contained in:
parent
99048715f1
commit
652c54aaf7
2 changed files with 34 additions and 12 deletions
|
@ -30,13 +30,6 @@
|
||||||
(instance? weka.core.Instances dataset))
|
(instance? weka.core.Instances dataset))
|
||||||
|
|
||||||
|
|
||||||
(defn attribute-name-at
|
|
||||||
"Returns the name of an attribute situated at the provided position in
|
|
||||||
the attributes definition of an instance or class"
|
|
||||||
[dataset-or-instance pos]
|
|
||||||
(let [class-attr (.attribute dataset-or-instance pos)]
|
|
||||||
(.name class-attr)))
|
|
||||||
|
|
||||||
(defn index-attr
|
(defn index-attr
|
||||||
"Returns the index of an attribute in the attributes definition of an
|
"Returns the index of an attribute in the attributes definition of an
|
||||||
instance or dataset"
|
instance or dataset"
|
||||||
|
@ -46,6 +39,19 @@
|
||||||
(let [attr-name (name attr-name)]
|
(let [attr-name (name attr-name)]
|
||||||
(find-first #(= attr-name (.name (.attribute dataset %))) (range (.numAttributes dataset))))))
|
(find-first #(= attr-name (.name (.attribute dataset %))) (range (.numAttributes dataset))))))
|
||||||
|
|
||||||
|
(defn attribute-at
|
||||||
|
"Returns the name of an attribute situated at the provided position in
|
||||||
|
the attributes definition of an instance or class"
|
||||||
|
[dataset-or-instance index-or-name]
|
||||||
|
(.attribute dataset-or-instance (index-attr dataset-or-instance index-or-name)))
|
||||||
|
|
||||||
|
(defn attribute-name-at
|
||||||
|
"Returns the name of an attribute situated at the provided position in
|
||||||
|
the attributes definition of an instance or class"
|
||||||
|
[dataset-or-instance index-or-name]
|
||||||
|
(let [^Attribute class-attr (attribute-at dataset-or-instance index-or-name)]
|
||||||
|
(.name class-attr)))
|
||||||
|
|
||||||
(defn attributes
|
(defn attributes
|
||||||
"Returns the attributes (weka.core.Attribute) of the dataset or instance"
|
"Returns the attributes (weka.core.Attribute) of the dataset or instance"
|
||||||
[dataset-or-instance]
|
[dataset-or-instance]
|
||||||
|
@ -196,17 +202,32 @@
|
||||||
(conj acum {(keyword val) index})))
|
(conj acum {(keyword val) index})))
|
||||||
acum))))
|
acum))))
|
||||||
|
|
||||||
(defn dataset-labels-at [dataset-or-instance pos]
|
(defn dataset-labels-at [dataset-or-instance index-or-name]
|
||||||
"Returns the lables (possible values) for a nominal attribute at the provided position"
|
"Returns the lables (possible values) for a nominal attribute at the provided position"
|
||||||
(let [class-attr (.attribute dataset-or-instance pos)
|
(let [attr (attribute-at dataset-or-instance index-or-name)
|
||||||
values (.enumerateValues class-attr)]
|
values (.enumerateValues attr)]
|
||||||
(if (nil? values)
|
(if (nil? values)
|
||||||
:not-nominal
|
:not-nominal
|
||||||
(loop [continue (.hasMoreElements values)
|
(loop [continue (.hasMoreElements values)
|
||||||
acum {}]
|
acum {}]
|
||||||
(if continue
|
(if continue
|
||||||
(let [val (.nextElement values)
|
(let [val (.nextElement values)
|
||||||
index (.indexOfValue class-attr val)]
|
index (.indexOfValue attr val)]
|
||||||
|
(recur (.hasMoreElements values)
|
||||||
|
(conj acum {(keyword val) index})))
|
||||||
|
acum)))))
|
||||||
|
|
||||||
|
#_(defn dataset-labels-at [dataset-or-instance index-or-name]
|
||||||
|
"Returns the lables (possible values) for a nominal attribute at the provided position"
|
||||||
|
(let [attr (attribute-at dataset-or-instance index-or-name)
|
||||||
|
values (.enumerateValues attr)]
|
||||||
|
(if (nil? values)
|
||||||
|
:not-nominal
|
||||||
|
(loop [continue (.hasMoreElements values)
|
||||||
|
acum {}]
|
||||||
|
(if continue
|
||||||
|
(let [val (.nextElement values)
|
||||||
|
index (.indexOfValue attr val)]
|
||||||
(recur (.hasMoreElements values)
|
(recur (.hasMoreElements values)
|
||||||
(conj acum {(keyword val) index})))
|
(conj acum {(keyword val) index})))
|
||||||
acum)))))
|
acum)))))
|
||||||
|
|
|
@ -136,6 +136,7 @@
|
||||||
(let [ds (make-dataset "test" [:a :b {:c [:d :e]}] [{:a 1 :b 2 :c :d} [4 5 :e]])
|
(let [ds (make-dataset "test" [:a :b {:c [:d :e]}] [{:a 1 :b 2 :c :d} [4 5 :e]])
|
||||||
attrs (attributes ds)]
|
attrs (attributes ds)]
|
||||||
(is (every? #(instance? weka.core.Attribute %) attrs))
|
(is (every? #(instance? weka.core.Attribute %) attrs))
|
||||||
|
(is (= (first attrs) (attribute-at ds 0) (attribute-at ds :a)))
|
||||||
(is (= '("a" "b" "c") (map #(.name %) attrs)))
|
(is (= '("a" "b" "c") (map #(.name %) attrs)))
|
||||||
(is (= '("a" "b" "c") (map #(.name %) (attributes (dataset-at ds 0)))))
|
(is (= '("a" "b" "c") (map #(.name %) (attributes (dataset-at ds 0)))))
|
||||||
(is (= [(.attribute ds 2)] (nominal-attributes ds)))
|
(is (= [(.attribute ds 2)] (nominal-attributes ds)))
|
||||||
|
@ -151,4 +152,4 @@
|
||||||
(let [ds (make-dataset "test" [:a :b {:c [:d :e]}]
|
(let [ds (make-dataset "test" [:a :b {:c [:d :e]}]
|
||||||
[{:a 1 :b 2 :c :d} [4 5 :e]])]
|
[{:a 1 :b 2 :c :d} [4 5 :e]])]
|
||||||
(dataset-set-class ds :c)
|
(dataset-set-class ds :c)
|
||||||
(is (= {:d 0 :e 1} (dataset-class-labels ds) (dataset-labels-at ds 2)))))
|
(is (= {:d 0 :e 1} (dataset-class-labels ds) (dataset-labels-at ds :c)))))
|
||||||
|
|
Loading…
Reference in a new issue