çµ±è¨è§£æã¢ããªãIncanterãå ¥é
ãClojure製統è¨è§£æã¢ããªãIncanterãã¯ç¡æãã¤jarãã¡ã¤ã«ä¸ã¤ã§ãæ軽ã«å®è¡ã§ããããã®ä¸Javaã®è±å¯ãªã©ã¤ãã©ãªãå©ç¨ããããæç»ã«ç¹åããProcessingã¨ããå¦çç³»ã§ãªãããªã¢ãã¡ã¼ã·ã§ã³ãã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ã§ããã¨ããé¢ç½ãã¢ããªã±ã¼ã·ã§ã³ã§ããã¾ããJVMä¸ã§åããããHadoopãLuceneãªã©ã«ã·ã¼ã ã¬ã¹ã§é©ç¨ã§ãã¾ãããGoSenãªã©å½¢æ ç´ è§£æã¢ããªãç°¡åã«å¼ã³åºãããããèªç¶è¨èªå¦çãããã¹ããã¤ãã³ã°ã«ãæ´»ç¨ã§ãã¾ããä¸çªéè¦ãªãã¨ã¯ãClojureã§ããã¨ãããã¨ãã¤ã¾ãçããã®æããLispã§ã³ã¼ãã£ã³ã°åºæ¥ãã¨ãããã¨ã§ããããä¸åº¦è¨ãã¾ãããLispã§çµ±è¨è§£æãåºæ¥ãã¨ããåï½ï¼ç¥ï¼ã
ãç¡æã®çµ±è¨è§£æã¢ããªã¨ããã¨Rï¼ãã¨ã¢ããªã§ã¯ããã¾ããããPython-Scipy/Numpyãªã©ï¼ãæããããã¨æãã¾ããæ£ç´ãªè©±ãIncanterã¯Rã«æ¯ã¹ã¦ï¼ãããã¾ãæ¯ã¹ç©ã«ãªããªãã®ã§ã¯â¦ï¼ãã¦ã¼ã¶ã¼æ°ãå©ç¨å®ç¸¾ã§ã¯å®å ¨ã«å£ãã¾ããRã§ã¯ææ°ã®è§£æææ³ãå®è£ ããã¦ããããã¾ãããIncanterã¯å²ã¨åºæ¬çãªææ³ããããã¾ããããããã±ã¼ã¸ã®æ°ãå ¨ãè¦å£ããã¾ããç 究ãå®åã§ä½¿ãå ´åã¯Rãç¨ããã»ããè¯ãã§ããããã§ã¯Incanterã使ãå©ç¹ã¯ã©ãã«ããã®ã§ããããï¼Incanterã¯ã³ã³ãã¤ã«ãã¦Jarãã¡ã¤ã«ã«ã¾ã¨ãã¦é å¸ãããã¨ãã§ããã®ã§ãã解æçµæã渡ãã ãã§ã¯ãªããã¨ã³ãã¦ã¼ã¶ã¼ã使ããããç°¡åãªçµ±è¨ã¢ããªãä½ãããããããè²ã ã¤ã³ã¹ãã¼ã«ãã¦é ããã¨ç¡ãæ軽ã«ï¼ãã¨ããç¨éã«åãã¦ãã¾ããã¨ã³ãã¦ã¼ã¶ã¼ã¨ãã¦ããåãªãå ±åæ¸æ¸¡ãããã ããããããã©ã¡ã¿ãè²ã å¤æ´ã§ããã»ãã楽ããã§ããããã¾ããJavaã¨é£æºã§ããã¨ããå¼·ã¿ãããã®ã§ãRã§ã¯åºæ¥ãªã大è¦æ¨¡è§£æãå¯è½ï¼ããããã¾ããããã£ããã¨ãªãã®ã§â¦ã§ã夢ãåºããã¾ãããï¼ã§ãããï¼ï¼ãã¾ããJavaã®Swingãªã©ã使ãããããRããGUIã¢ããªãã¨ã¦ãä½ãæããªãã¾ãããã¨ä¸çªéè¦ãªãã¨ã¯Lispã£ã¦ãã¨ã§ãããRãScipyã¯Lispã§ã¯ãªãã®ã§é§ç®ã§ããããã¯ãIncanter使ãããæãã¾ãããIncanterç¥ããã«ä»ã¾ã§çãã¦ãããã¨ãæ·±ãæ¥ãã¦ãã ããã
ããã¦ãåç½®ãã¯ãããããã«ãã¦ãå®éIncanterã使ã£ã¦ã¿ã¾ããããIncanter本家ããIncanter.jarãè½ã¨ãã¦ãã¦*1ãIncanterãè¨ç½®ãããã£ã¬ã¯ããªã§æ¬¡ã®ã³ãã³ããå®è¡ãã¾ãã
java -jar incanter.jar incanter.main;âWindowsç°å¢ã®å ´å
java -cp incanter.jar incanter.main;âLinuxç°å¢ã®å ´å
ããã§Clojureã®REPLã¨ããå¦çç³»ãèµ·åãã¾ããã§ã¯çãã大好ãHello Worldï¼ããå§ãã¾ãããã
(str "Hello" " World!")
ã§"Hello World!"ã¨è¡¨ç¤ºããã¾ãããããã®æç¹ã§Incanterã¨ãããLispã®ç´ æ´ãããã®çé±ãè¦ãã¦ãã¾ãã"str"ã¯å¼æ°ãæ¨æºåºåããé¢æ°ã§ããã¾ãããã¯åé¡ãªãã§ããããããããä¸ã®ã³ã¼ããè¦ã¦ã¿ãã¨ãå¼æ°ãè¤æ°æå®ããã¦ãã¾ãã試ãã«å¼æ°ããã£ã¨å¢ããã¦ã¿ã¾ãããã
(str "hello" " world, " "ããã¡ã¹ï¼")
>"hello world, ããã¡ã¹ï¼"
å¼æ°ãå¢ããã¦ãå ¨ãåé¡ãªãåä½ããâ¦ç´ æ´ãããï¼ãã®ã¾ã¾é»åã®ããã«å©ç¨ãã¦ã¿ã¾ãããã足ãç®ããã¾ãã
(+ 1 2 3 4 5)
>15
1ã5ã¾ã§ã®æ´æ°ã足ãããããçµæ15ã«ãªãã¾ãããå ç¨ã®strã¨åãããã«ãè¤æ°ã«å¼æ°ãæå®ãã¦ãæ³å®éãåãã¾ããã
ãLispç³»ã®åºç¤ã¯ãã¾ãé¢æ°å(strã+ãªã©ã+ãLispã§ã¯é¢æ°ã§ã)ãä¸çªå·¦ã«ããããã®é¢æ°ã«æ¾ãè¾¼ãè¦ç´ ãé¢æ°åã®å³ã«æå®ããããã¦ãã®é¢æ°ãåãç¯å²ï¼ã¹ã³ã¼ãï¼ãæ¬å¼§ã§è¡¨ç¾ããã¨ãããã®ã§ããé¢æ°ã¨è¦ç´ ã¯ããã¨ãã¦ãã¹ã³ã¼ãã¨ã¯ä½ãã足ãç®ã¨æãç®ãçµã¿åããããã®ã§èª¬æãã¾ãããã
(+ 1 (* 2 3))
>7
â¦ããåããã¥ããï¼ï¼ã¨æãããããããã¾ããããè½ã¡çãã¦èãã¦ã¿ã¾ããããã¾ããä¸çªå·¦ã®é¢æ°ã¯ï¼ã§ããå¼æ°ã¯1ã¨(* 2 3)ã§ãããªã®ã§ãï¼ã¨(* 2 3)ã¨è¶³ãããããã¢ããä¸çªå¤ã®æ¬å¼§ã®å¦ççµæã«ãªãã¾ãããããã¦å å´ã®æ¬å¼§ã¯(* 2 3) = 2 * 3 = 6ãªã®ã§ãæçµçã«1 + 6ã«ãªããçãã¯ï¼ã¨ãªãã¾ããç¾ããâ¦ææ§æ§ãä¸æ¬ çããªãâ¦ãããã1~5ã¾ã§å ç®ããã¨ãããã¡ãã¡ä»ã®è¨èªã®ããã«1 + 2 + 3 + 4 + 5ã¿ãããªç¡é§ã«ï¼ãæ¸ãå¿ è¦ã¯ããã¾ããããªãã¦ã·ã³ãã«ããããLispã¨è¨ãã°ç®ãç©ãæ¬å¼§ã®æ°ãæ¶æããã¾ãããæ¼ç®ãstrã®ä¾ããå¥æ®µä»ã®è¨èªã¨æ¯ã¹ã¦æ¬å¼§ãå¤ãããã§ã¯ããã¾ãããCç³»ã®è¨èªã¨æ¯è¼ãã¦ã¿ãã¨print ("hello world!");ã¨(str "hello world!")ã§ãå¦çã«å¯¾ãã¦å ¨ãä½è¨ãª;ãªã©ãããã¾ãããã¨ããã;ã¨ã¯ä¸ä½ä½ãªã®ãï¼ãªããããªã¢ããå¿ è¦ãªã®ãï¼
ãããããã¾ã§èªãã§ãããããªãï¼ãã®ç²¾ç¥çèä¹ åã«åã¯æåããããããã§ã¨ããããããã¯ãããªçæ°ãªè©±ã¯åºã¦ããªãããå®å¿ãã¦æ¬²ãã:Dãã®ã£ã°ã®ããã ãã©ãä¸ã®è©±ã¯ãä»å¾ã©ããã¦ãå¿ è¦ã«ãªã前置記法ã«æ £ãã¦ãããããã«ã¯ä»æ¹ãªãã£ããã ãå®ã¯ãã£ã¨ãã£ã¨èª¬æããªãã¨ãããªããã¨ããããã ãã©ãLispã®èª¬æããã ãããé·ããªããããã§ã¯åã«Lispã¸ã®ç çãªææå¿ãåãé¤ãã¦ãããããããããªãã¨ããã¯ããããªããã¨ãããããµã³ãã«ã³ã¼ãã®å¤ãå¤ããã¨ããããåãã¦éã¼ããã¨æã£ã¦ããã ããã°å¹¸ãã ãããããIncanterãå©ç¨ãã¦ãããã¨ã«ãããããã£ã¨ãä¸ã¤ã ãäºåã«docã¨ããé¢æ°ã«ã¤ãã¦è¦ãã¦ããããããªãä½ãããããªãé¢æ°ãããã°(doc é¢æ°å)ã¨ããã¨ããã®é¢æ°ã®èª¬æãè¦ããã¨ãåºæ¥ããdocã¯åã®å¼·åãªå³æ¹ãããããªããã¨ãããã°ã¨ã«ããdocã試ãã¦ã¿ããï¼
ã¾ããIncanterã®æ§ã ãªçµ±è¨ã©ã¤ãã©ãªãå©ç¨ããããã«ãããã®ã©ã¤ãã©ãªã使ç¨ããããã¨ããã®ãå¦çç³»ã«æå®ãã¦æãã¦ãããªãã¨ããã¾ããã次ã®ã³ãã³ããREPLã«æã¡è¾¼ãã§ä¸ããã
(use '(incanter core stats charts io datasets))
useã§å¦çç³»ã«å¼æ°ã¨ãã¦æå®ããã©ã¤ãã©ãªãå©ç¨ãããã¨ãæãã¦ããããcoreã¯Incanterã®ã¾ãã«ã³ã¢é¨åãä½ãããã«ãããããªãã¨å§ã¾ããªããstatsã¯çµ±è¨å¦çãchartsã¯ã°ã©ãæç»ãioã¯ãã¡ã¤ã«å ¥åºåã®ã©ã¤ãã©ãªãdatasetsã¯ãµã³ãã«ãã¼ã¿ã»ããã§ããã¨ãããããããããç¨æãã¦ããã°ååã§ãããã次ã®ããã«ãã¼ã¿ã»ãããç¨æãã¾ãã
(dataset ["x1" "x2" "x3"] [ [1 2 3][4 5 6] ])
ããã§ï¼è¡ï¼åã®ãã¼ã¿ãåºæ¥ã¾ããExcelã§è¡¨ç¤ºããã¨
x1 | x2 | x3 |
---|---|---|
1 | 2 | 3 |
4 | 5 | 6 |
ã¨ããæãã«ãªãã¾ãã
ãããdataã¨ããå¤æ°ã«æç¸ãã¾ãã
(def data (dataset ["x1" "x2" "x3"] [ [1 2 3][4 5 6] ]))
ãã®ãã¼ã¿ãExcelã®ãããªã¤ã³ã¿ã¼ãã§ã¤ã¹ã§è¡¨ç¤ºããã¾ãããã
ç°¡åã§ããããã®ããã«ãviewé¢æ°ã使ãã¨ãã¼ã¿ãGUIã§è¡¨ç¤ºã§ãã¾ããå¤æ°dataã«å ç¨ã®è¡¨ãã¼ã¿ãæç¸ããã®ã§ãããããã¡ã¤ã«å ¥åºåãã¦ã¿ã¾ãããã
(save data "test.csv")
(read-dataset "test.csv" :header true)
ãªãã¨ããã ãã§CSVå½¢å¼ã§ã®ãã¡ã¤ã«ã®å ¥åºåãå¯è½ã§ããé常ã«ç°¡åã§ããã
ã次ã«ãå¹³åã¨æ¨æºåå·®ãæ±ãã¦ã¿ã¾ããããIncanterã«ã¯datasetsã¨ããã©ã¤ãã©ãªã«ãµã³ãã«ãã¼ã¿ãç¨æãã¦ãã¾ããdatasetsã¯æ¢ã«useãã¦ããã¾ãããããµã³ãã«ãã¼ã¿ã¯get-dataseté¢æ°ã§åå¾ãã¾ããä»åã¯carsã¨ããè»ã®é度ã¨èµ°è¡è·é¢ã®ãã¼ã¿ãå©ç¨ãã¾ãããµã³ãã«ãã¼ã¿ã®ä¸ããcarsã®ãã¼ã¿ãåå¾ããã«ã¯
(get-dataset :cars)
ã¨ãã¾ããããã¨speed, distã®ï¼åï¼ï¼è¡ã®ãã¼ã¿ãåå¾åºæ¥ã¾ãããã®ãã¼ã¿ã®ãã¡speedåã®ãã¼ã¿ã ãåå¾ãããå ´åã¯$ãå©ç¨ãã¾ãã
($ :speed (get-dataset :cars))
ãããã¾ãããdataã«æç¸ããå¹³åã¨æ¨æºåå·®ãå¾ãã«ã¯ãåã mean, sdé¢æ°ãç¨ãã¾ãã
(def data($ :speed (get-dataset :cars)))
(str "mean = "(mean data) " :: SD = " (sd data))
>"mean = 15.4 :: SD = 5.2876444352347844"
ç°¡åã§ãããä»ç´°ã ã¨ãããã¼ã¿æä½ã«ã¤ãã¦æ¸ãã¦ããã¾ãã
($where {:speed {> 10}} data);carsãã¼ã¿ããspeedåã10ããä¸ã®ãã¼ã¿ã ãåå¾
(def data (get-dataset :iris));irisãã¼ã¿ãdataã«æç¸
($where {:Species "setosa"} data);irisãã¼ã¿ããSpeciesåãsetosaã®è¡ã ãåå¾(def data (get-dataset :hair-eye-color));髪ã¨ç®ã®è²ã®ãã¼ã¿ãdataã«æç¸
($order :count :desc data);countåãéé ã§ã½ã¼ã
ãã¼ã¿ã®æç»ãç°¡åã§ããæ£å¸å³ãæ¸ãã¦ã¿ã¾ããããscatter-plotã¨ããé¢æ°ã§æ£å¸å³ãæãã¾ãã
(view (scatter-plot :Sepal.Length :Sepal.Width :data (get-dataset :iris)))
ç°¡åã§ããããã£ã¨ãã£ãããå³ãæ¸ãããã§ãããdocé¢æ°ãå©ç¨ãã¦ããªãã·ã§ã³ã調ã¹ã¦è¿½å ãã¦ã¿ã¾ãããã
(view (scatter-plot :Sepal.Length :Sepal.Width :data (get-dataset :iris)
:group-by
:Species
:title "Fisher Iris Data"
:x-label "Sepal Length (cm)"
:y-label "Sepal Width (cm)"))
æ£ã°ã©ããæãã¾ãããã
(view (bar-chart :Species :Sepal.Length :data (get-dataset :iris)))
(view (bar-chart :hair :count :group-by :eye :legend true :data (get-dataset :hair-eye-color)))
é¢æ°ã®ã°ã©ããç°¡åã«æãã¾ãã
綺éºãªã°ã©ããç°¡åã«ããã¾ãããããããç»åã¨ãã¦ä¿åããå ´åãsaveé¢æ°ãç¨ãã¾ããsaveé¢æ°ãç¨ããã¨ãã¯ã次ã®ããã«ä¿åæã®ååãæå®ãã¾ãï¼ã¡ãªã¿ã«saveé¢æ°ã§ç»åãä¿åããå ´åã¯pngããåºæ¥ãªãããã§ããå¤åï¼ã
(save (function-plot sin -10 10) "func.png")
ã¡ãã£ã¨çºå±çãªè©±é¡ã¨ãã¦ãIncanterããMongoDBãå©ç¨ããã®ã«ãã£ã¬ã³ã¸ãã¦ã¿ã¾ãããã
(use 'incanter.mongodb)
(use 'somnium.congomongo)
(mongo! :db "mydb")
(view (fetch-dataset :cars))
ããç²ãæ§ã§ããï¼Lispç³»åä½é¨ã®æ¹ãå¦ä½ã§ããï¼å··ã§è¨ãããç¨é£ããã¯ãªãã£ããªã¼ç°¡åã«åºæ¥ããã ãªã¼ã¨ããæããæã£ã¦ããã ããã°å¹¸ãã§ããLispããã«ã¼ã®çãããå¦ä½ã§ããï¼Incanterã§çµ±è¨è§£æãããæ°ãæ¼²ã£ã¦ãã¾ãããï¼ç°¡åãªè¨ç®ãããã¡ã¤ã«å
¥åºåãæç»ãDBæä½ã¨ä¸éãããããæä½ããã£ã¦ãã¾ãããçãããIncanterãå©ç¨ããç´ æµãªçµ±è¨è§£æã¢ããªãä½ã£ã¦ä¸ããã社ä¼ï¼ã¨éç¹çã«åï¼ããã£ã¨ãããã¼ã«ãã¦ããããã¨ãé¡ã£ã¦ã¾ããä¸æç¹ãããã°ã質åä¸ãããã§ã¯ã楽ããLispã«ããçµ±è¨è§£æã©ã¤ããï¼
*1:exeçãç§ã試ããã¨ããããªããã®ã¼ãªæããªã®ã§ãjarå©ç¨ããè¦ããã¾ã