Using+Weka+from+Groovy

toc Groovy...
 * is an agile and dynamic language for the Java Virtual Machine
 * builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
 * makes modern programming features available to Java developers with almost-zero learning curve
 * supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
 * makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
 * increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
 * simplifies testing by supporting unit testing and mocking out-of-the-box
 * seamlessly integrates with all existing Java objects and libraries
 * compiles straight to Java bytecode so you can use it anywhere you can use Java

//-- taken from the [|Groovy homepage]//

This article explains how to use Weka classes within Groovy.

=Groovy CLASSPATH= Additional jars can be added to Groovy in various ways: > Any jar that is placed in this directory will be available in Groovy > This file lists jar files and directories from which to include jar files. The syntax is fairly easy: >> > For example, loading the that is located in $HOME/myjars: >> > Or loading all jars in a directory, e.g., $HOME/myjars: >> > Groovy automatically imports the Java CLASSPATH, i.e., everything that is listed in the CLASSPATH environment variable. > When running a Groovy script, you can explicitly tell Groovy, what CLASSPATH to use: >>
 * **Java **

=The Grape: Groovy dependency manager= Grape is a JAR dependency manager embedded into Groovy. Grape lets you quickly add maven repository dependencies to your classpath, making scripting even easier. Weka can be added as a dependency in your Groovy script @Grab(group='nz.ac.waikato.cms.weka', module='weka-stable', version='3.6.12') You can also search for dependencies and versions numbers on [|mvnrepository.com]  and it will provide you the @Grab  annotation form of the pom.xml  entry. =Accessing Weka classes from Groovy=

Requirements

 * Groovy 1.5.7 or later
 * Weka 3.5.4 or later

Implementation
If your Groovy CLASSPATH has been setup correctly, you can use all those classes in Groovy straight away. E.g., after including the, I can run the following little script to train J48 on a supplied dataset and output its model: code format="groovy" import weka.classifiers.trees.J48 import weka.core.converters.ConverterUtils.DataSource import weka.core.Instances

if (args.size == 0) { println "Usage: UsingJ48.groovy " System.exit(0) }

// load data and set class index data = DataSource.read(args[0]) data.setClassIndex(data.numAttributes - 1)

// create the model j48 = new J48 j48.buildClassifier(data)

// print out the built model println j48 code A slightly more elaborate example can be found in, which uses more methods of the class.


 * NB:** The example needs Weka 3.6.x (or a snapshot of the developer version) to run, due to some changes in the API.

=Implementing a Groovy classifier=

Requirements

 * Groovy 1.5.7 or later
 * developer version of Weka later than 3.5.8, snapshots later than 02/02/2009

Implementation
Implementing a Groovy classifier is pretty straight-forward, since the syntax is almost the same and Java classes are imported/used in Groovy just like in Java. The file re-implements the  classifier as Groovy script.

The class declaration for looks like this, for instance: code format="groovy" class GeroR extends Classifier implements WeightedInstancesHandler { ... } code

For more information on implementing classifiers, see the Writing your own Classifier article.

Groovy
As long as you have the in your Groovy environment, you can directly run these scripts using the  command. Here is an example, executing the script in a Linux bash: code format="bash" groovy -classpath weka.jar \ /some/where/FunkyClassifier.groovy \ -t /my/datasets/data.arff \  code

Weka
In order to execute classifiers written in Groovy, you have to use the classifier. Here is an example, executing the script in a Linux bash: code format="bash" java weka.jar:groovy-all-1.5.7.jar \ weka.classifiers.scripting.GroovyClassifier \ -t /my/datasets/data.arff \ -G /some/where/FunkyClassifier.groovy \ --  code

=Downloads=
 * [[file:UsingJ48.groovy]] - simple example for using J48 in a Groovy script
 * [[file:UsingJ48Ext.groovy]] - a slightly more elaborate example script for using J48
 * [[file:CustomCV.groovy]] - generates train/test splits based on values of a nominal attribute instead of fixed number of folds (based on [|this])Wekalist thread
 * [[file:GeroR.groovy]] - implemented in Groovy
 * [[file:LibsvmWeights.groovy]] - tests several weights strings on a datasets (binary class) using LibSVM and outputs the best weights
 * [[file:multiple_eval.groovy]] - evaluates multiple classifiers on multiple train/test pairs and outputs statistics
 * [[file:AttributeStatistics.groovy]] - Outputs statistics for each attribute in a dataset

=Links=
 * Groovy
 * [|Homepage]
 * [|Getting Started]
 * [|User Guide]
 * [|IDE Support]