Skip to content

Commit

Permalink
cleaned up ns
Browse files Browse the repository at this point in the history
  • Loading branch information
behrica committed Apr 14, 2023
1 parent 5fd5801 commit 4726d06
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 79 deletions.
44 changes: 12 additions & 32 deletions src/scicloj/sklearn_clj.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(ns scicloj.sklearn-clj
(:require
[camel-snake-kebab.core :as csk]
[libpython-clj2.require :refer [require-python]]
[clojure.string :as str]
clojure.walk
[libpython-clj2.python
:as py
:refer [as-jvm as-python cfn path->py-obj python-type py. py.-]]
:refer [cfn path->py-obj python-type py.]]
[libpython-clj2.python.np-array]
[tech.v3.dataset :as ds]
[tech.v3.dataset.column-filters :as cf]
[tech.v3.dataset.modelling :as ds-mod]
[tech.v3.dataset.tensor :as dst]
[tech.v3.datatype.errors :as errors]
[tech.v3.tensor :as t]
[tech.v3.datatype :as dt]
[libpython-clj2.python.np-array]))
tech.v3.datatype.casting
[tech.v3.datatype.errors :as errors]))


(defn assert-numeric-ds [ds]
Expand All @@ -38,6 +38,7 @@
(prn (str ~label " - " "elapsed time: " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs"))
ret#))

(py/initialize!)
(println "'sklearn' version found: "
(get
(py/module-dict (py/import-module "sklearn"))
Expand Down Expand Up @@ -186,13 +187,11 @@
"Calls `predict_proba` on the given sklearn estimator object, and returns the result as a tech.ml.dataset"
([ds estimator inference-target-column-names]
(let
[_ (def ds ds)
first-target-column-name (first inference-target-column-names)
[first-target-column-name (first inference-target-column-names)
_ (errors/when-not-error first-target-column-name "No inference target column name given")
feature-ds (cf/feature ds)
X (ds->X feature-ds)
prob-prediction (py. estimator predict_proba X)]

X (ds->X feature-ds)]

(->
(py. estimator predict_proba X)
dst/tensor->dataset
Expand Down Expand Up @@ -222,8 +221,8 @@
(defn model-attribute-names [sklearn-model]

(->> (py/dir sklearn-model)
(filter #(and (clojure.string/ends-with? % "_")
(not (clojure.string/starts-with? % "_"))))))
(filter #(and (str/ends-with? % "_")
(not (str/starts-with? % "_"))))))

(defn save-py-get-attr [sklearn-model attr]
;; can fail in some cases
Expand All @@ -247,28 +246,9 @@


(defn model-attributes [sklearn-model]
(def sklearn-model sklearn-model)
(apply merge
(map
(fn [attr]
(hash-map (keyword attr)
(save->jvm (save-py-get-attr sklearn-model attr))))
(model-attribute-names sklearn-model))))

(comment

(def big
(ds/->dataset
(apply merge
(map
#(hash-map % (seq (range 10000)))
(range 10000)))))

(println
(labeled-time
:numeric-ds<->ndarray-as
(->
big
numeric-ds->ndarray
ndarray->ds
(ds/shape)))))
5 changes: 0 additions & 5 deletions src/scicloj/sklearn_clj/metamorph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
(sklearn/transform data estimator kw-args))))))))







(defn fit-transform
([module-kw estimator-class-kw]
(fit-transform module-kw estimator-class-kw {}))
Expand Down
8 changes: 2 additions & 6 deletions src/scicloj/sklearn_clj/ml.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
[libpython-clj2.python :refer [->jvm as-jvm cfn path->py-obj py.- py.] :as py]
[scicloj.metamorph.ml :as ml]
[scicloj.sklearn-clj :as sklearn]
[tech.v3.datatype.functional :as dtf]
[tech.v3.dataset :as ds]
[tech.v3.dataset.modelling :as ds-mod]
[tech.v3.dataset.categorical :as ds-cat]))
[tech.v3.dataset.modelling :as ds-mod]))

(py/initialize!)
(def pickle (py/import-module "pickle"))
(def filter-map
{"classifier" "classification"
Expand Down Expand Up @@ -102,9 +101,6 @@

names)))




(define-estimators! "regressor")
(define-estimators! "classifier")

4 changes: 1 addition & 3 deletions src/scicloj/sklearn_clj/sklearn_wrapper.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
[scicloj.metamorph.core :as mm]
[scicloj.sklearn-clj]
[tablecloth.api :as tc]
[tech.v3.dataset :as ds]
[tech.v3.dataset.categorical :as ds-cat]
[tech.v3.dataset.column-filters :as cf]))
[tech.v3.dataset :as ds]))


(py/initialize!)
Expand Down
2 changes: 1 addition & 1 deletion test/scicloj/cat_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
[scicloj.sklearn-clj.ml]
[clojure.test :refer [deftest is]]))


(py/initialize!)
(def builtins (py/import-module "builtins"))


Expand Down
22 changes: 10 additions & 12 deletions test/scicloj/metamorph_test.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
(ns scicloj.metamorph-test
(:require
[scicloj.sklearn-clj.ml]
[tech.v3.dataset.modelling :as ds-mod]
[tech.v3.dataset :as ds]
[tech.v3.dataset.tensor :as dst]
[tech.v3.tensor :as dtt]
[tablecloth.api :as tc]
[libpython-clj2.python :refer [->jvm py.- py.
python-type]]
[libpython-clj2.require :refer [require-python]]
[scicloj.sklearn-clj.metamorph :as sklearn-mm]
[clojure.test :refer [deftest is]]
[libpython-clj2.python :refer [->jvm]
:as py]
[libpython-clj2.require :refer [require-python]]
[scicloj.metamorph.core :as morph]
[scicloj.metamorph.ml :as mm-ml]
[scicloj.sklearn-clj.metamorph :as sklearn-mm]
[scicloj.sklearn-clj.ml]
[tablecloth.api :as tc]
[tech.v3.dataset :as ds]
[tech.v3.dataset.column-filters :as ds-cf]
[scicloj.metamorph.core :as morph]))
[tech.v3.dataset.modelling :as ds-mod]))



(py/initialize!)
(deftest test-evaluate
(let [XY (->
(tc/dataset [ [-1, -1], [-2, -1], [1, 1], [2, 1]]
Expand Down
6 changes: 3 additions & 3 deletions test/scicloj/perf_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[libpython-clj2.python.np-array]
[scicloj.sklearn-clj :refer [fit]]
[libpython-clj2.python :as py]))

(py/initialize!)
(defmacro labeled-time
"Evaluates expr and prints the time it took. Returns the value of
expr."
Expand Down Expand Up @@ -44,5 +44,5 @@
_ (fit train :sklearn.ensemble :GradientBoostingClassifier)
end (System/currentTimeMillis)
clojure-time (/ (- end start) 1000)]
(t/is (< python-time 20))
(t/is (< clojure-time 20))))
(t/is (< python-time 21))
(t/is (< clojure-time 21))))
32 changes: 15 additions & 17 deletions test/scicloj/sklearn_clj_test.clj
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
(ns scicloj.sklearn-clj-test
(:require [clojure.test :refer :all]
[tech.v3.dataset :as ds]
[tech.v3.dataset.modelling :as ds-mod]
[tablecloth.api :as tc]
[tech.v3.dataset.tensor :as dst]
[tech.v3.tensor :as tensor]
[libpython-clj2.python :refer [python-type] :as py]
[tech.v3.datatype.functional :as f]
[scicloj.metamorph.ml.toydata :as toydata]
[scicloj.metamorph.ml :as ml]
[scicloj.metamorph.core :as mm]
[scicloj.sklearn-clj :refer :all]
[tablecloth.pipeline :as tcm]
[tech.v3.dataset.metamorph :as ds-mm]
[tech.v3.dataset.column-filters :as cf]
[scicloj.sklearn-clj.ml]))
(:require
[clojure.test :refer :all]
[libpython-clj2.python :refer [python-type] :as py]
[scicloj.metamorph.core :as mm]
[scicloj.metamorph.ml :as ml]
[scicloj.metamorph.ml.toydata :as toydata]
[scicloj.sklearn-clj :refer :all]
[scicloj.sklearn-clj.ml]
[tablecloth.api :as tc]
[tech.v3.dataset :as ds]
[tech.v3.dataset.modelling :as ds-mod]
[tech.v3.dataset.tensor :as dst]
[tech.v3.datatype.functional :as f]
[tech.v3.tensor :as tensor]))



(py/initialize!)
(def texts ["This is the first document."
"This document is the second document."
"And this is the third one."
Expand Down

0 comments on commit 4726d06

Please sign in to comment.