Skip to content
Snippets Groups Projects
Commit 768ade61 authored by Simon Belak's avatar Simon Belak
Browse files

minor refactor

parent d536784c
No related branches found
No related tags found
No related merge requests found
......@@ -195,8 +195,8 @@
(merge
{:histogram h/histogram
:cardinality cardinality
:kurtosis (redux/pre-step stats/kurtosis (somef double))
:skewness (redux/pre-step stats/skewness (somef double))
:kurtosis ((map (somef double)) stats/kurtosis)
:skewness ((map (somef double)) stats/skewness)
:zeros ((filter (somef zero?)) stats/count)}
(when (costs/full-scan? max-cost)
{:sum ((keep (somef double)) +)
......@@ -312,7 +312,8 @@
(defmethod feature-extractor [DateTime Num]
[{:keys [max-cost query]} field]
(redux/post-complete
(redux/pre-step
((map (fn [[^java.util.Date x y]]
[(some-> x .getTime double) y]))
(redux/fuse {; y = a + b*x
:linear-regression (stats/simple-linear-regression
first second)
......@@ -323,9 +324,7 @@
; y = a + b*ln(x)
:log-linear-regression (stats/simple-linear-regression
#(Math/log (first %)) second)
:series conj})
(fn [[^java.util.Date x y]]
[(some-> x .getTime double) y]))
:series conj}))
(merge-juxt
(field-metadata-extractor field)
(fn [{:keys [series linear-regression power-law-regression
......@@ -347,7 +346,6 @@
(math/ssr (fn [x]
(+ a (* b (Math/log x))))))})
(fn [fits]
(println fits)
(let [[model ssr] (apply min-key val fits)]
{:model model
:ssr ssr
......@@ -456,9 +454,8 @@
(defmethod feature-extractor Text
[_ field]
(redux/post-complete
(redux/fuse {:histogram (redux/pre-step
h/histogram
(somef (comp count u/jdbc-clob->str)))})
(redux/fuse {:histogram ((map (somef (comp count u/jdbc-clob->str)))
h/histogram)})
(merge-juxt
(field-metadata-extractor field)
histogram-extractor)))
......@@ -466,25 +463,19 @@
(defmethod feature-extractor DateTime
[_ {:keys [base_type unit] :as field}]
(redux/post-complete
(redux/fuse (merge
{:histogram (redux/pre-step
h/histogram
(somef (memfn ^java.util.Date getTime)))
:histogram-day (redux/pre-step
h/histogram-categorical
(somef (memfn ^java.util.Date getDay)))
:histogram-month (redux/pre-step
h/histogram-categorical
#(when %
(inc (.getMonth ^java.util.Date %))))
:histogram-quarter (redux/pre-step
h/histogram-categorical
(somef ts/quarter))}
(when-not (or (isa? base_type :type/Date)
(#{:day :month :year :quarter :week} unit))
{:histogram-hour (redux/pre-step
h/histogram-categorical
(somef (memfn ^java.util.Date getHours)))})))
(redux/fuse
(merge
{:histogram ((map (somef (memfn ^java.util.Date getTime)))
h/histogram)
:histogram-day ((map (somef (memfn ^java.util.Date getDay)))
h/histogram-categorical)
:histogram-month ((map #(some->> ^java.util.Date % .getMonth inc))
h/histogram-categorical)
:histogram-quarter ((map (somef ts/quarter)) h/histogram-categorical)}
(when-not (or (isa? base_type :type/Date)
(#{:day :month :year :quarter :week} unit))
{:histogram-hour ((map (somef (memfn ^java.util.Date getHours)))
h/histogram-categorical)})))
(merge-juxt
histogram-extractor
(field-metadata-extractor field)
......@@ -552,8 +543,8 @@
(for [[i field] (m/indexed cols)
:when (not (or (:remapped_to field)
(= :type/PK (:special_type field))))]
[(:name field) (redux/pre-step (feature-extractor opts field)
#(nth % i))])))
[(:name field) ((map #(nth % i))
(feature-extractor opts field))])))
rows))
(defmethod feature-extractor :default
......
......@@ -92,10 +92,10 @@
(h.impl/mean histogram))])))
(redux/post-complete
(redux/juxt (stats/sum-squares first second)
(redux/fuse {:s-x (redux/pre-step + first)
:s-xx (redux/pre-step + (comp sq first))
:s-y (redux/pre-step + second)
:s-yy (redux/pre-step + (comp sq second))}))
(redux/fuse {:s-x ((map first) +)
:s-xx ((map (comp sq first)) +)
:s-y ((map second) +)
:s-yy ((map (comp sq second)) +)}))
(fn [[{:keys [ss-xy ss-x n]} {:keys [s-x s-xx s-y s-yy]}]]
(when (and (> n 2) (not-any? zero? [ss-x s-x]))
(let [slope (/ ss-xy ss-x)
......
......@@ -78,13 +78,14 @@
"Transducer that calculates residual sum of squares.
https://en.wikipedia.org/wiki/Residual_sum_of_squares"
[model]
(redux/pre-step + (fn [[x y]]
(math/sq (- y (model x))))))
((map (fn [[x y]]
(math/sq (- y (model x)))))
+))
(def magnitude
"Transducer that claclulates magnitude (Euclidean norm) of given vector.
https://en.wikipedia.org/wiki/Euclidean_distance"
(redux/post-complete (redux/pre-step + math/sq) math/sqrt))
(redux/post-complete ((map math/sq) +) math/sqrt))
(defn cosine-distance
"Cosine distance between vectors `a` and `b`.
......@@ -92,9 +93,9 @@
[a b]
(transduce identity
(redux/post-complete
(redux/fuse {:magnitude-a (redux/pre-step magnitude first)
:magnitude-b (redux/pre-step magnitude second)
:product (redux/pre-step + (partial apply *))})
(redux/fuse {:magnitude-a ((map first) magnitude)
:magnitude-b ((map second) magnitude)
:product ((map (partial apply *)) +)})
(fn [{:keys [magnitude-a magnitude-b product]}]
(some->> (safe-divide product magnitude-a magnitude-b) (- 1))))
(map (comp (partial map double) vector) a b)))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment