Last active
March 14, 2020 12:18
-
-
Save lagenorhynque/2997ba8904897b4a9d61a0270cf41b22 to your computer and use it in GitHub Desktop.
libpython-clj usage examples (Clojure ❤️ Python?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dev> (require '[libpython-clj.python :as py]) | |
nil | |
dev> (py/initialize! :python-executable venv-python | |
:library-path dylib-path) | |
... | |
dev> (require '[libpython-clj.require :refer [require-python]]) | |
nil | |
dev> (require-python '[numpy :as np] | |
'[numpy.random :as np.rand] | |
'[pandas :as pd]) | |
:ok | |
dev> (defn create-dataset [num] | |
(reduce (fn [dataset i] | |
(let [x (double (/ i (dec num))) | |
y (+ (np/sin (* 2 np/pi x)) | |
(np.rand/normal :scale 0.3))] | |
(py/py. dataset append (pd/Series [x y] :index ["x" "y"]) | |
:ignore_index true))) | |
(pd/DataFrame :columns ["x" "y"]) | |
(range num))) | |
#'dev/create-dataset | |
dev> (create-dataset 10) | |
x y | |
0 0.000000 -0.476798 | |
1 0.111111 1.041657 | |
2 0.222222 0.806179 | |
3 0.333333 0.566477 | |
4 0.444444 -0.060949 | |
5 0.555556 -0.228797 | |
6 0.666667 -1.143311 | |
7 0.777778 -0.448987 | |
8 0.888889 -0.117432 | |
9 1.000000 -0.424472 | |
dev> (py/py.- *1 y) | |
0 -0.476798 | |
1 1.041657 | |
2 0.806179 | |
3 0.566477 | |
4 -0.060949 | |
5 -0.228797 | |
6 -1.143311 | |
7 -0.448987 | |
8 -0.117432 | |
9 -0.424472 | |
Name: y, dtype: float64 | |
dev> (type *2) | |
:pyobject | |
dev> (py/python-type *3) | |
:data-frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/clj-python/libpython-clj
cf. https://github.com/enakai00/ml4se/blob/master/scripts/02-square_error.py