Clojure/Incanter/clj-mlã§ãã¼ã¿ãã¤ãã³ã°å ¥é
æ¦è¦
Clojureã§ãã¼ã¿ãã¤ãã³ã°ã«å¿
è¦ãªåææ³ã解説ããè¨äºã§ãã
æ¬è¨äºãèªãã¨ãå
¨ãClojureãç¥ããªãæ¹ã§ã
- ãã¼ã¿æ½åºã»éè¨
- å¯è¦å
- æ©æ¢°å¦ç¿ï¼æ±ºå®æ¨ãã©ã³ãã ãã©ã¬ã¹ããk-meansã¯ã©ã¹ã¿ãªã³ã°)
ãClojureã§å®è¡ã§ããããã«ãªãã¾ãã
ã¯ããã«
Clojureã¨ã¯JVMä¸ã§åãï¼ã¤ã¾ãOSãåãã沢山ã®ç°å¢ã§åãä¸ã«
Javaè³ç£ããã®ã¾ã¾ä½¿ããï¼Lispç³»ã®è¨èªã§ãã
Clojureã§ã¯ãã¼ã¿ãã¤ãã³ã°ãè¡ãéã
Incanterã¨clj-mlã¨ãã2ã¤ã®ä¾¿å©ãªãã¼ã«ãããã¾ãã
Incanterã¯çµ±è¨å¦çç¨ã®å°éã©ã¤ãã©ãªã§ã
åä½ã§æ§ã
ãªçµ±è¨åæãè¡ã£ããéè¨ã便å©ã«ãããããæ©è½ã沢山ç¨æããã¦ãã¾ãã
clj-mlã¯wekaã¨ããæ©æ¢°å¦ç¿ç³»ã®ãã¼ã«ãClojureã§ç°¡åã«ä½¿ããããã«ããã©ããã¼ã§ã
決å®æ¨ãã©ã³ãã ãã©ã¬ã¹ããªã©ã®åé¡å¨ã
å種ã¯ã©ã¹ã¿ãªã³ã°ææ³ãå©ç¨åºæ¥ãããã«ãªãã¾ãã
ãã®è¨äºã§ã¯ClojureããIncanterã¨clj-mlãç¨ãã¦
ç°¡åã«ãã¼ã¿ãã¤ãã³ã°ãè¡ãæé ã«ã¤ãã¦èª¬æãã¾ãã
åææ³ãã³ã¼ãã®è©³ç´°ã¯è¿½ããã
Clojureã§ä½ãåºæ¥ãã®ãã俯ç°ããããã®å
¥éè¨äºã§ãã
Clojureã¨leiningenã®ã¤ã³ã¹ãã¼ã«
leiningenã¯Clojureã®æ¨æºçãªãã«ããã¼ã«ã§ã
ãããå
¥ããã¨Clojureæ¬ä½ããå種ãã¼ã«ã®ã¤ã³ã¹ãã¼ã«ã
ããã¸ã§ã¯ãã®èµ·åããã«ããªã©æ§ã
ãªãã¨ãåºæ¥ã¦ä¾¿å©ã§ãã
以ä¸ã®ããã¥ã¡ã³ãéãã«ããã°ç°¡åã«ã¤ã³ã¹ãã¼ã«åºæ¥ã¾ãã
http://marui.hatenablog.com/entry/2014/02/15/113838
http://kaosf.github.io/20140209-clojure-getting-started/#/
ç¹ã«windowsã§ã¯æ¬¡ã®ãã¼ã«ã使ãã¨
ãã¦ã¹ãã½ã¡ã½ã¡ãã¦ãã ãã§ã¤ã³ã¹ãã¼ã«å®äºã§ãã
http://leiningen-win-installer.djpowell.net/
incanterã¨clj-mlã®è¨å®
ä»»æã®ãã©ã«ãã«ç°å¢ãæ§ç¯ãã¾ãã
ããã§ã¯windows, c:\testã«ä»åã®ç°å¢ãä½ãã¨ãã¾ãã
cd c:\
lein new test
ã¨ããã¨c:\以ä¸ã«testãã©ã«ããçæããã¦ããã¯ãã§ãã
ãã®ãã©ã«ãã«ããproject.cljãã¨ãã£ã¿ã§éãã
ä¸è¨ãã³ãããã¦ä¸ããã
(defproject test "0.1.0-SNAPSHOT" :dependencies [[org.clojure/clojure "1.6.0"] [incanter "1.5.5"] [cc.artifice/clj-ml "0.6.0-SNAPSHOT"]])
ããã¯2014/09/02ç¾å¨ææ°ãã¼ã¸ã§ã³ã®Clojureã¨Incanterã¨clj-mlãå©ç¨ããè¨å®ã§ãã
ãã®è¨å®ãçµãã£ãå¾ã
lein deps
ã¨ããã¨è¨å®ããã©ã¤ãã©ãªãleiningenãèªåã§åå¾ãã¦ããã¾ãã
incanterã使ã£ã¦ã¿ã
Incanterãå©ç¨ããã¨æ¬¡ã®ãããªãã¨ãå¯è½ã¨ãªãã¾ãã
- ãã¼ã¿æ½åºãå種éè¨
- å¯è¦å
- å種統è¨åæ(å帰åæã主æååæãªã©)
ã¾ãã¯ä¾¿å©ãªãã¼ã¿æä½ãå種éè¨æ¹æ³ãå¦ã³ã¾ãããã
; â Incanteråä½ç¢ºèª ; ã¾ãuseé¢æ°ã§å種æ©è½ãèªã¿è¾¼ã¿ã¾ãã ; charts: æç» ; stats: çµ±è¨åæ ; datasets: ãµã³ãã«ãã¼ã¿ã»ãã (use '(incanter core charts stats datasets)) ; ãµã³ãã«ãã¼ã¿ã»ããããirisãèªã¿è¾¼ã¿ã ; irisã¨ããååã«æç¸ãã¾ã (def iris (get-dataset :iris)) ; irisã®ä¸èº«ãè¦ã¦ã¿ã¾ãã ; Excelã©ã¤ã¯ãªç»é¢ãç«ã¡ä¸ããã¾ã ; ããã§ä¸åº¦irisãã©ã®ãããªãã¼ã¿ã確èªãã¦ãã ãã (view iris) ; â å種ãã¼ã¿æ½åº ; ãã¼ã¿ãã¤ãã³ã°ãããå ´åã¯ãã¼ã¿ãæ§ã ãªè§åº¦ããçºããã®ãèè¦ã§ãã ; åããã¼ã¿ã§ãåãå£ãå¤ããã¨è¦ãæ¹ãå¤ãã£ã¦ãã¦ã ; ä»ã¾ã§æ°ã¥ããªãã£ãç¹å¾´ãè¦åºããã¨ãããã¾ãã ; ããæ¡ä»¶ãæºããããã¼ã¿ã ããæ½åºããæ¹æ³ã説æãã¾ãã ; irisãã¼ã¿ããSpeciesåãsetosaã®è¡ã ãåå¾ ($where {:Species "setosa"} iris) ; :Petal.Widthåã>2ã®è¡ã ãåå¾ ($where {:Petal.Width {:$gt 2}} iris); ; æå®åã§æé (asc)/éé (desc)ãasc, descçç¥ä¸å¯ ($order :Petal.Width :asc iris) ; ä¸è¨çµåãã ($order :Petal.Width :desc ($where {:Petal.Width {:$gt 2.2}} iris)) ; æå®åãåå¾ ($ :Sepal.Length iris) ; â ãã¼ã¿ããè¦ç´çµ±è¨éãå¾ã ; Incanterã«ã¯å種è¦ç´çµ±è¨éãç®åºããé¢æ°ãæã£ã¦ãã¾ãã ; æå®åã®ç®è¡å¹³å (mean ($ :Sepal.Length iris)) ; 5.843333333333335 (mean ($ :Petal.Length iris)) ; 3.7580000000000027 ; æå®åã®æ¨æºåå·® (sd ($ :Sepal.Length iris)) ; 0.8280661279778632 (sd ($ :Petal.Length iris)) ; 1.7652982332594656 ; ãã¼ã¿å ¨ä½ããã§ã¯ãªãã°ã«ã¼ãã³ã°ãã¦è¦ç´çµ±è¨éãå¾ã ; ããã§ã¯irisããSpeciesãã¨ã«å種è¦ç´çµ±è¨éãå¾ã ; $rollupé¢æ°ã¯ç¬¬ä¸å¼æ°ã«ã©ã®ãããªå¦çããããªãã·ã§ã³ãé¢æ°ãæå® ; 第äºå¼æ°ã§åæå® ; 第ä¸å¼æ°ã§ã°ã«ã¼ãã³ã°ããåãæå® ; 第åå¼æ°ã§ãã¼ã¿ãæå® ($rollup :mean :Sepal.Length :Species iris) ;| :Species | :Sepal.Length | ;|------------+-------------------| ;| virginica | 6.587999999999998 | ;| versicolor | 5.936 | ;| setosa | 5.005999999999999 | ($rollup :count :Sepal.Length :Species iris) ($rollup :max :Sepal.Length :Species iris) ($rollup :min :Sepal.Length :Species iris) ; $rollupã®ãªãã·ã§ã³ã«ç¡ãéè¨ããããå ´å㯠; 第ä¸å¼æ°ã«ä»»æã®é¢æ°ãæ¾ãè¾¼ãã°ãã ($rollup median :Sepal.Length :Species iris) ($rollup variance :Sepal.Length :Species iris) ; ããç¨åº¦éè¨æ¸ã¿ã®ãã¼ã¿ã«å¯¾ãã ; æ´ã«æ¡ä»¶å ãã¦éè¨ããã«ã¯group-byãä¾¿å© (with-data (get-dataset :hair-eye-color) ($group-by [:hair :eye])) ;{{:eye "green", :hair "black"} ;| :hair | :eye | :gender | :count | ;|-------+-------+---------+--------| ;| black | green | male | 3 | ;| black | green | female | 2 | ;, {:eye "hazel", :hair "red"} ;| :hair | :eye | :gender | :count | ;|-------+-------+---------+--------| ;| red | hazel | male | 7 | ;| red | hazel | female | 7 | ;...
次ã«ãã¼ã¿ãå¯è¦åãã¦ã¿ã¾ãã
詳細ã«ã¤ãã¦ã¯ä¸è¨ãåç
§ãã¦ãã ããã
http://liebke.github.io/incanter/charts-api.html
(def iris (get-dataset :iris)) ; æ£ã°ã©ã bar-chart (view (bar-chart :Species :Sepal.Length :data iris))
; æãç·ã°ã©ã line-chart (def airline (get-dataset :airline-passengers)) (def years (sel airline :cols 0)) (def months (sel airline :cols 2)) (def passengers (sel airline :cols 1)) (view (line-chart months passengers :group-by years :legend true))
; ãã¹ãã°ã©ã (view (histogram :Sepal.Width :data iris))
; ç®±ã²ãå³ (view (box-plot :Sepal.Width :group-by :Species :legend true :data iris))
; æ£å¸å³ scatter-plot (view (scatter-plot :Sepal.Length :Sepal.Width :data iris)) ; æ£å¸å³ã«group-byãªãã·ã§ã³ãç¨ãã¦Speciesæ¯ã«è²åã表示 (view (scatter-plot :Sepal.Length :Sepal.Width :data iris :group-by :Species)) ; æ£å¸å³è¡å scatter-plot-matrix (view (scatter-plot-matrix iris :nbins 20 :group-by :Species))
; QQãããã (view (qq-plot :Sepal.Length :data iris))
clj-mlã§æ©æ¢°å¦ç¿
clj-mlã¯wekaãClojureããç°¡åã«ä½¿ããããã«ãããã®ã§ãã
詳細ã¯ä¸è¨ãåç
§ãã¦ãã ããã
https://github.com/joshuaeckroth/clj-ml
wekaã¯ãã¼ã¿ãã¤ãã³ã°æ¥çã§çµæ§å¤ããã使ããã¦ãããã¼ã«ã§ã
å種æ©æ¢°å¦ç¿ã®ææ³ãå©ç¨ã§ãã¾ãã
ä»åã¯ãã使ãããåé¡å¨ã§ãã決å®æ¨ã¨ã©ã³ãã ãã©ã¬ã¹ãã
éé層åã¯ã©ã¹ã¿ãªã³ã°ææ³ã§ããk-meansãç´¹ä»ãã¾ãã
(use 'clj-ml.classifiers 'clj-ml.utils 'clj-ml.io 'clj-ml.data) ; wekaç¹æã®ãã¼ã¿å½¢å¼arffãå©ç¨ãã¾ãã (def iris (-> (load-instances :arff "http://repository.seasr.org/Datasets/UCI/arff/iris.arff") (dataset-set-class :class))) ; CSVå½¢å¼ãèªã¿è¾¼ãå ´åã¯æ¬¡ã®ããã«ãã¾ãã ;(def iris (-> (load-instances :csv "iris.csv") (dataset-set-class :class))) ; â 決å®æ¨ (def decision-tree (-> (make-classifier :decision-tree :c45) (classifier-train iris))) ; ä½æãã決å®æ¨ã®ä¸èº«ãè¦ã¦ã¿ã¾ã decision-tree ;petalwidth <= 0.6: Iris-setosa (50.0) ;petalwidth > 0.6 ;| petalwidth <= 1.7 ;| | petallength <= 4.9: Iris-versicolor (48.0/1.0) ;| | petallength > 4.9 ;| | | petalwidth <= 1.5: Iris-virginica (3.0) ;| | | petalwidth > 1.5: Iris-versicolor (3.0/1.0) ;| petalwidth > 1.7: Iris-virginica (46.0/1.0) ;Number of Leaves : 5 ;Size of the tree : 9 ;ã¯ãã¹ããªãã¼ã·ã§ã³ã«æãã¦ã¿ã¾ã (def decision-tree-evaluation (classifier-evaluate decision-tree :cross-validation iris 10)) ;ã¯ãã¹ããªãã¼ã·ã§ã³ã®çµæã確èªãã¾ã (println (:summary decision-tree-evaluation)) ;Correctly Classified Instances 143 95.3333 % ;Incorrectly Classified Instances 7 4.6667 % ;Kappa statistic 0.93 ;Mean absolute error 0.0393 ;Root mean squared error 0.1755 ;Relative absolute error 8.8447 % ;Root relative squared error 37.2333 % ;Total Number of Instances 150 (println (:confusion-matrix decision-tree-evaluation)) ;=== Confusion Matrix === ; a b c <-- classified as ; 49 1 0 | a = Iris-setosa ; 0 47 3 | b = Iris-versicolor ; 0 3 47 | c = Iris-virginica
; â ã©ã³ãã ãã©ã¬ã¹ã ; è¦ãã°ãããã¾ãããmake-classifierã®ãªãã·ã§ã³ã ; :c45 -> :random-forestã«å¤æ´ããã ãã§ã (def random-forest (-> (make-classifier :decision-tree :random-forest) (classifier-train iris))) random-forest (def random-forest-evaluation (classifier-evaluate random-forest :cross-validation iris 10)) (println (:summary random-forest-evaluation)) (println (:confusion-matrix random-forest-evaluation))
; â k-means (use 'clj-ml.clusterers) ; ããããspeaciesãããã°ã¯ã©ã¹ã¿ãªã³ã°ããå¿ è¦ç¡ãã®ã§ä¸æ¦å¤ãã¾ã (def iris (-> (load-instances :arff "http://repository.seasr.org/Datasets/UCI/arff/iris.arff") (dataset-remove-attribute-at 4))) (def k-means (make-clusterer :k-means {:number-clusters 3})) (clusterer-build k-means iris) k-means ;Number of iterations: 6 ;Within cluster sum of squared errors: 6.998114004826762 ;Missing values globally replaced with mean/mode ;Cluster centroids: ; Cluster# ;Attribute Full Data 0 1 2 ; (150) (61) (50) (39) ;========================================================= ;sepallength 5.8433 5.8885 5.006 6.8462 ;sepalwidth 3.054 2.7377 3.418 3.0821 ;petallength 3.7587 4.3967 1.464 5.7026 ;petalwidth 1.1987 1.418 0.244 2.0795
çµããã«
éåé§ã足ã§ã¯ããã¾ããããClojureãç¨ãããã¼ã¿ãã¤ãã³ã°ã俯ç°ãã¾ããã
ç°¡åã«ãã¼ã¿ãã¤ãã³ã°åºæ¥ããããã£ã¦ã¿ããï¼ã¨æãåã£ã¦é ããã°å¹¸ãã§ãã
ä»åã¯ãã£ã¨ããç´¹ä»ãè¡ãã ãã§
åææ³ã®è©³ç´°ãã³ã¼ãã®èª¬æãè¡ãã¾ããã§ãããã
ä»å¾è©³ç´°ã«ã¤ãã¦ã解説ãã¦ããããã¨æãã¾ãã
çããããã²Clojureã§ãã¼ã¿ãã¤ãã³ã°ãã¾ãããï¼
Clojureãã¤ãã³ã°é¢é£è³æ
Incanterãä»ããã¯ããããªã
http://marui.hatenablog.com/entry/2014/02/15/113838
çµ±è¨è§£æã¢ããªãIncanterãå
¥é
http://antibayesian.hateblo.jp/entry/20111128/1322494688
Clojure/lucene-kuromojiã§ããã¹ããã¤ãã³ã°å
¥éãï½å½¢æ
ç´ è§£æããã¯ã¼ãã«ã¦ã³ãã¾ã§ï½Add Star
http://d.hatena.ne.jp/Kazuhira/20130911/1378914422
Clojure/kuromojiã§ããã¹ããã¤ãã³ã°å
¥éãï½å½¢æ
ç´ è§£æããã¯ã¼ãã«ã¦ã³ãã¾ã§ï½
http://antibayesian.hateblo.jp/entry/2013/09/10/231334
Web系女åãLispã¨åºä¼ã£ã¦çµ±è¨å¦ã«ç®è¦ããã¾ã§ã®ã話
http://antibayesian.hateblo.jp/entry/20121210/1355152537
Incanter API
http://liebke.github.io/incanter/index.html
â 追è¨
clojure, Incanter, clj-mlã®ãã¼ã¸ã§ã³ã¢ããã«ä¼´ãã
project.cljã®å
容ã2014/09/02ææ°çã«å¤æ´ãã¾ããã