{"id":1867,"date":"2016-08-23T22:45:36","date_gmt":"2016-08-23T14:45:36","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1867"},"modified":"2016-08-23T22:48:26","modified_gmt":"2016-08-23T14:48:26","slug":"first-trial-of-weka-in-processing","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2016\/08\/23\/first-trial-of-weka-in-processing\/","title":{"rendered":"First trial of Weka in Processing"},"content":{"rendered":"<p>Instead of using the machine learning module (ML) of OpenCV, I also investigated another popular machine learning library for Java, <a href=\"http:\/\/www.cs.waikato.ac.nz\/ml\/weka\/index.html\">Weka<\/a>, from the University of Waikato. The first trial was to load an external CSV file into the proper data structure of the Weka library. The content of the CSV file is as follows. The first column will be the index of the records.<\/p>\n<pre>\r\nA,1,2,3,4\r\nB,2,3,4,1\r\nC,3,4,1,2\r\nD,4,1,2,3\r\nE,4,3,2,1\r\n<\/pre>\n<p>The first thing to do was to <a href=\"http:\/\/www.cs.waikato.ac.nz\/ml\/weka\/downloading.html\">download<\/a> the latest Weka distribution, currently 3.8 and placed the weka.jar file into the <strong>code<\/strong> folder of the Processing sketch. <\/p>\n<p><em>The complete codes<\/em><\/p>\n<pre lang=\"java\">\r\nimport weka.core.converters.CSVLoader;\r\nimport weka.core.Instances;\r\nimport weka.core.Instance;\r\nimport java.util.Enumeration;\r\nimport java.io.File;\r\n\r\nInstances data;\r\n\/\/ Name of the CSV data file\r\nString csv;\r\n\r\nvoid setup() {\r\n  size(600, 600);\r\n  csv = \"Testing.csv\";\r\n  try {\r\n    loadData();\r\n  } \r\n  catch (Exception e) {\r\n    e.printStackTrace();\r\n  }\r\n  noLoop();\r\n}\r\n\r\nvoid draw() {\r\n  background(0);\r\n}\r\n\r\nvoid loadData() throws Exception {\r\n  CSVLoader loader = new CSVLoader();\r\n  loader.setNoHeaderRowPresent(true);\r\n  loader.setSource(new File(dataPath(csv)));\r\n  data = loader.getDataSet();\r\n  data.setClassIndex(0);\r\n\r\n  println(\"Attributes : \" + data.numAttributes());\r\n  println(\"Instances : \" + data.numInstances());\r\n  println(\"Name : \" + data.classAttribute().toString());\r\n  \/\/ To scan through all the records of the CSV file\r\n  Enumeration all = data.enumerateInstances();\r\n  while (all.hasMoreElements()) {\r\n    Instance rec = (Instance) all.nextElement();\r\n    println(\"Instance : \" + rec.classValue() + \": \" + rec.toString());\r\n  }\r\n}\r\n<\/pre>\n<p><em>The console output<\/em><\/p>\n<pre>\r\nAttributes : 5\r\nInstances : 5\r\nName : @attribute att1 {A,B,C,D,E}\r\nInstance : 0.0: A,1,2,3,4\r\nInstance : 1.0: B,2,3,4,1\r\nInstance : 2.0: C,3,4,1,2\r\nInstance : 3.0: D,4,1,2,3\r\nInstance : 4.0: E,4,3,2,1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Instead of using the machine learning module (ML) of OpenCV, I also investigated another popular machine learning library for Java, Weka, from the University of Waikato. The first trial was to load an external CSV file into the proper data structure of the Weka library. The content of the CSV file is as follows. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,79],"tags":[147,62,148],"class_list":["post-1867","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-machine-learning","tag-processing-org","tag-weka"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1867"}],"version-history":[{"count":3,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions"}],"predecessor-version":[{"id":1870,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions\/1870"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1867"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}